Java Java Credit Card Payment Calculator with For Loop

  • Thread starter Thread starter jai6638
  • Start date Start date
  • Tags Tags
    Calculation Java
AI Thread Summary
The discussion revolves around a Java programming assignment focused on calculating credit card payments, balances, and interest over five months using a for loop. The initial problem was how to update the balance for subsequent iterations. Participants suggested assigning the initial balance of $500 to a variable named 'balance' at the start of the program. They emphasized the importance of using meaningful variable names instead of generic ones like 'o' and 'd'. The corrected program structure involves declaring all variables outside the for loop, calculating the payment, interest, and new balance within the loop, and then updating the balance for the next iteration. The final code provided successfully implements these suggestions, resulting in a functioning program that meets the assignment requirements. The user expressed gratitude for the assistance received in resolving the issues.
jai6638
Messages
263
Reaction score
0
Hey I hava a java programming assignment for my intro to java class... I've written my program but I don't 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:
 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
 
Technology news on Phys.org
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
 
like this?

Code:
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 don't 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. I am 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:
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 statements. Spend some time learning how to interpret the error message the compiler is generating.

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

Code:
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);
         }

     }

}
 
That looks good -- does it work?

- Warren
 
yup.. it works..

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