GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jun-2006, 13:06
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Need help with Java mortgage program


I designed a Java mortgage program and it is working fine but I need to add some more things to it. I want to make the program list the loan balance and the interest paid for each payment over the term of the 30 year loan.(I want it to list for every 1 yr and then every couple of years). I want this list to scroll off the screen or even do a loop to show the partial list, hesitate and then display more of the list. I want to do this program with no GUI(graphical user interface). I will post my program coding below and any help would be appreciated greatly.
JAVA Code:
public class MortgageProgram {
    
    public static void main (String[] args){
        double payment, principal = 200000; //*Principle amount of loan is $200,000
        double annualInterest = 0.0575; //*Interest rate is currently 5.75%
        int years = 30; //*Term of the loan is 30 years
        
        if (args.length == 3) {
           
              principal = Double.parseDouble(args[0]);
              annualInterest = Double.parseDouble(args[1]);
              years = Integer.parseInt(args[2]);
         
           }
           print(principal, annualInterest, years);
        
         
            System.out.println("Usage: Calculate Mortgage Payment Amount ");
            System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term. 
            System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
	    print(principal, annualInterest, years);
            System.exit(0); //*System exits nice and quietly
        }    
    public static double calculatePayment(double principal, double annRate, int years){
        double monthlyInt = annRate / 12;
        double monthlyPayment = (principal * monthlyInt)
                    / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Shows 1 monthly payment multiplied by 12 to make one complete year. 
        return format(monthlyPayment, 2);
    }
    public static double format(double amount, int mortgage) {
        double temp = amount; 
        temp = temp * Math.pow(10, mortgage);
        temp = Math.round(temp);
        temp = temp/Math.pow(10, mortgage);
        return temp;
    }
    public static void print(double pr, double annRate, int years){
        double mpayment = calculatePayment(pr, annRate, years);
        System.out.println("The principal is $" + (int)pr); //*Shows the principle amount in $ value. 
        System.out.println("The annual interest rate is " + format(annRate * 100, 2) +"%");
        System.out.println("The term is " + years + " years"); //*Term is normally in years.
        System.out.println("Your monthly payment is $" + mpayment); //*Shows output of monthly payment. 
      
    }
}
  #2  
Old 16-Jun-2006, 06:19
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with Java mortgage program


Do you already have the amortization formula? I've found it on the web, but haven't got it to work-just-right -- yet.

Consider adding a constructor to the class, and move all your statements in main to the constructor, then all you'll have in main is an object creation, like this: (and EXCEPT for main, you can then remove the static keyword from the other functions)
JAVA Code:
    public static void main (String[] args){
		MortgageProgram MP = new MortgageProgram(args);
    }
the constructor would look like this:
JAVA Code:
    public MortgageProgram(String[] args)	{
        if (args.length == 3) {  
		principal = Double.parseDouble(args[0]);
		annualInterest = Double.parseDouble(args[1])/100;
		years = Integer.parseInt(args[2]);
                print(principal, annualInterest, years);
        }
        else
        {
        	showUsage();  // add this, see code in next block.
	}
   }
the showUsage() function in the if (above) simply groups the usage statements from main into another member function, here: (add to the class)
JAVA Code:
    public void showUsage() {
		System.out.println("Usage: Calculate Mortgage Payment Amount ");
		System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term.
		System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
	 	print(principal, annualInterest, years);
		System.exit(0); //*System exits nice and quietly
	}
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 17-Jun-2006, 01:44
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with Java mortgage program


1. Here is your class, with some modifications above what was mentioned in the last post. This should show even more why to use an object from main(). Minor changes, really, have a look.
(see the comments in all CAPS that point out the changes)

2. a showAmortization() function is included, but it is a full listing at the moment -- no page/screen scrolling.
JAVA Code:
import java.text.*; // FOR THE FORMATTERS

public class MortgageProgram {
	// FORMATTERS: see use in functions print(), and showAmortization().
	NumberFormat currency = NumberFormat.getCurrencyInstance(); // currency format.
	DecimalFormat intRate = new DecimalFormat("##.00");	// interest rate format.

