2

I am trying to do some calculations using different values stored in an array. The problem is that my array's values increase but the calculations are coming out decreasing.

Main:

public static void main(String[] args) {
    double pay, rate, deposit;
    int years;
    char yes = 'y';
    char answer;
    double[] interest = new double[15];
    interest[0] = 3.75;
    interest[1] = 4.00;
    interest[2] = 4.25;
    interest[3] = 4.50;
    interest[4] = 4.75;
    interest[5] = 5.00;
    interest[6] = 5.25;
    interest[7] = 5.50;
    interest[8] = 5.75;
    interest[9] = 6.00;
    interest[10] = 6.25;
    interest[11] = 6.50;
    interest[12] = 6.75;
    interest[13] = 7.00;
    interest[14] = 7.25;

    mortgageClass mortgagePayment = new mortgageClass();

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Are you a first time buyer? ");
    answer = keyboard.next().charAt(0);
    if (answer == yes)
    {
        mortgagePayment.setRate(4.50);
    }
    else
    {
        mortgagePayment.setRate(interest[0]);
    }

    System.out.print("What is your mortage term? ");
    years = keyboard.nextInt();
    mortgagePayment.setTermYears(years);

    System.out.print("What is your amount of mortgage? ");
    pay = keyboard.nextDouble();
    mortgagePayment.setAmount(pay);

    System.out.print("What is your deposit amount? ");
    deposit = keyboard.nextDouble();
    mortgagePayment.setdepositAmt(deposit);

    System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
    System.out.println();

    for ( int i = 0; i < interest.length; i++ ){
        mortgagePayment.setRate(interest[i]);
        System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
        System.out.println();
    }
}

MortgagePayment Class

public class mortgageClass {

    private double rate;
    private double loanAmount;
    private double depositAmt;
    private int termYears;

    public void setRate(double r) {
        rate = r;
    }

    public void setAmount(double loan) {
        loanAmount = loan;
    }

    public void setTermYears(int years) {
        termYears = years;
    }

    public void setdepositAmt(double amount) {
        depositAmt = amount;
    }

    public double getMonthlyPayment() {
        rate /= 100.0;
        loanAmount = loanAmount - depositAmt;
        double monthlyRate = rate / 12.0;
        int termMonths = termYears * 12;
        double monthlyPayment = (loanAmount * monthlyRate)
                / (1 - Math.pow(1 + monthlyRate, -termMonths));
        return monthlyPayment;
    }
}

The output is decreasing

Your mortgage payment is 1309.00 
Your mortgage payment is 1220.49 //my output
Your mortgage payment is 1128.42 

When it should be increasing

Your mortgage payment is 1309.00 
Your mortgage payment is 1331.44 //Expected output
Your mortgage payment is 1354.10

Obviously the first value is assigned correctly, so why isn't the increment working?

EDIT: I have added a print statement to see if the correct values are being used and it seems like they are

  for ( int i = 0; i < interest.length; i++ ){
    mortgagePayment.setRate(interest[i]);
    //System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
    System.out.println(interest[i]);
    System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
    System.out.println();

Output:

3.75
Your mortgage payment is 1309.00 
4.0
Your mortgage payment is 1220.49 
4.25
Your mortgage payment is 1128.42 
4.5
Your mortgage payment is 1032.74 

..... I would just like to know WHY the calculations are decreasing.

7
  • 1
    Why are you doing interest[i] = i + 1;? But also what does setRate and getMonthlyPayment do? Commented Jun 24, 2015 at 4:54
  • 7
    You need to show us the code for the MortgagePayment class. Commented Jun 24, 2015 at 4:55
  • @TimBiegeleisen I have added the whole program Commented Jun 24, 2015 at 5:25
  • What are the input values you used to generate the output you gave above? Commented Jun 24, 2015 at 6:29
  • @TimBiegeleisen Are you a first time buyer? n What is your mortgage term? 15 What is your amount of mortgage? 195000 What is your deposit amount? 15000 Commented Jun 24, 2015 at 14:56

1 Answer 1

1

skip

interest[i] = i + 1;

EDITED 2nd time :

the problem is

loanAmount = loanAmount - depositAmt;

every time you call mortgagePayment.getMonthlyPayment()

it decreasing the loanAmount , Thats why monthly amount is coming down

suggested change :

 public double getloanAmount(){

        return loanAmount - depositAmt;
    }

    public double getMonthlyPayment() {
        rate /= 100.0;

        double loanAmount = getloanAmount();

        double monthlyRate = rate / 12.0;

        int termMonths = termYears * 12;

   double monthlyPayment = (loanAmount * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -termMonths));

        return monthlyPayment;
    }

that will fix the problem.

EDITED : use this fromula Monthly Mortgage Payment

M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]

The variables are as follows:

    M = monthly mortgage payment
    P = the principal, or the initial amount you borrowed.
    i = your monthly interest rate. Your lender likely lists interest rates as an annual figure, 
        so you’ll have to divide by 12, for each month of the year. So, if your rate is 5%, 
        then the monthly rate will look like this: 0.05/12 = 0.004167.
    n = the number of payments, or the payment period in months. If you take out a 30-year fixed rate mortgage, 
        this means: n = 30 years x 12 months per year = 360 payments.
Sign up to request clarification or add additional context in comments.

4 Comments

I have already tried that and it produces the exact same output
the problem is somewhere else , even the formula I proposed is same . discussing the problem in answer
It worked! Can you explain to me what the changes in the mortgagePayment class did exactly?
I just added getloanAmount() method. And removed line "loanAmount = loanAmount - depositAmt;" from getMonthlyPayment() and added "double loanAmount = getloanAmount();" in to the same method (getMonthlyPayment())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.