Java Credit Card Payment Calculator with For Loop

In summary, the program prints out "Payment: $500.00 Balance: $0.00 Interest: $0.15 New Balance: $0.00" on five separate occasions.
  • #1
jai6638
263
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
  • #2
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
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:
  • #4
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
5) Declare and initialize ALL VARIABLES outside of the for loop, at the beginning of your function. This is generally a good idea.
 
  • #6
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);
         }

     }

}
 
  • #7
That looks good -- does it work?

- Warren
 
  • #8
yup.. it works..

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

1. How do I calculate payment using Java?

To calculate payment using Java, you can use the built-in Math library which includes functions for addition, subtraction, multiplication, and division. You will need to gather the necessary input values, such as the total amount due and the number of payments, and then use these values in your calculation. You can also use conditional statements and loops to make your calculation more dynamic.

2. Can Java calculate complex payment formulas?

Yes, Java has the capability to calculate complex payment formulas. You can write your own custom functions to handle specific formulas or use existing libraries and APIs that offer more advanced mathematical functions. With the right knowledge and tools, Java can handle any type of payment calculation.

3. How can I troubleshoot errors in my payment calculation program written in Java?

If you encounter errors in your payment calculation program, you can use a debugging tool to step through your code and identify the source of the error. You can also use print statements to track the values of your variables and see where the error may be occurring. It is also important to double check your syntax and ensure that your code is logically sound.

4. Can Java handle currency conversion for payment calculations?

Yes, Java can handle currency conversion for payment calculations. You can use APIs or libraries that offer currency conversion functions or write your own custom code to handle the conversion. It is important to ensure that you are using accurate and up-to-date exchange rates for your calculations.

5. Is there a specific Java library for payment calculations?

There are many Java libraries available for payment calculations, such as Apache Commons Math, JScience, and Joda-Money. These libraries offer a variety of functions and capabilities for handling payment calculations. It is important to research and choose the library that best fits your specific needs.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
720
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
Replies
3
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top