	// ADDED:  rate, months
	double payment = 0.0, rate = 0.0;
	double principal = 200000.0; //*Principle amount of loan is $200,000
	double annualInterest = 0.0575; //*Interest rate is currently 5.75%
	int years = 30; //*Term of the loan is 30 years
	int months = 0; // years expressed in months

        public static void main (String[] args){
		MortgageProgram MP = new MortgageProgram(args);
        }

        public MortgageProgram(String[] args)	{
           if (args.length == 3) {
	       principal = Double.parseDouble(args[0]);
	       annualInterest = Double.parseDouble(args[1])/100; // AUTO-CONVERT -- this assumes
		   						// the user enters as 5.75, etc.
		   						// as your statement in showUsage()
		   						// implies.
	       years = Integer.parseInt(args[2]);
               print();
           }
           else
           {
               showUsage();
	   } 
	}

        public void calculatePayment(){ // ELIMINATED ARGUMENTS and, RETURN TYPE CHANGED TO VOID.
	    rate = annualInterest / 12;
	    months = years * 12;	// USE NEW VARS.
            payment = (principal * rate)
                    / (1 - Math.pow(1/ (1 + rate), months)); //*Shows 1 monthly payment multiplied by 12 to make one complete year.
        }

/*  NO LONGER NEEDED
    public double format(double amount, int mortgage) {
        double temp = amount;
        temp = temp * Math.pow(10, mortgage);
        temp = Math.round(temp);
        temp = temp/Math.pow(10, mortgage);
        return temp;
    }
*/

    public void print(){ // ELIMINATED ARGUMENTS
        calculatePayment();
        System.out.println("The principal is " + currency.format(principal)); //*Shows the principle amount in $ value.
        System.out.println("The annual interest rate is " + intRate.format(annualInterest * 100) +"%");
        System.out.println("The term is " + years + " years"); //*Term is normally in years.
        System.out.println("Your monthly payment is " + currency.format(payment)); //*Shows output of monthly payment.
	showAmortization();
    }

    public void showUsage() {
	System.out.println("Usage: Calculate Mortgage Payment Amount ");
	System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term.
	System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
	print();
	System.exit(0); //*System exits nice and quietly
    }

    public void showAmortization()	{
	double pmt2interest = 0.0;  // payment-to-interest portion.
	double pmt2principl = 0.0;  // payment-to-principal portion.
	double pmtBalance = 0.0;
	
        System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance");
        System.out.println("====\t===========\t===========\t=======");

	for ( int i = 1; i <= months; ++i )
	{
	    pmt2interest = principal * rate;
	    pmt2principl = payment - pmt2interest;
	    pmtBalance = principal - pmt2principl;

	    System.out.println( "#" + i
			+ "\tP=" + currency.format(pmt2principl)
			+ "\tI=" + currency.format(pmt2interest)
			+ (pmt2interest < 10 ? "\t\tB=" : "\tB=")  // tab adjustment.
			+ currency.format(pmtBalance) );

	    principal -= pmt2principl; // reduce by our payment-to-principal
	}
    }
}
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 18-Jun-2006, 10:26
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Re: Need help with Java mortgage program


Thank you so much for your help but when I try to compile my code it still gives me an error saying ';' expected. I will post the rest of my code and if you can look at it and help me fix the error I honestly will appreciate it.
JAVA Code:
public class MortgageProgram {
    
