The Artima Developer Community
Sponsored Link

Java Answers Forum
Mortgage Homework

10 replies on 1 page. Most recent reply: Dec 11, 2009 6:16 AM by George B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
Agustin Nazario

Posts: 2
Nickname: anaza201
Registered: Feb, 2008

Mortgage Homework Posted: Feb 23, 2008 10:18 AM
Reply to this message Reply
Advertisement
I will begin by saying that I have zero programming experience but I have to take this class as part of the degree requirements. Bellow is a problem that I've been working on for several days now.

The monthly payment on mortgage loan of L dollars, at a rate of interest r is given
by
Monthly payment = [L(r/12) ]/[ - 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
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);

I don't expect people to do my homework, but I'm stuck and any help will be appreciated.
This is what I have so far and obviously is not working. Thanks in advance


public class Mortgage
{
public static void main(String args[])
{
int L;
for(L=100000;L<=200000;L+=20000)
{
double r;
for(r=6.0;r<=10.0;r+=0.5)

{
int N = 30;
double temp=1+r/12;
double temp1=1;
for(int i=1;1<=12*N;i++)
{
temp1=temp1*temp;
}
double mp=(L*(r/12)*temp1)/(temp1-1);
System.out.println(mp);

}
}
}
}


George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Mortgage Homework Posted: Feb 24, 2008 8:35 AM
Reply to this message Reply
The first problem is a typo that's causing an infinite loop. The typo is fixed in the reformatted code below, with the problem line commented out:

public class Mortgage {
    public static void main(String args[]) {
        int L;
 
        for (L = 100000; L <= 200000; L += 20000) {
            double r;
 
            for(r = 6.0; r <= 10.0; r += 0.5) {
                int N = 30;
                double temp  = 1 + r / 12;
                double temp1 = 1;
 
////            for (int i = 1; 1 <= 12 * N; i++) {
                for (int i = 1; i <= 12 * N; i++) {
                    temp1 = temp1 * temp;
                }
 
                double mp = (L * (r / 12) * temp1) / (temp1 - 1);
                System.out.println(mp);
            }
        }
    }
}
 


Hopefully this can help get you unstuck. Note that if you enclose code snippets between 'java' and '/java', replacing the single quotes with square brackets, formatting will be preserved in your postings to this forum.

Agustin Nazario

Posts: 2
Nickname: anaza201
Registered: Feb, 2008

Re: Mortgage Homework Posted: Feb 24, 2008 10:54 AM
Reply to this message Reply
Your help really pointed me in the right direction.
Thank you.

Les H

Posts: 6
Nickname: crazyman65
Registered: Dec, 2009

Re: Mortgage Homework Posted: Dec 10, 2009 10:36 AM
Reply to this message Reply
This program has been bugging me for days and started searching for help today and came across your site. I have a couple of problems that i just can't figure out.
One is how to make the output in to a table format as asked in the directions the other is when I run the code my answers are like the first one 50000 when it should be 500.
Can somebody point me in the correct direction.
Thanks

Les



class mortgage {
public static void main (String[] args)
{
int L;
for (L=100000; L<=200000; L+=20000){
double r;
for(r=6; r<=10; r+.5){
int N=30;
double temp = 1+r/12;
double temp1 = 1;

for(int i = 1; i<=12*N; i++){
temp1=temp1*temp;
}
double mp = (L * (r / 12) * temp1) / (temp1-1);
System.out.println("Principal \t 6%\t 6.5%\t 7%\t 7.5%\t 8% \t8.5% \t9% \t 9.5% \t 10%");
System.out.println("100,000 \n 120,000 \n 140,000 \n 160,000 \n 180,000 \n 200,000");
System.out.print("\t");
System.out.printf("$%1.0f\n",mp);
}
}
}
}

Les H

Posts: 6
Nickname: crazyman65
Registered: Dec, 2009

Re: Mortgage Homework Posted: Dec 10, 2009 10:56 AM
Reply to this message Reply
I figured out my math problem I still need help on figuring out how to do a table out put as stated in the orignal program.
Here is my corrected code.

Les

import static void java.lang.System.out;
class mortgage {
public static void main (String[] args)
{
int L;
for (L=100000; L<=200000; L+=20000){
     double r;
     for(r=6; r<=10; r+.5){
         int N=30;
         double temp = 1+r/12;
         double temp1 = 1;
 
         for(int i = 1; i<=12*N; i++){
         temp1=temp1*temp;
         }
          double mp = (L * (r / 12) * temp1) / (temp1-1);
          out.println("Principal \t 6%\t 6.5%\t 7%\t 7.5%\t 8% \t8.5% \t9% \t 9.5% \t 10%");
          out.println("100,000 \n 120,000 \n 140,000 \n 160,000 \n 180,000 \n 200,000");
          out.print("\t");
          out.printf("$%1.0f\n",mp);
         }
      }
   }
}

