Does this constructor look right?

Hi I am still getting familiar with the basics behind constructors. I am trying to make a program that computes loan amounts, interest rates, etc. My program works however the answer I'm getting seems a bit crazy. Here is my code:

Quote:

public class ComputePayment{

public double computePayment(

double loanAmt,

double rate,

double futureValue,

int numPeriods) {

double interest = rate / 100.0;

double partial1 = Math.pow((1 + interest),

- numPeriods);

double denominator = (1 - partial1) / interest;

answer = (-loanAmt / denominator)

- ((futureValue * partial1) / denominator);

return answer;

}

void printAnswer() {

// loanAmt, rt, futv, numP

computePayment(4000000,.04,5000000,22);

System.out.println("The answer is: " +

answer);

}

public static void main(String[] args) {

ComputePayment pay=new ComputePayment();

pay.printAnswer();

}

}

I also don't understand money all that well, but my answer is negative, should that be the case, or is my program wrong or did I just enter in the values wrong

Re: Does this constructor look right?

The answer is the monthly amount

Re: Does this constructor look right?

Hello there.

The code you have written is not a constructor. It is a double return type method. A constructor is a method with no type that has the exact same name of the class it's within and returns nothing.

A constructor of your class would be:

Code:

`public ComputePayment(){`

}

Re: Does this constructor look right?

First, please use code tags and not quote tags (Use the # button in the advanced editor). I've done it here to make it more legible (at least for me)

Code:

`public class ComputePayment{`

public double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) {

double interest = rate / 100.0;

double partial1 = Math.pow((1 + interest),

- numPeriods);

double denominator = (1 - partial1) / interest;

answer = (-loanAmt / denominator)

- ((futureValue * partial1) / denominator);

return answer;

}

void printAnswer() {

// loanAmt, rt, futv, numP

computePayment(4000000,.04,5000000,22);

System.out.println("The answer is: " +

answer);

}

public static void main(String[] args) {

ComputePayment pay=new ComputePayment();

pay.printAnswer();

}

}

Now, you are not using any constructors in this program (except the implied one). What you have is a method which computes a value and returns the value. But your printAnswer() method does not retrieve the return value from computePayment(). In fact, does this code even compile? It looks like answer is local to printAnswer() is not declared anywhere. Nor have you declared answer in ComputePayment().

Regards,

Jim

Re: Does this constructor look right?

ok, that makes sense. So why is my value negative when I say computePayment(4000000,.04,5000000,22);

Did I put that in wrong or is my method wrong?

Re: Does this constructor look right?

I have no idea. Did you post your current code? The code shown should not even compile.

Regards,

Jim

Re: Does this constructor look right?

Add

Right above double computePayment, and it should compile. Please post the algorithm you are using

Re: Does this constructor look right?

This compiles and gives me a long negative answer.

Code:

`public class ComputePayment extends ConsoleProgram{`

double answer;

double loanAmt;

double rate;

double futureValue;

int numPeriods;

public double computePayment(

double loanAmt,

double rate,

double futureValue,

int numPeriods) {

this.loanAmt=loanAmt;

this.rate=rate;

this.futureValue=futureValue;

this.numPeriods=numPeriods;

double interest = rate / 100.0;

double partial1 = Math.pow((1 + interest),

- numPeriods);

double denominator = (1 - partial1) / interest;

answer = (-loanAmt / denominator)

- ((futureValue * partial1) / denominator);

return answer;

}

void printAnswer() {

// loanAmt, rt, futv, numP

computePayment(400000,.04,500000,24);

System.out.println("Your montly payments are: " +

answer);

}

public static void main(String[] args) {

ComputePayment pay=new ComputePayment();

pay.printAnswer();

}

}

The answer is: -37487.787442077475

Re: Does this constructor look right?

code tags, not quote tags; I fixed them for you this time ...

kind regards,

Jos

Re: Does this constructor look right?

Constructor should be no return type ( even void ).