Solved

Problem with loop initialization

Posted on 2004-09-14
6
237 Views
Last Modified: 2010-03-31
I am almost finished with my Java homework, the 'classic' mortgage amortization.  It runs fairly well except I can't figure out how to stop the first month from displaying all zeros.  It should be starting with the data displayed in week 2...

What am I doing wrong?

//Authors Name:  Tom Pitts
//Assignment:    POS 406: Workshop 3              
//Creation Date: September 11, 2004
//Due Date:      September 15, 2004

import java.lang.Math;                  //Import math class
import java.text.DecimalFormat;      //Import decimal formatting class

 class POS406_W3_TRP { //Declare Class
     public static void main(String[] arguments) {
            double rate = .0575;  //Interest rate in decimal format
            double principle = 200000;  //Amount of loan
            double term = 360;      //Term of loan in months
            double monthlyRate = (rate/12);     //Monthly interest rate = .0575/12

            //Loan Overview Output
            System.out.println("Name:         Tom Pitts");
            System.out.println("Assignment:   Workshop 2");
            System.out.println("________________________ \n");
            System.out.println("Principle       : $" + principle);
            System.out.println("Interest Rate   : " + rate * 100 + "%");
            System.out.println("# of Payments   : " + term);

            /*Mortgage Payment Formula provided by Instructor is:
            Principal * rate / ( 1 - (1 + rate)^ -number of payments )*/

            double payments = (principle * monthlyRate) /  (1- Math.pow((1 + monthlyRate), -term));
            DecimalFormat myVar = new DecimalFormat("$###,##0.00");
            String myoutput = myVar.format(payments);
            System.out.println("Monthly Payment : " + myoutput + "\n");
   
      try
      {System.out.println("Press Enter to Continue");
      System.in.read();
      System.in.read();
      }
      catch(Exception error)
      {}
      
            System.out.print("\tPrincipal\tInterest\tLoan\n"); //Amortization Headings
            System.out.println("Months\tPayment\t\tPayment\t\tBalance\n");//Amortization Headings
            int monthCounter=1;  
            double loanBalance=0.0;
            double interestPaid=0.0;
            double principlePayment=0.0;

                  for (;monthCounter <= 360; monthCounter++) { //Intrement monthCounter by 1 each loop
            DecimalFormat balanceVar = new DecimalFormat("$###,##0.00");
            String decimal_Loan_Balance = balanceVar.format(loanBalance);
            String interestPaid_output = balanceVar.format(interestPaid);
            String principlePayment_output = balanceVar.format(principlePayment);

                  if(monthCounter ==1){
                  interestPaid= principle * monthlyRate;
                  loanBalance= principle - (payments - interestPaid);
                                                }
                  else{
                  interestPaid= loanBalance * monthlyRate;
                  loanBalance= loanBalance - (payments - interestPaid);
                                                }
            
            principlePayment = payments - interestPaid;

            System.out.println(monthCounter + "\t" + principlePayment_output + "\t\t"+ interestPaid_output + "\t\t" + decimal_Loan_Balance);

            if(monthCounter % 12==0)

      try
      {System.out.print("Press Enter to Continue");
      System.in.read();
      System.in.read();
      }
      catch(Exception error)
      {}
      }
 }
 }
0
Comment
Question by:nihlcat
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
It should be something like this

//Authors Name:  Tom Pitts
//Assignment:    POS 406: Workshop 3              
//Creation Date: September 11, 2004
//Due Date:      September 15, 2004

