Asa Nam - 1 year ago 60

Java Question

I am trying to do the calculations for the interest in another method, and I know that I have to make another method outside of main and then put return an the end, but I have no idea what to title this new method and how to go about doing the calculations there. I think it is the while loop that is confusing me. I have done this once before on a different project, so I have an idea of how to do it, but this project isn't anything like the other one and I don't really understand it. Any help is extremely appreciated as I have been working on this for a long time and just want to get it over with. Thanks in advance.

`import java.util.Scanner; // This allows for the use of the scanner in the class`

public class SavingsAccount // Start of class

{

public static void main(String[]args) // Start of main

{

double P; // These store the amounts that will be used in the accruing interest formula

double i;

double n;

double S = 0;

int timesLooped = 0;

Scanner readConsole = new Scanner(System.in); // This is the scanner

System.out.println("I am a savings account interest calculator."); // Prompts the user for input

System.out.println("How much money have you deposited?");

P = readConsole.nextDouble();

S = P;

System.out.println("Now, what is the annual interest rate? (i.e. .05)");

i = readConsole.nextDouble();

System.out.println("Finally, how long do you plan on having the money in the account?");

n = readConsole.nextDouble();

while (timesLooped <= n)

{

S = S + (P * i);

timesLooped += 1;

}

System.out.println("Your balance in that time span is " + S + "."); // Tells you your ending balance

}

}

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Based on your comment, I think you want this:

```
private static double addInterest(double S, double P, double i)
{
return S + (P * i);
}
...
public static void main()
{
...
while (timesLooped <= n)
{
S = addInterest(S, P, i);
}
```

**EDIT**

I made some small improvements just for fun:

- I put the entire interest calculation into the function and used exponentiation rather than a loop.
- I gave the variables more descriptive names.
- I used
`System.out.format`

to print the result.

Here's the code:

```
private static double computeCompoundInterest(double principal, double rate,
double years) {
return principal * Math.pow(1 + rate, years);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("I am a savings account interest calculator.");
System.out.println("How much money have you deposited?");
double principal = scanner.nextDouble();
System.out.println("Now, what is the annual interest rate? (i.e. .05)");
double rate = scanner.nextDouble();
System.out.println("How many years will you hold that money in the account?");
double years = scanner.nextDouble();
double total = computeCompoundInterest(principal, rate, years);
System.out.format("Your balance at the end of that period will be %.2f.\n", years, total);
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**