Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Mortgage Calculator Issue

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mortgage Calculator Issue

    HI all,

    I have my code working only problem is that on the first loan its supposed to stop after 84 months, but it goes all the way to 100.
    This of course causes a negative ammount to be displayed. I need to figure out how to make only this first load (the 7 year one) stop at 84 months, but the others need to continue past that because they are 180 months and 360 months respectively.

    Here is my code:
    // Dwight Welsh
    /Mortgage Calculator
     
     //Principal = The initial amount of the loan
     //InterestYearly = The annual interest rate (from 1 to 100 percent)
     //InterestMonthly = monthly interest in decimal form = interestYealy / (12 * 100)
     //LengthInYears = the length (in years) of the loan
     //NumberOfPayments = number of months over which loan is amortized = L * 12
     //InterestMonthly = InterestYearly / (12.0*100.0)
     //NumberOfPayments = LengthInYears *12
     //MonthlyPayment = (Principal * InterestMonthly) / (1.0 - (Math.pow(1.0 + InterestMonthly, - NumberOfPayments)))
     
     //This Imports the various classes
     
    import java.text.NumberFormat;
    import java.util.Date;
    import java.util.Scanner;
    import java.util.Formatter;
     
     
    class Loan {
    	public final double principal;
    	public final double interestYearly;
    	public final int lengthInYears;
     
    	public Loan(double principal, double interestYearly, int lengthInYears) {
    		this.principal = principal;
    		this.interestYearly = interestYearly;
    		this.lengthInYears = lengthInYears;
    	}
     
    	public double ComputeMonthlyInterest() {
    		return interestYearly / 12.0;
    	}
     
    	public double ComputeMonthlyPayment() {
    		double interestMonthly = ComputeMonthlyInterest();
    		double numberOfPayments = lengthInYears * 12.0;
    		return (principal * interestMonthly)
    				/ (1.0 - (Math.pow((1.0 + interestMonthly), -numberOfPayments)));
    	}
    }
     
    // File name is Mortgage.java
    public class Mortgage {
    	private Loan loan;
    	private NumberFormat nf;
     
     
       // Calculate and Return the Monthly Payment
     
    	public Mortgage(double principal, double interestYearly, int lengthInYears) {
    		this(new Loan(principal, interestYearly, lengthInYears));
    	}
     
    	public Mortgage(Loan loan) {
    		this.loan = loan;
    		nf = NumberFormat.getCurrencyInstance();
    	}
     
       // Displays the Monthly Payment
     
    	public void DisplayMonthlyPayment() {
    	        double monthlyPayment = loan.ComputeMonthlyPayment();
    	        double total = loan.principal;
    	        double mrate = loan.ComputeMonthlyInterest();
     
    	        //tells the number of lines to show on a screen before asking the user to press enter
    	        int linesPerScreen = 20;
     
    	        //counts the number of lines (months) printed to the console
    	        int monthsCount = 0;
     
    	        //This will read input from the console
    	        Scanner input = new Scanner(System.in);
     
     
     
    			        System.out.println ("Loan amount " + nf.format(loan.principal)
    			            + " over " + loan.lengthInYears + " years term at "
    			            + (loan.interestYearly * 100) + "% interest rate");
     
    			System.out.println("Month\t\tPayment Amount\t\tInterest\tPrincipal");
     
                 //Start of Loop code
     
     
    	        while (monthsCount < loan.lengthInYears * 12){
    				for( int k = 0 ; k < linesPerScreen ; k++ ){
    	                double interest = total * mrate;
    	                total -= (monthlyPayment - interest);
     				           System.out.printf ("%2d\t\t%s\t\t%s\t\t%s\n",
    					                		monthsCount+1, nf.format(monthlyPayment), nf.format(interest), nf.format(total));;
     
     
    	            	//This will cause the month to increase
    	            	monthsCount = monthsCount+1;
    				}
     
    				System.out.println ("Press Enter to continue...");
    				input.nextLine();
    	    }
    	}
     
    	public static void main(String[] args) {
     
    		System.out.println("Dwight Welsh: " + new Date());
    		Mortgage pgm = new Mortgage(200000, 0.0535, 7);
    		pgm.DisplayMonthlyPayment();
    		Mortgage pgm2 = new Mortgage(200000, 0.0550, 15);
    		pgm2.DisplayMonthlyPayment();
    		Mortgage pgm3 = new Mortgage(200000, 0.0575, 30);
    		pgm3.DisplayMonthlyPayment();
     
    	}
     
    }

    Thanks in advance

    COY
    Last edited by coyboss; February 5th, 2011 at 10:31 AM. Reason: correct code display


Similar Threads

  1. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  2. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  3. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM
  4. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM