Java Method Help
Author |
Message |
samanthaxc
|
Posted: Thu Mar 07, 2013 9:23 pm Post subject: Java Method Help |
|
|
My program is supposed to allow the user to input a principle amount and rate, which it will calculate the the balance after it has been compounded for 10 years. It should display the balance at the end of every year. My problem is that when I try to return the amount to the main method, it won't work. some please help!
Java: |
import java.io.*;
import java.text.*;
public class compoundInterest {
public static void main ( String[] args ) throws IOException
{
String depositA, rateA; // String variables are declared
double deposit, rate; // double variables are declared
BufferedReader myInput = new BufferedReader (new InputStreamReader (System. in)); // Buffered Reader reads the number inputed by the user
System. out. println("Enter the amount of your initial deposit: "); // asks user to input deposit amount
depositA = myInput. readLine(); // reads previous value entered
deposit = Double. parseDouble(depositA ); // converts value into a double/decimal
System. out. println("Enter the rate in a decimal form (e.g: 5% -> 0.05): "); //asks user to input rate in decimal form
rateA = myInput. readLine(); // reads previous value entered
rate = Double. parseDouble(rateA ); // converts value into a double/decimal
for (int year = 1; year < 11; year++ )
{
System. out. println("The balance after " + year + " year(s) is:\t" + "$" + findAmount ( year )); // outputs a sentence displaying the balance after each year
}
}
public static int findAmount (int finalNumA )
{
double amount = 0, amountB = 0, rate = 0, year = 0, deposit = 0;
int finalNumB;
amount = (1 + rate ); // adds 1 to the decimal rate given earlier
amountB = Math. pow(amount, year ); // takes amount and ploaces it to the power of 10. assigned a new variable, amountB
DecimalFormat total = new DecimalFormat("#0.00"); // Assigns two decimal places to the answer
double amountC = amountB * deposit; // amountB is multiplied by the deposited amount, and is assigned a variable, amountC
String finalNum = total. format(amountC ); //applies decimal place format to amountC, and is assigned a new variable, amountD
return finalNumB;
}
}
|
Mod Edit:
Please wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code: |
[syntax="java"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Thu Mar 07, 2013 9:45 pm Post subject: RE:Java Method Help |
|
|
Define exactly how it doesn't work. Does it show the wrong calculation? Does it not output at all? Does it cause an error? If it causes an error, what does the error message say? |
|
|
|
|
![](images/spacer.gif) |
samanthaxc
|
Posted: Thu Mar 07, 2013 9:47 pm Post subject: Re: Java Method Help |
|
|
When I compile the program, it says the "return finalNumB" is incompatible and should be an 'int' |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Thu Mar 07, 2013 9:52 pm Post subject: RE:Java Method Help |
|
|
Well you're defining it as an int alright. However, you seem to be trying to return a value that hasn't been initialized (you don't set it to anything).
You're probably going to run into all the years showing the same result. For this, I suggest you take a closer look at what variables you're using for what (and which you're not using at all).
You also seem to be trying to format the value into a string inside that function that will be returning an integer. I suggest you make two functions, one to format the number into a humanly readable money string, and another to do your logic.
Eg:
Java: |
formatMoney(compoundInterest(deposit, rate, numIterations))
|
|
|
|
|
|
![](images/spacer.gif) |
ishidon
|
Posted: Thu Mar 07, 2013 10:13 pm Post subject: Re: Java Method Help |
|
|
I believe what you're looking for is
code: |
import java.io.*;
public class compoundInterest {
public static void main( String[] args ) throws IOException {
String depositA, rateA;// String variables are declared
double deposit, rate;// double variables are declared
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed by the user
System.out.println("Enter the amount of your initial deposit: ");// asks user to input deposit amount
depositA = myInput.readLine();// reads previous value entered
deposit = Double.parseDouble(depositA);// converts value into a double/decimal
System.out.println("Enter the rate in a decimal form (e.g: 5% -> 0.05): ");//asks user to input rate in decimal form
rateA = myInput.readLine();// reads previous value entered
rate = Double.parseDouble(rateA);// converts value into a double/decimal
double num;
for (int year = 1; year <= 10; year++) {
num = cInterest(deposit, rate, year);
System.out.printf("The balance after %d year(s) is: $%.2f\n", year, num);// outputs a sentence displaying the balance after each year
}
}
public static double cInterest(double principal, double interest, int year) {
return (principal*Math.pow((1+interest),year));
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
|
|