# Help with creating new method

• 10-23-2011, 07:42 PM
Help with creating new method
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.
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.
• 10-23-2011, 07:59 PM
coasterguy10
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.
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
Code:

`Balance(//type in your parameters here)`
• 10-24-2011, 04:28 PM
```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;         } }```