Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java help

  1. Apr 23, 2005 #1
    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. jcsd
  3. Apr 23, 2005 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  4. Apr 23, 2005 #3
    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
  5. Apr 24, 2005 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  6. Apr 24, 2005 #5
    5) Declare and initialize ALL VARIABLES outside of the for loop, at the beginning of your function. This is generally a good idea.
     
  7. Apr 24, 2005 #6
    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);
             }

         }

    }


     
     
  8. Apr 24, 2005 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    That looks good -- does it work?

    - Warren
     
  9. Apr 24, 2005 #8
    yup.. it works..

    Thanks much for your help guys.. appreciate it :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java help
  1. Java Help (Replies: 7)

  2. Java help (Replies: 15)

  3. Need help with Java (Replies: 53)

  4. Help! Cant get Java! (Replies: 3)

Loading...