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

Join 150,184 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,109 people online right now. Registration is fast and FREE... Join Now!




Program works..need adjustments

 
Reply to this topicStart new topic

Program works..need adjustments, The output pauses after the first payment. It then list x many times a

jgear
23 Aug, 2008 - 05:40 PM
Post #1

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 47

CODE

import java.text.DecimalFormat;

public class mortgage1
{
    public static void main (String[] args)
    {
        //Declare and construct variables
        DecimalFormat decimalPlaces = new DecimalFormat(".00");
        double    monthlypayments, principle,interest,
                interestAmount, payment;

        int    amount, i, paymentsPerPage, lengthOfPause;


        /* Below is the hard coded values used in the formula
           for calculating the monthly mortgage payment amount
        */
        interest = 0.0575;       //Interest rate
        principle = 200000;      //Amount borrowed
        monthlypayments = 360;   //Number of monthly payments

        //This is the formula used to calculate the monthly payment
        payment = principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0)),-monthlypayments) ));

        //Output to screen
            System.out.println("Principle = $"+decimalPlaces.format(principle));               //The original amount borrowed
            System.out.println("Interest Rate ="+interest*100 +"%");                           //The interest rate
            System.out.println("Monthly Payments = $"+decimalPlaces.format(monthlypayments));  //The number of payments
            System.out.print("Payment per Month = $");                                         //The monthly payment amount
            System.out.println(decimalPlaces.format(payment));                                 //formats the payment amount to two decimals
            System.out.println("**************************");
            System.out.println("**************************");



        lengthOfPause = 5; //The amount of time (in seconds) to pause the page
        paymentsPerPage = 20; //The number of payments to view per page
        i = 1;



        for(i = 1; i <= 360; i++)
        {
            System.out.println("Payment " + i + ":");
            System.out.println("----------");

            System.out.println("Payment Amount:   $ " + decimalPlaces.format(payment));
            interestAmount = ((interest / 12) * principle);
            System.out.println("Interest Amount:   $ " + decimalPlaces.format(interestAmount));

            // You also have to calculate interest and add that back in.
            principle = (principle - payment) + interestAmount;
            System.out.println("Remaining Balance: $ " + decimalPlaces.format(principle));
            System.out.println("----------");
            System.out.println("");
            if(((i - 1) % paymentsPerPage) == 0)
            {
                try{Thread.sleep(lengthOfPause * 1000);}  //This is a pause function.
                catch(InterruptedException ie){}
            }
        }
    }
}

Edited to add the [ code] tags

This post has been edited by pbl: 23 Aug, 2008 - 06:11 PM
User is offlineProfile CardPM
+Quote Post

thenovices
RE: Program Works..need Adjustments
23 Aug, 2008 - 07:51 PM
Post #2

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
could you please list your problem in the actual post, not in the thread title?
User is offlineProfile CardPM
+Quote Post

jgear
RE: Program Works..need Adjustments
24 Aug, 2008 - 03:41 AM
Post #3

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 47

QUOTE(thenovices @ 23 Aug, 2008 - 08:51 PM) *

could you please list your problem in the actual post, not in the thread title?


I apologize. When I run the program the first payment made is displayed. Then the next 20 are displayed and so on. I can not seem to get the first 20 to display at the same time. I do not think it is a major issue but it is driving me crazy.

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Program Works..need Adjustments
24 Aug, 2008 - 04:18 AM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
This is what causes the problem:

if(((i - 1) % paymentsPerPage) == 0)

Since your for-loop (earlier in the code) starts at 1 through 360, after displaying the first payment, consisting of 7 System.out.println's, you have i = 1 and that if-statement will evaluate to true so the thread.sleep() method is executed, followed by another 20 payments.
(if i = 1 then i-1 = 0. And 0 % x = 0 always!)

Simply remove the (-1) from the if-statement:

if((i % paymentsPerPage) == 0)

It will take care of your problem.

This post has been edited by Gloin: 24 Aug, 2008 - 04:21 AM
User is offlineProfile CardPM
+Quote Post

jgear
RE: Program Works..need Adjustments
24 Aug, 2008 - 04:50 AM
Post #5

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 47

QUOTE(Gloin @ 24 Aug, 2008 - 05:18 AM) *

This is what causes the problem:

if(((i - 1) % paymentsPerPage) == 0)

Since your for-loop (earlier in the code) starts at 1 through 360, after displaying the first payment, consisting of 7 System.out.println's, you have i = 1 and that if-statement will evaluate to true so the thread.sleep() method is executed, followed by another 20 payments.
(if i = 1 then i-1 = 0. And 0 % x = 0 always!)

Simply remove the (-1) from the if-statement:

if((i % paymentsPerPage) == 0)

It will take care of your problem.



Thanks a lot. It works perfect!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:00AM

Be Social

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

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month