    public static void main (String[] args){
        double payment, principal = 200000; //*Principle amount of loan is $200,000
        double annualInterest = 0.0575; //*Interest rate is currently 5.75%
        int years = 30; //*Term of the loan is 30 years
        
        if (args.length == 3) {
           
              principal = Double.parseDouble(args[0]);
              annualInterest = Double.parseDouble(args[1]);
              years = Integer.parseInt(args[2]);
         
           }
           print(principal, annualInterest, years);
        
         
            System.out.println("Usage: Calculate Mortgage Payment Amount ");
            System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term. 
            System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
	    print(principal, annualInterest, years);
            System.exit(0); //*System exits nice and quietly
        }    
    public static double calculatePayment(double principal, double annRate, int years){
        double monthlyInt = annRate / 12;
        double monthlyPayment = (principal * monthlyInt)
                    / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Shows 1 monthly payment multiplied by 12 to make one complete year. 
        return format(monthlyPayment, 2);
    }
    public static double format(double amount, int mortgage) {
        double temp = amount; 
        temp = temp * Math.pow(10, mortgage);
        temp = Math.round(temp);
        temp = temp/Math.pow(10, mortgage);
        return temp;
    }
    public static void print(double pr, double annRate, int years){
        double mpayment = calculatePayment(pr, annRate, years);
        System.out.println("The principal is $" + (int)pr); //*Shows the principle amount in $ value. 
        System.out.println("The annual interest rate is " + format(annRate * 100, 2) +"%");
        System.out.println("The term is " + years + " years"); //*Term is normally in years.
        System.out.println("Your monthly payment is $" + mpayment); //*Shows output of monthly payment. 
        
        While (mortgageAmount > 0)
 {
	 // Determine the interest that will be paid this month
    double interestPayment = (mortageAmount * monthlymortgageRate * 1);
 
    // Out put the balance and interest paid
    System.out.println ("Mortgage Amount: " + mortgageAmount + "\tInterest Payment: " + interestPayment);

    // Decrease the value of the balance by the principle paid
    mortgageAmount = mortgageAmount - monthlyPayment;
 }
    }
}
  #5  
Old 18-Jun-2006, 12:02
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with Java mortgage program


Notice the loop where you have 'While', that should be 'while'.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 19-Jun-2006, 08:22
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Re: Need help with Java mortgage program


When I do change the "While" into "while" I am getting a handful of errors saying something along the lines of cannot resolve symbol variable mortgageAmount. Do you know any way that I can fix this? I can't figure it out at all. I appreciate all your time and effort =)
  #7  
Old 19-Jun-2006, 12:21
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with Java mortgage program


What/where is variable 'mortgageAmount'? It is not defined, that is the basis of the error: "cannot resolve symbol variable mortgageAmount" -- so, you either need to create it, or use 'principal' instead.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #8  
Old 19-Jun-2006, 16:27
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Re: Need help with Java mortgage program


I did everything as you said but I am still getting errors. If you can look at my code I would appreciate it very kindly.
JAVA Code:
import java.io.*;
import java.util.*;

public class MortgageProgram {
    
    public static void main (String[] args){
        double payment, principal = 200000; //*Principle amount of loan is $200,000
        double annualInterest = 0.0575; //*Interest rate is currently 5.75%
        int years = 30; //*Term of the loan is 30 years
        
        if (args.length == 3) {
           
              principal = Double.parseDouble(args[0]);
              annualInterest = Double.parseDouble(args[1]);
              years = Integer.parseInt(args[2]);
         
           }
           print(principal, annualInterest, years);
        
         
            System.out.println("Usage: Calculate Mortgage Payment Amount ");
            System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term. 
            System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
	    print(principal, annualInterest, years);
            System.exit(0); //*System exits nice and quietly
        }    
    public static double calculatePayment(double principal, double annRate, int years){
        double monthlyInt = annRate / 12;
        double monthlyPayment = (principal * monthlyInt)
                    / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Shows 1 monthly payment multiplied by 12 to make one complete year. 
        return format(monthlyPayment, 2);
    }
    public static double format(double amount, int mortgage) {
        double temp = amount; 
        temp = temp * Math.pow(10, mortgage);
        temp = Math.round(temp);
        temp = temp/Math.pow(10, mortgage);
        return temp;
    }
    public static void print(double pr, double annRate, int years){
        double mpayment = calculatePayment(pr, annRate, years);
        System.out.println("The principal is $" + (int)pr); //*Shows the principle amount in $ value. 
        System.out.println("The annual interest rate is " + format(annRate * 100, 2) +"%");
        System.out.println("The term is " + years + " years"); //*Term is normally in years.
        System.out.println("Your monthly payment is $" + mpayment); //*Shows output of monthly payment. 
        
        while (principal > 0)
 
	 // Determine the interest that will be paid this month
    double interestPayment = (principal * monthlymortgageRate * 1);
 
    // Out put the balance and interest paid
    System.out.println ("Mortgage Amount: " + principal + "\tInterest Payment: " + interestPayment);

    // Decrease the value of the balance by the principle paid
    principal = principal - monthlyPayment;
 }
    }
}
  #9  
