Java Credit Card Payment Calculator with For Loop

  • Context: Java 
  • Thread starter Thread starter jai6638
  • Start date Start date
  • Tags Tags
    Calculation Java
Click For Summary

Discussion Overview

The discussion revolves around a Java programming assignment focused on creating a credit card payment calculator using a for loop. Participants explore how to correctly calculate and update the balance over multiple iterations while incorporating interest and payment amounts.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks help on how to update the balance for subsequent iterations of the loop after calculating payments and interest.
  • Another participant suggests assigning the initial balance to a variable and updating this variable with the new balance at the end of each loop iteration.
  • A later reply emphasizes the importance of using meaningful variable names instead of single letters, and highlights the need to declare all variables outside the loop.
  • Participants discuss the correct placement of semicolons and the interpretation of compiler error messages.
  • A participant presents a revised version of the code that incorporates feedback and successfully calculates the payment, balance, and interest over five months.

Areas of Agreement / Disagreement

Participants generally agree on the approach to structuring the program and the importance of variable naming and initialization, but there is no explicit consensus on the initial implementation details before the revisions were made.

Contextual Notes

Limitations include potential misunderstandings of variable scope and the handling of compiler errors, which were not fully resolved in the discussion.

Who May Find This Useful

Students learning Java programming, particularly those working on assignments involving loops and financial calculations.

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 :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
2K
Replies
3
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K