Hopefully someone can help me...I'm a student taking my first Java class. We are working on classes (building them). For this assignment the teacher gave us a test program that we have to build a class for to computer some basical mortgage calculations. Below is the class I built that is being passed variables from the end-user and is supposed to return some calculations. No matter what I try I keep getting an error

- Exception in thread "main" java.lang.NoSuchMethodError: MortgageCalcs.futureValue()D at
TestMortgage.main(TestMortgage.java:28)

Like I said...he gave us TestMortgage...

public class MortgageCalcs
    {
        private double rate; //All of these private statments are decleration of variables
        private double term;
        private double principal;
        private double calcPayment;
        private double futureValue;
        private double intCharge;
        private double r;                
        private double x;
        private double y;
        private double temp;
        
                
        public MortgageCalcs (double interestRate, double term, double principal) //begining of the constructor that captures the data from method above
            {
                rate = interestRate; //assigns value to interest rate being fed by TestMortgage 
                term = term; //assigns value to term that is being fed by TestMortgage
                principal = principal; //assigns value to principal that is being fed by TestMortgage
            } //end of constructor
            
        public double calcPayment (double principal) //method to calculate the value of monthly payment
            {
                r = rate/100; //calculation of variable r as provided in instructions                
                x = 1.0+r/12.0; //calculation of variable x as provided in instructions
                y = term*12.0; //caluclation of variable y as provided in instructions
                temp = (1.0/Math.pow(x,y)); //calculation of variable temp as provided in instructions
                
                calcPayment=(principal*r/12.0)/(1-temp); //calculation of payment based on the formula given in instructions
                return calcPayment;
            
            }
                         
        public double futureValue (double calcPayment, double term) // method to calculation of futureValue
            {
                futureValue=calcPayment*term*12;
                return futureValue;//
            }
            
        public double intCharge (double futureValue, double principal)
            {
                intCharge=futureValue-principal;
                return intCharge;
            }   
        
        }

Check the method call parameters in the class he gave you versus the futureValue() method that you have defined. Do the parameter types match?

Then he must have explicitly told you which methods to implement in your class. His tester class obviously works against a very specific interface and something about your own class is not matching up.

Here are his instructions...I've read through it a few times...


Assignment #2 CSI205 Assigned 09/20/11
Due 09/29/11

Assignment:

Create a class that can be used by clients to perform mortgage calculations.
Specifically, your class will be used to calculate mortgage payment, total interest
charge, and future value of a mortgage based on the amount of the loan (Principal),
interest rate, and term.

The Mortgage Payment is computed as:

Payment=(Principal*r/12.0)/(1-temp)

where
r = interest rate/100.0
temp = (1.0/pow(x,y))
x = 1.0+r/12.0
and y = term of mortgage in years * 12.0

pow(x,y) is a method defined in the Math class that raises x
to the y power.

The Future Value of the loan is computed as:

Future value = Payment * term of mortgage in years * 12

The Interest Charge is computed as:

Interest Charge = Future Value - Amount of Loan


Your class should contain the following:

Private Data Fields:

double interestRate
double term
double principal


Public Methods:

//Constructor Method. Accepts values for interest rate, term, and principal and places these
//values into the corresponding private data fields

MortgageCalcs(double interestRate, double term, double principal)


//this method calculates and returns the Monthly Payment.

double calcPayment()

//this method calculates and returns the Future Value.

double futureValue()

//this method calculates and returns the Interest Charge.

double intCharge()

A sample client has been created that will allow you to test your implementation of MortgageCalcs.

To test MortgageCalcs.java:

- copy the client application, named TestMortgage.class, from the class folder on the ITS Unix
system to your own folder. The client is located in /home2/classes/csi205/TestMortgage.class

- Compile your MortgageCalcs.java

- Run TestMortgage and follow the on-screen instructions. TestMortgage will
create and use one of your MortgageCalcs object.

Notes:
-Include Comments in your code and format your program neatly. Remember to
include your name and Student ID# in your comments
-All variables you use for mathematical computations should be of type double
-You should only submit MortgageCalcs.java. (the "M" and "C" are uppercase!)
-The turnin command for assignment #2 is
turnin-csi205 -c csi205 -p prog2 MortgageCalcs.java

So he asked for

//this method calculates and returns the Future Value.
double futureValue()

How does that compare to the method that you wrote?

Ok, I understand what the difference is. I was under the impression that we MUST pass something to the method in order to computer what was requested of the method. Is that not the case? What's in the () is what we are passing, correct??? Unless within a class we don't need to pass the method something because it's aware of what's up top.

Your class already contains the variables (yes, the private ones up top) needed to make the calculation, so you can use them directly and return the value.

A side note, check your constructor carefully and note that you're using the exact same names for the parameters and instance variables you're setting. It might have some consequences in your program if you don't properly qualify them. Check your class notes on that.

Yeah...I noticed once I fixed the () that a few of the values for the output = to zero...probably because the constructor sets them to zero...need the change that.

Thank you for all of your help...much obliged!

For some reason my futureValue output is zero...

public double calcPayment () //method to calculate the value of monthly payment
            {
                double r = rate/100; //calculation of variable r as provided in instructions                
                double x = 1.0+r/12.0; //calculation of variable x as provided in instructions
                double y = years*12.0; //caluclation of variable y as provided in instructions
                double temp = (1.0/Math.pow(x,y)); //calculation of variable temp as provided in instructions
                
                calcPayment=(amount*r/12.0)/(1-temp); //calculation of payment based on the formula given in instructions
                return calcPayment;
            
            }
                         
        public double futureValue () // method to calculation of futureValue
            {
                futureValue=calcPayment*years*12.0;
                return futureValue;
            }

calcPaymen computes properly...

Edited 5 Years Ago by clickclack: n/a

Your original code had no variable named 'years'. Post your updated code.

Note that you have defined more private variables than your instructor requested. He/she may only want you to include the ones called for.

This article has been dead for over six months. Start a new discussion instead.