Old 19-Jun-2006, 18:44
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with Java mortgage program


You deleted the opening '{' to the while loop. You will have other errors, good luck!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #10  
Old 26-Jun-2006, 10:21
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Re: Need help with Java mortgage program


I need help again with my Java Program if it's possible. Here is what I want to do with it this time. I want to modify the mortgage program for to display 3 mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. Use an array to hold the three interest rates and an array for the three loan terms. Display the mortgage payment amount for each loan.
JAVA Code:
import java.lang.Math.*;
import java.text.*;
import java.io.*;


public class MortgageCalculator
{
//variables that are to be used and the information in the program. 
int i;
int intSelection;
int intSelected;
int[] intTerms = { 7*12, 15*12, 30*12 };
double[] dblRates = { 0.0535, 0.055, 0.0575 };
int intOption[] = {1, 2, 3};
double dblLoanAmt = 200000;
BufferedReader keyboardInput;
}

public MortgageCalculator
{


import java.lang.Math.*;
import java.text.*;
import java.io.*;

public class MortgageCalculator
{
}
// (Main) method that will display all of the output.
 public static void main(String[] args)
{

// Define the variables to be used
double principle = 200000;  			// mortgage amount to be borrowed
int termArray[] = {360, 180, 84};		// Different terms in the array(months 360,180,84)
double rateArray[] = {0.0575, 0.055, 0.0535}; // Different interest rates for the different months (5.75,5.5, and 5.35)
double payment;							// Monthly Payment
double balance = 200000;				// Current Balance of the Mortgage(will fluctuate as user makes his payments)
double interestPaid; 					// Amount paid for interest on the loan of the mortgage
int i = 1;									

// Array Size
int size = rateArray.length - 1;

// Correct and proper Currency Format
DecimalFormat decimalAmount = new DecimalFormat("#,##0.00");

// Variables used for calculating Mortgage
System.out.println();
System.out.println("\t\t\t\tMortgage Calculator\n");
System.out.println("Principle: \t$" + decimalAmount.format(principle));
System.out.println();
System.out.println("Term\t \tRate\t\tPayment");

for (i=size; i>=0; i--)
	{
// Payment method, principle, terms and the interest rate array. 
payment = CalculatePayment(principle, termArray[i], rateArray[i]);

// Should display payment, rate and the amount in the current mortgage. 
System.out.println(termArray[i]/12 + " \t\t" + 100 * rateArray[i] + "%\t\t$" + decimalAmount.format(payment));
	}
System.out.println();
	}

// Used to calculate the amound in the payment based on the principle, the term and the interest rate. 
public static double CalculatePayment(double principle, int term, double rate)
    {
		//Returns the amount of the mortgage and does all the fancy work for the user behind the scenes in the meanwhile exiting quietly. 
return(principle * Math.pow(1+(rate/12), term) * rate/12) / (Math.pow(1 + rate/12, term) - 1);
	}

}
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My first Java Program (Desperate for Help) Skyer Java Forum 9 14-Nov-2006 15:00
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11
New mortgage affiliate program!!!! leadscash Member Announcements, Advertisements & Offers 0 20-Oct-2005 02:33
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
calling a java program from C Magicman C Programming Language 3 20-Oct-2004 07:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.