Java help

1. Apr 23, 2005

jai6638

Hey I hava a java programming assignment for my intro to java class... I've written my program but I dont know how to make the "new balance" my balance for the second time it runs and so on..

Q)Save the programs as ccard1. Take a credit card balance of \$500. Calculate the 5 months the payment based on .05 of the balance and the interest usign .15. The program should use the for loop. This program will print out in the following format 5 times:

Payment xxx
Balance xxx
Interest xxx
New Balance xxx

Program:

Code (Text):
class ccard1

{
public static void main(String[] args)  {

for (int i=1;i<=10;i++ ) {

double m=i*500
double p=m*.05;
double r=500-p;
double e=r * .15;
double d=p+e;

System.out.println("Payment     " + p +"Balance      "+ r + "Interest     " + e + "New Balance   " + d) ; }

}
}

Any help is much appreciated.. thanks

2. Apr 23, 2005

chroot

Staff Emeritus
Instead of using the literal '500' everywhere in your program, assign 500 to a variable called 'balance' at the beginning of the program. Then, at the end of the for loop, assign the balance variable with the new calculated balance.

- Warren

3. Apr 23, 2005

jai6638

like this?

Code (Text):

class ccard1

{
public static void main(String[] args)  {

for (int i=1;i<=10;i++ ) {

double o=500
double p=o*.05;
double r=o-p;
double e=r * .15;
double d=p+e;

System.out.println("Payment     " + p +"Balance      "+ r + "Interest     " + e + "New Balance   " + d) ; }

double o=d

}
}

however, i still dont understand how I can assign the new blanace to the current balance at the end of the loop.. would double o=d do the trick( i.e. im tellin it to assume that the Current balance is actually the new balanace which was assigned to D when it was first run ) ?

thanks

EDIT: tried running the program but got 7 errors :(

Last edited: Apr 23, 2005
4. Apr 24, 2005

chroot

Staff Emeritus
1) Stop naming variables with names like 'o' and 'd.' They might mean something to you, but they mean nothing to the rest of us. Please use real names, like 'balance' and 'paymentAmount' and so on.

2) Assign 500 to the balance variable at the beginning of the program, before the for loop ever runs. That's the initial balance.

3) At the bottom of the loop, assign the new balance to the balance variable. Don't make a new variable: the line "double o=d" in you program doesn't just assign d to o, it actually makes a new variable called o that exists only within that iteration of the for loop.

4) Use semicolons after statments. Spend some time learning how to interpret the error message the compiler is generating.

- Warren

5. Apr 24, 2005

so-crates

5) Declare and initialize ALL VARIABLES outside of the for loop, at the beginning of your function. This is generally a good idea.

6. Apr 24, 2005

jai6638

hows this?

Code (Text):

class vccard1
{
public static void main(String[] args)
{
double initialBalance = 500;
double balance;
double interest;
double amountDeducted;
double amount=initialBalance;
double payment;
for (int i = 1 ; i <= 5 ; i++)
{
interest=(0.15*amount);
amountDeducted=(0.05*amount);
payment=interest+amountDeducted;
balance=(amount-payment);
System.out.println("Payment: "+payment);
System.out.println("Balance: "+balance);
System.out.println("Interest: "+interest);
amount=balance;
System.out.println("New Balance: "+amount);
}

}

}

7. Apr 24, 2005

chroot

Staff Emeritus
That looks good -- does it work?

- Warren

8. Apr 24, 2005

jai6638

yup.. it works..

Thanks much for your help guys.. appreciate it :)