Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
May 20th, 2007, 10:17 PM
 mic822
Registered User

Join Date: Mar 2007
Location: Chicago
Posts: 4
Time spent in forums: 1 h 4 m 53 sec
Reputation Power: 0
Java compound interest

Hi everyone, I am having difficultly completing a program that calculates compund interest based based on user's input, any help would be appreciated.

Code:
public class CompoundInterest
{

public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);

System.out.print("Enter the initial investment amount: ");
int amt = sc.nextInt();

System.out.print("Enter the interest rate: ");
int rt = sc.nextInt();

System.out.print("Enter the number of years: ");
int yrs = sc.nextInt();

}

public static double CompoundByPow(double amt, double rt,  int yrs,double finalInterest)
{
//error here  finalInterest = amt* Math.pow((1 + rt)*yrs);

return finalInterest;
}

public static double CompoundByLoop(double amt, double rt, int yrs, double interest)
{
for ( yrs = 0;  yrs > 10;  yrs++ ) {
interest = amt * rt*yrs;
amt += interest;
System.out.println(interest);
}

}
}

error message
pow(double,double) in java.lang.Math cannot be applied to (double)
finalInterest = amt* Math.pow((1 + rt)*yrs);

#2
May 20th, 2007, 10:31 PM
 nizack05
Contributing User

Join Date: Oct 2003
Location: Wisconsin
Posts: 134
Time spent in forums: 1 Day 8 h 30 m 33 sec
Reputation Power: 44
Quote:
 Originally Posted by mic822 Hi everyone, I am having difficultly completing a program that calculates compund interest based based on user's input, any help would be appreciated. Thanks in advance Code: public class CompoundInterest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the initial investment amount: "); int amt = sc.nextInt(); System.out.print("Enter the interest rate: "); int rt = sc.nextInt(); System.out.print("Enter the number of years: "); int yrs = sc.nextInt(); } public static double CompoundByPow(double amt, double rt, int yrs,double finalInterest) { //error here finalInterest = amt* Math.pow((1 + rt)*yrs); return finalInterest; } public static double CompoundByLoop(double amt, double rt, int yrs, double interest) { for ( yrs = 0; yrs > 10; yrs++ ) { interest = amt * rt*yrs; amt += interest; System.out.println(interest); } } } error message pow(double,double) in java.lang.Math cannot be applied to (double) finalInterest = amt* Math.pow((1 + rt)*yrs);

that's because the static method pow does not take one double, it takes two. Your code is passing in one double. look at the api:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#pow(double,%20double)

#3
May 20th, 2007, 10:58 PM
 mic822
Registered User

Join Date: Mar 2007
Location: Chicago
Posts: 4
Time spent in forums: 1 h 4 m 53 sec
Reputation Power: 0
Thanks

-michelle

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Java compound interest