Having some trouble with some algorithms

hey guys im having some trouble trying to figure out some algorithms for this one program. the program is a credit plan for purchases. It is suppose to take in the purchase price of a computer and then output in a textArea a table including the month, current balance of that month, interest charged, principal, the monthly payment due, and the user's new balance of that month. there is a 10 percent down payment and an annual interest rate of 12 percent( i was thinking once month=12 i could somehow make the interest rate 12 percent havent figured this out yet though). It is suppose to keep on doing this until the balance of the month is 0. I have a do while loop for this and figured out some algorithms for this program. but still im not

[code]

import javax.swing.*;

import BreezySwing.*;

public class GUICreditPlan extends GBFrame {

private JLabel purchasePriceLabel;

private DoubleField purchasePriceField;

private JButton calculateButton;

private JTextArea output;

private double purchasePrice;

private double downPayment;

private double currentBalance;

private int iterations = 1;

private int month;

private double interest = 0;

private double principal;

private double paymentDue;

private double newBalance;

public GUICreditPlan()

{

String header = Format.justify('l',"Month", 7) +

Format.justify('r',"Current Balance", 16) +

Format.justify('r',"Interest", 10) +

Format.justify('r',"Principal", 11) +

Format.justify('r',"Payment Due", 14) +

Format.justify('r',"New Balance", 14) + "\n";

purchasePriceLabel = addLabel ("Purchase Price", 1,1,1,1);

purchasePriceField = addDoubleField(0.0, 1,2,1,1);

calculateButton = addButton ("Calculate", 2,1,2,1);

output = addTextArea (header , 3,1,5,6);

output.setEnabled(false);

}

public void buttonClicked(JButton buttonObj)

{

purchasePrice = purchasePriceField.getNumber();

downPayment = (purchasePrice * .1);

currentBalance = purchasePrice - downPayment;

do

{

paymentDue = ((purchasePrice - downPayment) * .05);

interest = currentBalance * .01;

principal = currentBalance - (paymentDue - interest);

newBalance = (currentBalance + interest) - paymentDue;

month = iterations;

displayNumbers (month, currentBalance, interest, principal, paymentDue, newBalance);

currentBalance = (currentBalance + interest) - paymentDue;

iterations++;

}

while(currentBalance>0);

}

private void displayNumbers(int month, double currentBalance, double interest, double principal, double paymentDue,double newBalance)

{

String numberLine = Format.justify('l',month, 7) +

Format.justify('r',currentBalance, 16,2) +

Format.justify('r',interest, 10,2) +

Format.justify('r',principal, 11,2) +

Format.justify('r',paymentDue, 14,2) +

Format.justify('r',newBalance, 14,2) + "\n";

output.append (numberLine + "\n");

}

public static void main(String[] args)

{

GUICreditPlan theGUI = new GUICreditPlan();

theGUI.setSize(600,300);

theGUI.setTitle("Credit Plan for the TidBit Computer Store");

theGUI.setVisible(true);

}

}

/code]

thanks for the help

[3423 byte] By [cupOfJava2008a] at [2007-11-27 6:14:33]
# 1
So what's your exact problem? Be specific.When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
jverda at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 2

when the purchase price is entered all the ouput is correct now except i have two problems.

one like i said above is trying to figure out how to charge an interest of 12 percent anually the other is when the program runs through it doesnt finish when newBalance is zero it ends in the negatives

import javax.swing.*;

import BreezySwing.*;

public class GUICreditPlan extends GBFrame {

private JLabel purchasePriceLabel;

private DoubleField purchasePriceField;

private JButton calculateButton;

private JTextArea output;

private double purchasePrice;

private double downPayment;

private double currentBalance;

private int iterations = 1;

private int month;

private double interest = 0;

private double principal;

private double paymentDue;

private double newBalance;

public GUICreditPlan()

{

String header = Format.justify('l',"Month", 7) +

Format.justify('r',"Current Balance", 16) +

Format.justify('r',"Interest", 10) +

Format.justify('r',"Principal", 11) +

Format.justify('r',"Payment Due", 14) +

Format.justify('r',"New Balance", 14) + "\n";

purchasePriceLabel = addLabel ("Purchase Price", 1,1,1,1);

purchasePriceField = addDoubleField(0.0, 1,2,1,1);

calculateButton = addButton ("Calculate", 2,1,2,1);

output = addTextArea (header , 3,1,5,6);

output.setEnabled(false);

}

public void buttonClicked(JButton buttonObj)

{

purchasePrice = purchasePriceField.getNumber();

downPayment = (purchasePrice * .1);

currentBalance = purchasePrice - downPayment;

do

{

paymentDue = ((purchasePrice - downPayment) * .05);

interest = currentBalance * .01;

principal = currentBalance - (paymentDue - interest);

newBalance = (currentBalance + interest) - paymentDue;

month = iterations;

displayNumbers (month, currentBalance, interest, principal, paymentDue, newBalance);

currentBalance = (currentBalance + interest) - paymentDue;

iterations++;

}

while(currentBalance>0);

}

private void displayNumbers(int month, double currentBalance, double interest, double principal, double paymentDue,double newBalance)

{

String numberLine = Format.justify('l',month, 7) +

Format.justify('r',currentBalance, 16,2) +

Format.justify('r',interest, 10,2) +

Format.justify('r',principal, 11,2) +

Format.justify('r',paymentDue, 14,2) +

Format.justify('r',newBalance, 14,2) + "\n";

output.append (numberLine + "\n");

}

public static void main(String[] args)

{

GUICreditPlan theGUI = new GUICreditPlan();

theGUI.setSize(600,300);

theGUI.setTitle("Credit Plan for the TidBit Computer Store");

theGUI.setVisible(true);

}

}

