Solved

Java Mortgage Calculator

Posted on 2008-10-20
8
404 Views
Last Modified: 2013-11-23
Hello experts, I am in Java class and I am having difficulties with my assignment.  I have an assignment that I must create a calculator that can show the monthly payments on a 30 year mortgage. The principals are from $100000 -$200000 and shows the amount of interest between 6%-10% in 0.5% intervals. The output should be in a table format. Here is what our instructor is asking us to perform:

 Programming problem 2: Mortgage monthly payment
 
           The monthly payment on mortgage loan of L dollars, at a rate of interest r is given
         by

          Monthly payment = [ L(r/12)(1 + r/12)  12 n] / [(1 + r/12)12n - 1]

Where N is the number of years of mortgage. Write a program to find the monthly
payment for 30 years, for principals from $100,000 through $200,000(with increments
of $20,000) and interest rates of 6% through 10% (with increments of 0.5%). Print
monthly payment as a whole number (rounded off).
The output must be a neat table, similar to the following (your answers may be slightly
different because of rounding off).
Principal 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%
100000 600 632 665 699 733 768 804 840 877
120000 719 758 798 839 880 922 965 1009 1053
140000 & & & & & & & & &
160000 & & & & & & & & &
180000 & & & & & & & & &
200000 & & & & & & & & &
Hint: Use the following idea to compute (1 + r/12)
double temp = 1+r/12;
double temp1 = 1;
for (int i = 1; i<=12*N;i++)
{
temp1 = temp1*temp;
}
The final formula now becomes:
double mp = (L*(r/12)*temp1)/(temp1-1);


 Here is what I have so far with my code:

 import java.util.*;

class mortgageCalculator;
{
public static void main(String[]args)
{
double temp = 1+r/12;
double temp1 = 1;
double mp = (L*(r/12)*temp1)/(temp1-1);
for (int i = 1; i<=12*30;i++)    
{
temp1 = temp1*temp;
}

for(temp1=.06;temp1<=.10;temp1++) // Is this the loop for the interest rate??
{
System.out.println("Monthly Payments" + mp);
}

for( int principal=100000;principal<=200000;principal++) // I am guessing there needs to be a loop to set the principal?
{

}

This table format looks like an array format. We haven't gotten into array's yet and I am pretty confused and unsure how to get started. I don't think this is supposed to be a GUI type of program. Any advice and guidance you could give me would be great. Thank you for your time.
0
Comment
Question by:Fletch3
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:aaronblum
ID: 22756790
I think that the question is simply asking you to print your output in a orderly list of columns.  If you tackle it line by line it shouldn't be too terrible.  I recommend looking at the PrintFormat java class documentation.  It may make this easier.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22757001
You will need to do some work on what you have already as well.  Here is a good starting point for you on all of it and should also cover the PrintFormat suggestion above:
http://java.sun.com/docs/books/tutorial/
0
 

Author Comment

by:Fletch3
ID: 22761550
So all I need to do is


System.out.println("Principal 6%  6.5%  7%  7.5%  8%  8.5%  9%  9.5% 10%");
System.out.println(" 100000  600 632 665 699 733 768 804 840 877");

and so on untill the table portion is complete?  I guess to calculate the monthly payments I can just use the portion that was given :

 import java.util.*;

class mortgageCalculator;
{
public static void main(String[]args)
{
double temp = 1+r/12;
double temp1 = 1;
double mp = (L*(r/12)*temp1)/(temp1-1);
for (int i = 1; i<=12*30;i++)    
{
temp1 = temp1*temp;
}

I guess I haven't declared the neccessary variables. Am I on the right track now?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Accepted Solution

by:
aaronblum earned 250 total points
ID: 22761896
Sounds like it, I would be very careful doing the calculations in the for loop you are using to print.  It may be easier to use more than one loop so you can keep better track of things.  Not sure if you have covered that yet and that is a personal opinion so choose the looping option that you are most comfortable with.

Good luck
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22764129
Correct.  I agree with aaron.  You will still need to define L and r, remembering that their are different values for L and r increasing at specified increments.
0
 

Author Comment

by:Fletch3
ID: 22765239
Yes, we are currently doing loops. I figured that maybe a loop needs to be in place to show the loan amount and the interest.  I am working on getting the variables correct but I am not sure if I am doing it right. Plus it doesn't seem to compile. This is my code so far:

import java.util.*;

class mortgageCalculator
{
public static void main(String[]args)
{      
int l;
double rate;
double interest;
double r;
double temp = 1+r/12;
double temp1 = 1;
double mp = (l*(r/12)*temp1)/(temp1-1);
temp1 = temp1*temp;

for (int i = 1; i<=12*30;i++)  

rate = r;
for (rate = .006; rate <= .10; rate++)
{
    System.out.println("Amount of Interest" + rate);
}      
dollarsLoan = l;
for (dollarsLoan = 100000; dollarsLoan <= 200000; dollarsLoan++)
{
   System.out.println("Mortgage Amount" + dollarsLoan);
}

System.out.println(" 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%");
System.out.println("Principal");
System.out.println("100000");
System.out.println("120000");
System.out.println("140000");
System.out.println("160000");
System.out.println("180000");
System.out.println("200000");

System.out.println("Monthly Payments" + mp);
}
 

}

 Am I on the right track?
 
0
 
LVL 2

Expert Comment

by:aaronblum
ID: 22766815
I would check your output errors from the compiler to see why it's not compiling.  It'll probably say something about an undefined variable.

You'll get a better idea of how tings are going if you can compile and run the code.  Make that your first priority, then have a look at your output.

It does look look like a good start though.
0
 

Author Closing Comment

by:Fletch3
ID: 31507742
The solution got me on the right track but a brief example of how to make the loop in the program what you meant would have helped me understand  a little bit better
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 34
check java version using powershell 13 180
use lov values 2 64
What is the use of Forwarding Class in java 1 16
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question