should be a simple java question

Joined
Aug 10, 2001
Messages
2,312
for some reason the few times i've tried to use the Math pow() function in java it hasn't worked. i've always worked around it because i've known what the power i was going to raise to was, but that is not the case here. i've tried many different ways of writing this program (in terms of the pow()) but none work. here is what i have at the moment.

Code:
import java.io.*;
import java.util.*;
import java.lang.Math.*;

public class CompoundInterest {
    // define our variables
    private static double deposit;  // the deposit amount
    private static double apr;  // the APR of the investment
	 public static double apy;  // the rate to be used in calculations
    private static double term;  // the term of the investment
	 private static double year;	// the current age of the investment
	 private static double multiplier;	// to be multiplied by the deposit amount in the compoundAnnual method
    
	 // begin main class
    public static void main(String[] args) throws IOException {
	 		// setup user input stream
		BufferedReader stdin = new BufferedReader (
			new InputStreamReader (System.in));
					// now read the user input
               System.out.println("This program will calculate the growth of an investment .");
					System.out.println("Enter the deposit amount:");
					deposit = Double.parseDouble (stdin.readLine());
					System.out.println("Enter the Annual Percentage Rate of the investment:");
					apr = Double.parseDouble (stdin.readLine());
					apy = (1 + (apr/100)); 
					System.out.println("Enter the term of the investment in years:");
					term = Double.parseDouble (stdin.readLine());
					
					// print the results
					for (year = 0; year <= term; ++year) {
						multiplier = (pow(double apy, double year));
						System.out.println("The value of the investment after " + year + " is " + compoundAnnual(deposit, multiplier));
						}
					if (year > term) {
						multiplier = (pow(double apy, double term));
						System.out.println("The value of the investment after " + term + " is " + compoundAnnual(deposit, multiplier));
						}

    } // close main
    
    public static double compoundAnnual(double deposit, double multiplier) { 
	 		return (deposit * multiplier);
	 } // close method compoundAnnual
	 
} // close class compoundAnnual

if you could tell me how to use pow() correctly i'd greatly appreciate it. with what i have i get a '.class' expected error, and with what i had before i got a "symbol not found" error with an arrow seeming to point to apy within the pow().

thanks
 
Wish I could remember this from my AP Comp Sci class. I think you just have to call Math.pow(num,power) and it should work. Imports look fine, IIRC
 
^^ right. unlike c/c++, you can't just call a function. you have to call it from its class, Math.pow(num, pow); the same way you have to call System.out.println(...); and not just println(...);

i don't think you actually need to import Math.*, but it doesn't really hurt anything

iirc there is a way in java 1.5 to import everything in a particular class into the current namespace so that you don't have to actually call Math.whatever and you could call just whatever but i don't remember what that is
 
Back
Top