Les H

Posts: 6
Nickname: crazyman65
Registered: Dec, 2009

Re: Mortgage Homework Posted: Dec 10, 2009 10:59 AM
Reply to this message Reply
I figured out my math problem I still need help on figuring out how to do a table out put as stated in the orignal program.
Here is my corrected code.

Les
class mortgage {
public static void main (String[] args)
{
int L;
for (L=100000; L<=200000; L+=20000){
     double r;
     for(r=.06; r<=.10; r+.005){
         int N=30;
         double temp = 1+r/12;
         double temp1 = 1;
 
         for(int i = 1; i<=12*N; i++){
         temp1=temp1*temp;
         }
          double mp = (L * (r / 12) * temp1) / (temp1-1);
          System.out.println("Principal \t 6%\t 6.5%\t 7%\t 7.5%\t 8% \t8.5% \t9% \t 9.5% \t 10%");
          System.out.println("100,000 \n 120,000 \n 140,000 \n 160,000 \n 180,000 \n 200,000");
          System.out.print("\t");
          System.out.printf("$%1.0f\n",mp);
         }
      }
   }
}

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Mortgage Homework Posted: Dec 10, 2009 11:51 AM
Reply to this message Reply
I'm not sure what you're trying to accomplish with some of the math using temp and temp1 and N, but this will print your results as a table.

class mortgage {
    public static void main (String[] args) {
        int principal;
        double rate;
 
        //
        // print table header
        //
        System.out.print("Principal \t");
        for (rate = .06; rate <= .10; rate += .005) {
            System.out.printf("%.1f\t", rate * 100);
        }
        System.out.println("");
 
        //
        // calculate and print table body
        //
        for (principal = 100000; principal <= 200000; principal += 20000) {
            System.out.print("" + principal + "    \t");
 
            for (rate = .06; rate <= .10; rate += .005) {
                int N = 30;
                double temp = 1 + rate/12;
                double temp1 = 1;
 
                for (int i = 1; i <= 12 * N; i++) {
                    temp1 = temp1 * temp;
                }
                double mp = (principal * (rate / 12) * temp1) / (temp1-1);
                System.out.printf("$%1.0f\t", mp);
            }
            System.out.print("\n");
        }
    }
}

Les H

Posts: 6
Nickname: crazyman65
Registered: Dec, 2009

Re: Mortgage Homework Posted: Dec 10, 2009 2:19 PM
Reply to this message Reply
George B,
Thank you so very much.
Now I realized I still messed up on the math.
I need it to show out to 10% and i currently only make it to 9.5.
Any ideas???

Thanks
Les

Les H

Posts: 6
Nickname: crazyman65
Registered: Dec, 2009

Re: Mortgage Homework Posted: Dec 10, 2009 3:52 PM
Reply to this message Reply
I figured it out.

Les H

Posts: 6
Nickname: crazyman65
Registered: Dec, 2009

Re: Mortgage Homework Posted: Dec 10, 2009 6:52 PM
Reply to this message Reply
Here is what I get when I run the code:
--------------------Configuration: <Default>--------------------
Principal 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
$100000 $600 $632 $665 $699 $734 $769 $805 $841 $878
$120000 $719 $758 $798 $839 $881 $923 $966 $1009 $1053
$140000 $839 $885 $931 $979 $1027 $1076 $1126 $1177 $1229

What I need to figure out is to have it show this:
--------------------Configuration: <Default>--------------------
Principal 6.0% 6.5% 7.0% 7.5% 8.0% 8.5% 9.0% 9.5% 10.0%
$100000 $600 $632 $665 $699 $734 $769 $805 $841 $878
$120000 $719 $758 $798 $839 $881 $923 $966 $1009 $1053
$140000 $839 $885 $931 $979 $1027 $1076 $1126 $1177 $1229

I thought I could add another % in the second print line it compiles ok but when I run the code it errors out.
Any ideas??

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Mortgage Homework Posted: Dec 11, 2009 6:16 AM
Reply to this message Reply
In a printf statement, a percent sign is used to mark the beginning of a placeholder that indicates how a value is to be formatted, e.g. "%d" means it should be formatted as a decimal number or "%f" means it should be a floating point number. If you just put in a "%", the library errors out because it finds a tab ("\t") as the next character and it doesn't know how to format "%\t". To insert a percent sign use "%%".

You can see all (well, most anyway) of the formatting options on the api page for the java Formatter class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html

Flat View: This topic has 10 replies on 1 page
Topic: querying an Oracle database using Java Previous Topic   Next Topic Topic: New to Java?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use