import java.lang.Math;               //Import math class
import java.text.DecimalFormat;     //Import decimal formatting class

 class POS406_W3_TRP { //Declare Class
     public static void main(String[] arguments) {
          double rate = .0575;  //Interest rate in decimal format
          double principle = 200000;  //Amount of loan
          double term = 360;     //Term of loan in months
          double monthlyRate = (rate/12);     //Monthly interest rate = .0575/12

          //Loan Overview Output
          System.out.println("Name:         Tom Pitts");
          System.out.println("Assignment:   Workshop 2");
          System.out.println("________________________ \n");
          System.out.println("Principle       : $" + principle);
          System.out.println("Interest Rate   : " + rate * 100 + "%");
          System.out.println("# of Payments   : " + term);

          /*Mortgage Payment Formula provided by Instructor is:
          Principal * rate / ( 1 - (1 + rate)^ -number of payments )*/

          double payments = (principle * monthlyRate) /  (1- Math.pow((1 + monthlyRate), -term));
          DecimalFormat myVar = new DecimalFormat("$###,##0.00");
          String myoutput = myVar.format(payments);
          System.out.println("Monthly Payment : " + myoutput + "\n");
   
     try
     {System.out.println("Press Enter to Continue");
     System.in.read();
     System.in.read();
     }
     catch(Exception error)
     {}
     
          System.out.print("\tPrincipal\tInterest\tLoan\n"); //Amortization Headings
          System.out.println("Months\tPayment\t\tPayment\t\tBalance\n");//Amortization Headings
          int monthCounter=1;  
          double loanBalance=0.0;
          double interestPaid=0.0;
          double principlePayment=0.0;

               for (;monthCounter <= 360; monthCounter++) { //Intrement monthCounter by 1 each loop
          DecimalFormat balanceVar = new DecimalFormat("$###,##0.00");
          String decimal_Loan_Balance = balanceVar.format(loanBalance);
          String interestPaid_output = balanceVar.format(interestPaid);
          String principlePayment_output = balanceVar.format(principlePayment);

               if(monthCounter ==1){
               interestPaid= principle * monthlyRate;
               loanBalance= principle - (payments - interestPaid);
                                        }
               else{
               interestPaid= loanBalance * monthlyRate;
               loanBalance= loanBalance - (payments - interestPaid);
                                        }
         
          principlePayment = payments - interestPaid;
      if(monthCounter >1)
          System.out.println((monthCounter-1) + "\t" + principlePayment_output + "\t\t"+ interestPaid_output + "\t\t" + decimal_Loan_Balance);

          if((monthCounter-1) % 12==0)

     try
     {System.out.print("Press Enter to Continue");
     System.in.read();
     System.in.read();
     }
     catch(Exception error)
     {}
     }
 }
 }
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 250 total points
Comment Utility
Small correction

>>for (;monthCounter <= 360; monthCounter++) { //Intrement monthCounter by 1 each loop

for (;monthCounter <= 361; monthCounter++) { //Intrement monthCounter by 1 each loop

0
 
LVL 7

Expert Comment

by:bvanderveen
Comment Utility
Look at where you format interestPaid_output and principlePayment_output - before you compute the source values.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:bvanderveen
Comment Utility
Just to be clear - step through with Eclipse debugger.  You are computing the payment and interest correctly, but you have already computed principlePayment_output and interestPaid_output from the values of interest and principle as they were initialized (zero).  Just need to move a few lines...

I wish other homework questions were like this - honest, and looking for debugging help, rather than "do it for me".
0
 
LVL 7

Expert Comment

by:bvanderveen
Comment Utility
Sudhakar's solution works, but I think it is a kludge.  Instead of printing out.

   System.out.println((monthCounter-1) + "\t" + principlePayment_output + "\t\t"+ interestPaid_output + "\t\t" + decimal_Loan_Balance);

You should move the assignments for those variables after interestPaid and loan_balance have been computed.  Then your program will be more "rational".  

Follow the logic - your teacher should give some points off for this crude workaround - it would be harder to maintain and understand.
0
 
LVL 6

Author Comment

by:nihlcat
Comment Utility
Ok, understood.  I haven't turned it in yet, so I will review this.  I wanted to clean up the code anyway.  THANK YOU for your opinions!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
firstChar challenge 13 83
get weblogic logged in user in java 2 39
Java Server Faces parameter pass? 6 18
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now