thanks for the help

cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 3
[url= http://en.wikipedia.org/wiki/Compound_interest][/url]
petes1234a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 4
thanks that gives the algorithms i think i have them now but the problems im having are with the 12 percent charged each year and the output not stopping when the balance is zero
cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 5
please help guys
cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 6

> thanks that gives the algorithms i think i have them

> now but the problems im having are with the 12

> percent charged each year

I think that 12% a year compounded monthly is simply equivalent to 1% a month. The true yearly interest will be different due to compounding.

> and the output not stopping

> when the balance is zero

It is not set to stop when balance == 0, it is set to stop when this (balance > 0) is false, and that's probably as it should be. Since you are using double, if you try to end when this (balance != 0) is false, you'll likely go on forever. My guess is that the end balance will be negative, but it should be a very small number. How big is it?

petes1234a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 7

heres the ouput for a purchase price of 10000 and thanks for the help

MonthCurrent Balance Interest PrincipalPayment DueNew Balance

19000.0090.008640.00450.008640.00

28640.0086.408276.40450.008276.40

38276.4082.767909.16450.007909.16

47909.1679.097538.26450.007538.26

57538.2675.387163.64450.007163.64

67163.6471.646785.27450.006785.27

76785.2767.856403.13450.006403.13

86403.1364.036017.16450.006017.16

96017.1660.175627.33450.005627.33

10 5627.3356.275233.60450.005233.60

11 5233.6052.344835.94450.004835.94

12 4835.9448.364434.30450.004434.30

13 4434.3044.344028.64450.004028.64

14 4028.6440.293618.93450.003618.93

15 3618.9336.193205.12450.003205.12

16 3205.1232.052787.17450.002787.17

17 2787.1727.872365.04450.002365.04

18 2365.0423.651938.69450.001938.69

19 1938.6919.391508.08450.001508.08

20 1508.0815.081073.16450.001073.16

21 1073.1610.73633.89450.00633.89

22633.896.34190.23450.00190.23

23190.231.90-257.87450.00-257.87

cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 8
would this work for the annual interest percent charge?if (month % 12 ==0){interest = currentBalance * .12;}else {interest = currentBalance * .01;}
cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 9
help
cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 10

> would this work for the annual interest percent

> charge?

>

> if (month % 12 ==0)

>{

> interest = currentBalance * .12;

>}

>

>else

> {

>interest = currentBalance * .01;

> }

Can you explain what that's supposed to mean? Why are you doing that?

jverda at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 11
because the company is suppose to charge 12 percent interest annually
cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 12
> because the company is suppose to charge 12 percent> interest annuallyThat doesn't answer my question.Explain what the code means.
jverda at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 13

> > would this work for the annual interest percent

> > charge?

> >

> > if (month % 12 ==0)

> >{

> > interest = currentBalance * .12;

> >}

> >

> >else

> > {

> >interest = currentBalance * .01;

> > }

>

>

> Can you explain what that's supposed to mean? Why are

> you doing that?

If the balance is $100, and it's January, what will interest be?

Feb?

Mar?

...

Dec?

jverda at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 14
oh well once the month modulus 12 equals zero ( which is every twelve months = a year) the interest will be changed from .1 to .12 percent. not sure if this would work though
cupOfJava2008a at 2007-7-12 17:24:09 > top of Java-index,Java Essentials,Java Programming...
# 15

> oh well once the month modulus 12 equals zero ( which

> is every twelve months = a year) the interest will be

> changed from .1 to .12 percent. not sure if this

> would work though

I can see that. It doesn't explain what it means or its purpose or the thinking behind it.

jverda at 2007-7-21 21:48:22 > top of Java-index,Java Essentials,Java Programming...
# 16

there is a 10 percent down payment , an annual interest rate of 12 percent, and monthly payments are 5 percent of the listed purchase price, the amount of interest for a month is equal to balance * rate /12, the amount of principal for a month is equal to the monthly payment minus the interest owed

cupOfJava2008a at 2007-7-21 21:48:22 > top of Java-index,Java Essentials,Java Programming...
# 17
Have a look at [url= http://rds.yahoo.com/_ylt=A0geu5KQ82JGbUEABLJXNyoA;_ylu=X3oDMTB2bWJzNWI1BHNlYwNzcgRwb3MDMQRjb2xvA2UEdnRpZAMEbANXUzE-/SIG=12pusq5re/EXP=1180976400/**http%3a//www.eg.bucknell.edu/~csci203/2007-spring/projects/project3.pdf]this[/url].
petes1234a at 2007-7-21 21:48:22 > top of Java-index,Java Essentials,Java Programming...
# 18

If you do your calcs correctly, you should only have a trivial positive or negative balance at the end. I was able to avoid negative results by using a constant, I called "DELTA", that allows for the trivial positive balances.

Something like this worked

private static final double DELTA = 0.001;

.....

.....

private void runCalculations()

{

getStartingData();

while (newBalance > 0 + DELTA) // the 0 is redundant but left in for clarity

{

doCalc();

showResults();

}

}

Oh yeah, you probably should test newBalance in your while loop, not currentBalance. When newBalance is 0, that's when you quit the loop.

Message was edited by:

petes1234

petes1234a at 2007-7-21 21:48:22 > top of Java-index,Java Essentials,Java Programming...
# 19
thanks pete that was lots of help
cupOfJava2008a at 2007-7-21 21:48:22 > top of Java-index,Java Essentials,Java Programming...