Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,160 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,507 people online right now. Registration is fast and FREE... Join Now!




Amortization program help...

 

Amortization program help...

confused.help

17 Jul, 2008 - 11:35 PM
Post #1

New D.I.C Head
*

Joined: 13 Jul, 2008
Posts: 8

I'm trying to write this program to display an amortization schedule.... I am nearly finished, but my final principal is coming up negative, and I've tried everything I can think of to fix this but nothing is working... any ideas?? Thanks..
CODE

import java.util.Scanner;
import java.text.DecimalFormat;

public class Mortgage
{
      
    public static void main (String args[])
    {  
        Scanner input = new Scanner(System.in);
  
        int choice;
        double principal;
        double yearlyInterest;
        int paybackYears;
        double monthlyPmt;

        System.out.println("Select one of the following options");
        System.out.println();
        System.out.println("[1]   Monthly Loan Payment Computation");
        System.out.println("[2]   Amortization Schedule");
        System.out.println("[3]   Credit Card Pay-Off Schedule");
        System.out.println();
        System.out.print("Enter your choice  ===>>  ");
        choice = input.nextInt();
        System.out.println();

        switch (choice)
        {
            case 1:
                System.out.print("Enter loan amount       ===>>  ");
                principal = input.nextDouble();
                System.out.print("Enter annual percent    ===>>  ");
                yearlyInterest = input.nextDouble();
                System.out.print("Enter years to pay back ===>>  ");
                paybackYears = input.nextInt();
                Interest loan1 = new Interest(principal, yearlyInterest, paybackYears,0);
                loan1.computePayment();
                break;
            case 2:
                System.out.print("Enter principle balance  ===>>  ");
                principal = input.nextDouble();
                System.out.print("Enter annual percent     ===>>  ");
                yearlyInterest = input.nextDouble();
                System.out.print("Enter monthly payment    ===>>  ");
                monthlyPmt = input.nextDouble();
                Interest loan2 = new Interest(principal, yearlyInterest, 0, monthlyPmt);
                loan2.amortization();
                break;
                    }
        System.out.println();
    }
    
}



class Interest
{

    private double principal;
    private double percent;
    private int years;
    private double monthlyPmt;
    private int months;
    private int pmtNr;
    private double monthlyRate;
    private double interestPmt;
    private double principalPmt;
    private double totalPmt;
    private double totalInt;

    public Interest(double la, double yi, int py, double mp)
    // Do not alter anything in the constructor!
    {
        principal = la;
        percent = yi;
        years = py;
        monthlyPmt = mp;
        monthlyRate = percent/1200;
        months = years * 12;
        pmtNr = 0;
    }


    private double round(double x)
    {
        return (double) Math.round(100 * x) / 100;
    }
    
    
    public void computePayment()
    {
        System.out.println();
        System.out.println("Monthly Loan Payment Computation");
        System.out.println();
        
        DecimalFormat output = new DecimalFormat("##.0");
        monthlyPmt = (((monthlyRate)* Math.pow(1+monthlyRate,months))/(Math.pow(1+monthlyRate,months)-1))*principal;
        monthlyPmt = round(monthlyPmt);
        totalPmt =(months*monthlyPmt);
        totalInt = totalPmt - principal;
        
        System.out.println("Loan Amount:     " + principal);
        System.out.println("Yearly Interest: " + percent);
        System.out.println("Payback Years:   " + years);
        System.out.println("Monthly Payment: " + monthlyPmt);
        System.out.println("Total Payments:  " + output.format(totalPmt));
        System.out.println("Total Interest:  " + output.format(totalInt));
    }
//This is where I'm having trouble....
    public void amortization()
    {
        System.out.println();
        System.out.println("Amortization Schedule");
        System.out.println();
        System.out.println("Loan Amount:     " + principal);
        System.out.println("Yearly Interest: " + percent);
        System.out.println("Montly Payment:  " + monthlyPmt);
        double Loan = principal;
        

        do
        {
            interestPmt = round(principal*monthlyRate);
            principalPmt = round(monthlyPmt - interestPmt);
            principal = round(principal - principalPmt);
            
            
            pmtNr ++;
            

            System.out.println(pmtNr+"    \t"+monthlyPmt+"    \t"+interestPmt+"    \t"+principalPmt+"    \t"+principal);
        }
        while (principal > 0);
        
        totalPmt = pmtNr * monthlyPmt;
        totalInt = round(totalPmt - Loan);
    
    
        System.out.println("Payback Months:  " + pmtNr);  
        System.out.println("Total Payments:  " + totalPmt);
        System.out.println("Total Interest:  " + totalInt);
    }


User is offlineProfile CardPM
+Quote Post


Posts in this topic

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:59PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month