NJD
NJD

Reputation: 107

Math methods (powers)

I've been having trouble writing a math function. It is supposed to take in 3 variables and compute the equation like so.

answer = x(1 + y/100)^ z

I have it written as:

public compute_cert (int years, double amount, double rate)
{
certificate = amount * pow(1 + rate/100, years);
return certificate;
}

I am also to return the certificate (answer) amount but I get this error:

CDProgram.java:54: error: invalid method declaration; return type required
public compute_cert (int years, double amount, double rate)

Thanks for the help.

Upvotes: 0

Views: 76

Answers (2)

Sunny Patel
Sunny Patel

Reputation: 8078

You're missing a lot of types: That of the local variable certificate and the type in the method header. You also need to say Math.pow instead of just pow, or it doesn't know what pow method you're talking about. Your math is right.

public double compute_cert (int years, double amount, double rate)
{
    double certificate = amount * Math.pow(1 + rate/100, years);
    return certificate;
}

Upvotes: 4

Elliott Frisch
Elliott Frisch

Reputation: 201399

You're missing return type, and you probably want to use Math.pow -

// add the type - "double"
private double certificate;

// specify the signature.
public double compute_cert(int years, double amount, double rate) 
{
  certificate = amount * Math.pow(1 + rate / 100, years);
  return certificate;
}

Upvotes: 2

Related Questions