# Thread: Help with creating new method

1. Member Join Date
Sep 2011
Posts
45
Rep Power
0

## 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.  Reply With Quote

2. Member Join Date
Sep 2011
Posts
21
Rep Power
0

## 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)`  Reply With Quote

3. Member Join Date
Sep 2011
Posts
45
Rep Power
0

## 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.  Reply With Quote

#### Posting Permissions

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