Results 1 to 3 of 3
  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Help with creating new method

    Java Code:
    import java.util.Scanner;
    /*
    This program uses a method that computes the balance
    of a bank account after a number of years with added interest.
    Balance.java
    */
    
    public class Balance {
    	public static void main(String[] args){
    	Scanner in = new Scanner(System.in);
    	
    	//Variables
    	double initialBalance;
    	double interestRate;
    	double years;
    	double balance;
    	
    	//User input
    	System.out.println("Enter the initial balance: ");
    	initialBalance = in.nextDouble();
    	System.out.println("Enter the interest rate: ");
    	interestRate = in.nextDouble();
    	System.out.println("Enter the number of years: ");
    	years = in.nextDouble();
    	
    	//Compute balance and print
    	balance = initialBalance * Math.pow((1+interestRate), years);
    	System.out.printf("%.2f\n", balance);
    	}
    }
    So, I decided to just create the program (what it does is computes compounded interest).
    But the assignment is to create a new method (balance) that creates three parameters (initialBalance, interestRate, and years) and returns the balance.

    I'm not really sure how I go about this...I know how to create a new method, I would just make new brackets and declare the method and I guess put the parameters inside. This is the method description I was given in the assignment.
    Java Code:
    /** 
    * Computes new balance given initial balance, interest rate, 
    * and number of years 
    * @param initialBalance Initial Balance 
    * @param interestRate Interest rate 
    * @param numYears Number of years for which to compute interest 
    * @return New Balance after numYears 
    */
    What I need help with is knowing what information to put where...What do I put under my main method, what under the balance method? How exactly does "calling" a method work?
    I've been reading the book and it is just hard to get my head around. My professor tries to help, but he likes to fill every minute with lecture, no hands on work whatsoever, so it is a bit hard to understand these things on my own. Thanks in advance.

  2. #2
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Help with creating new method

    In order to make a new method in the same class you need to declare the method outside of the main brackets, but inside the class brackets. You will declare the method the same way you would any other method.
    Java Code:
    public double Balance(double initialB, double interestR, double numYears)
    {
    double balance = 0.0;                                                  
    double initialBalance = initialB;
    double interestRate = interestR;
    double years = numYears;
    // add code to compute the end balance
    return balance;
    }
    To call the method in main you would simply type
    Java Code:
    Balance(//type in your parameters here)

  3. #3
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with creating new method

    Hm, I am still a little lost.
    Java Code:
    public class Balance {
    	public static void main(String[] args){
    	Scanner in = new Scanner(System.in);
    	
    	//Variables
    	double initialB;
    	double interestR;
    	double numYears;
    	double balance = 0;
    	
    	//User input
    	System.out.println("Enter the initial balance: ");
    	initialB = in.nextDouble();
    	System.out.println("Enter the interest rate: ");
    	interestR = in.nextDouble();
    	System.out.println("Enter the number of years: ");
    	numYears = in.nextDouble();
    	}
    	
    	public double balance(double initialB, double interestR, double numYears){
    	
    	
    	//Variables
    	double initialBalance = initialB;
    	double interestRate = interestR;
    	double years = numYears;
    	double balance = 0;
    	
    
    	
    	//Compute balance and print
    	balance = initialBalance * Math.pow((1+interestRate), years);
    	return balance;
    	}
    }
    What goes in my main method? I tried typing my method and parameters and I got an error =/
    *EDIT
    I messed around and think I got it sorted mostly, the program works except the answer is not getting returned on the screen..hm.
    *I know you said I had to call my method, but where does that go? Do I put
    balance(double initialB, double interestR, double numYears)
    in my main method?
    Last edited by Aimforthehead; 10-24-2011 at 05:02 PM.

Similar Threads

  1. Replies: 7
    Last Post: 05-05-2011, 05:53 AM
  2. Creating a main method?
    By Brainz in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 10:56 AM
  3. Which method is best for creating Threads?
    By makpandian in forum Threads and Synchronization
    Replies: 11
    Last Post: 06-08-2009, 08:00 AM
  4. Creating a GUI - which method
    By matpj in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:40 PM
  5. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •