Section 4.7. Example Program: Debt Calculator

   

4.7 Example Program: Debt Calculator

While the checkerboard program is a hoot, it is not necessarily very useful. Let's write another program that is a bit more realistic, again with the aim of tying together many of the concepts we have learned thus far. The program we will write is a debt calculator. It will take a few variables , such as the amount of debt you've got, your income, and the interest rate, and the program will figure out how many years it will take you to pay it off. In so doing, it makes use of different variable types (including constants and typecasting), logical and mathematical operations and varying notation, conditional logic, and loops .

Here's a more specific overview. The purpose of the program is to calculate the number of years it will take to pay off a debt with accruing interest. There are a few variables required to perform this calculation: debt, income, expenses, and the interest rate. These are int s, except for the interest rate, which is a final double .

By subtracting expenses from income, we get an amount available to pay toward the debt ( savings ). The interest is added to the amount of debt and a while loop is set up to determine the number of years required to pay off that debt with that amount of savings at that interest rate. The interest compounds while the savings never gets any bigger, which creates a variety of possible payment scenarios. There are certain cases in which the debt will effectively never get paid off; in these cases, the program informs the user that they may be enjoying themselves , but they can never pay it off. In other cases, the debt could conceivably get paid off, but it might take longer than 50 years to do so. In these situations it is possible that, given the human life span, the user may die before paying off the debt completely. So we notify the user of this possibility after showing the effect of the first 50 years of payment.

Here is the code:

4.7.1 DebtApp.java

 /*  File: DebtApp.java Purpose: demonstrates a semi-realistic debt calculating program Author: E Hewitt */ public class DebtApp {     public static void main(String[] args){     // declare vars     int income, expenses, debt, interest, savings, payment;     byte years;     // initialize vars     income = 115000;     expenses = 82000;     debt = 149000;     // interest rate won't change in here     final double INTEREST_RATE = 7.25;     // this will hold the current year     years = 1;     // we'll put toward the debt what we have     // left over from salary after expenses are paid     savings = income - expenses;     payment = savings; if (expenses >= income        ((INTEREST_RATE / 100 * debt) > (income - expenses))) {     System.out.println("You are living the life. But...");     System.out.println("Your debt will never be paid off."); } else {         System.out.println("You started $" + debt +" in debt");         System.out.println("You pay $" + payment + " against the debt each year"); // make a new line spacer just for readability         System.out.println(); // as long as there is more debt, calculate what // remains to be paid while ( debt > payment) { // cast the interest to an int so we can work with it // more easily, and pennies don't matter    interest = (int) ((INTEREST_RATE / 100) * debt); // debt is debt + interest    debt += interest;    System.out.println("You are paying $" + interest + " in interest alone.");    System.out.println("Total debt including interest: $" + debt); // debt is debt - this payment    debt -= payment;    System.out.println("You still owe $" + debt +" in year "+ years); // spacer System.out.println(); // tell them if it is just fruitless     if (years >= 50) {        System.out.println("Your payments can't keep up with your interest.");         System.out.println("At this rate, it will probably take longer than you've got.");             break;             }     // increment years     ++years;          }       }    } } 

Here is the output with these particular numbers :

 C:\jdk1.4\JavaForCF\chp4>javac DebtApp.java  C:\jdk1.4\JavaForCF\chp4>java DebtApp You started 9000 in debt You pay 000 against the debt each year You are paying 802 in interest Total debt including interest: 9802 You still owe 6802 in year 1 You are paying 93 in interest Total debt including interest: 5995 You still owe 2995 in year 2 You are paying 67 in interest Total debt including interest: 0462 You still owe 462 in year 3 You are paying 15 in interest Total debt including interest: 077 You still owe 077 in year 4 You are paying 30 in interest Total debt including interest: 707 You still owe 707 in year 5 

That is the end of the output, as the user's payments can now overcome the debt and pay it off.

Let's take a moment to look at some of the more interesting aspects of the program. The (int) cast will automatically lose the decimal places introduced by the interest double . That is just fine here, because we don't want to deal with pennies, and it will make our output more attractive and readable. It is important to remember that the cast is necessary because the constant INTEREST_RATE is a double , and the compiler will worry that you will lose precision you might need without a cast ”both interest and debt are int s.

You may find it fruitful to work with this program a bit. Try modifying it in different ways to get used to how things work. For instance, you could add a savings account into the mix that also grows interest, which helps reduce the debt more quickly. You could modify the program to accept values such as the current debt and salary as arguments from the command line. Then, if the program receives no arguments, print out a help menu (the set of arguments the program accepts). To get an idea of what a help menu looks like, try typing javac at the command line. javac prints a list of argument options.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net