Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Assignment Help

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default While Loop Problem with Java Assignment Program

    Hey guys this is my 1st time posting here but have browsed the Forum at times before I am going an introduction course to Java and have coded my latest program which is based on Student Loans the calculations within the program are correct but I seem to be having problems with the While Loop within the program running properly, here is what I've got so far

    public class LoanPayments {
        // place constant declarations (static final) below.
        // e.g. 3% interest, 9% repayment, 5% pay rise and 10k theshold
            static final double INTEREST = 0.03, REPAID = 0.09, PAYRISE = 0.05, NET = 10000;  
        public static void main(String args[]) {
            double loans, prevloan, newloan, wages, net, interest, newlinterest=0, linterest=0, wagest=0, newwages=0, repaid=0, balance=0, newbalance=0, yrbal=0, year=1;
            // place all variable declarations here
     
            System.out.println(" Student Loan" );
            loans = UserInput.readDouble();
            // prompt and read the loan amount
     
            System.out.println (" Wages ");
            wages = UserInput.readDouble();
     
                interest =  loans * INTEREST;
                // add 3% to the loan
                net = wages - NET;
                // deduct 10k from the wages
                repaid = net * REPAID;
                // from the remaining wages calculate 9% repayment
                linterest = loans + interest;
                balance = linterest - repaid;
                // reapaid loan amount
                wagest = net;
                yrbal= balance * year;
                // sum up repayments and years so far
                System.out.println(" Year " + (int)year);
                MoneyOut.println(" Old Loan ", (int)loans);
                // produce output: current year, old loan, 
                MoneyOut.println(" Loans plus 3% interest ", linterest);
                // loan plus 3% interest, wages, 
                MoneyOut.println(" Wages ", (int)wages);
                MoneyOut.println(" Wages minus threshold ", wagest);
                //wages minus 10,000 pounds, repayment, new loan
                MoneyOut.println(" Repayment ", (int)repaid);
                MoneyOut.println(" New loan amount ", balance);
     
            while (newbalance>0) {
                // while loop starts here
                year++;
                prevloan = balance;
                newloan = prevloan;
                interest =  newloan * INTEREST;
                // add 3% to the loan
                newwages = wages + (wages * 5.0 /100.0);
                // calculate new wages by adding a pay rise of 5%
                net = newwages - NET;
                // deduct 10k from the wages
                repaid = net * REPAID;
                // from the remaining wages calculate 9% repayment
                //if (newwages <0) {
                  //  repaid =0;
                //} 
                // (if the remaining wages are zero or less, repayment is zero)
                newlinterest = newloan + interest;
                newbalance = newlinterest - repaid;
                // reapaid loan amount
                wagest = net;
                yrbal= newbalance * year;
                // sum up repayments and years so far
     
                System.out.println(" Year " + (int)year);
                MoneyOut.println(" Old Loan ", (int)balance);
                // produce output: current year, old loan, 
                MoneyOut.println(" Loans plus 3% interest ", newlinterest);
                // loan plus 3% interest, wages, 
                MoneyOut.println(" Wages ", (int)newwages);
                MoneyOut.println(" Wages minus threshold ", wagest);
                //wages minus 10,000 pounds, repayment, new loan
                MoneyOut.println(" Repayment ", (int)repaid);
                MoneyOut.println(" New loan amount ", newbalance);
                if (year >=30)
                {
                    System.out.println(0);
                }  // 'break' out of the 'while' loop if more than 30 years have passed
            }   // end of the 'while' loop, program leaves it when loan is 0 or less
     
                System.out.println(" It took you " +(int)year + 
                " years to repay the loan "); 
            // print out the number of year it took to repay the loan
     
                System.out.println(" The orginal loan was " + "£" + loans + 
                " but you had to pay back " + yrbal);
            // print out the original loan and the total amount paid back
     
                // When 'debugging' you can use the code below to allow 
                // the loop to run one year at a time. Take it out when
                // submitting to CourseMarker !!!
                // System.out.println("Press 'Enter' to continue");
                // UserInput.readString(); // waits for 'Enter' 
        } // end of main
     
    } // end of class LoanPayments

    Can anyone spot a reason why the Loop won't run properly the MoneyOut Class is a custom Class included with the program so is not the reason for the error.
    Last edited by welsh_rocker; January 11th, 2011 at 05:02 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Assignment Help

    define "won't run properly". Are you getting an exception? If so, please post the full exception text.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Assignment Help

    Sorry the problem within the coding seems to be focused within the while Loop, it is supposed to implement a Loop a number of times until the balance amount reaches zero, but will only run through the Loop 2 times at the most before ending the program.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Assignment Help

    Actually, your while loop isn't executing at all. newbalance is still equal to zero by the time the program reaches the while.

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Assignment Help

    Hey thanks for the feedback I will review the code and make the necessary changes

Similar Threads

  1. Really stressing with this java assignment
    By Nicky2010 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 5th, 2010, 04:03 PM
  2. my java assignment -- please help
    By java_beginner in forum Java Theory & Questions
    Replies: 6
    Last Post: May 20th, 2010, 08:32 AM
  3. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  4. I need extensive help with my java assignment
    By MoshMoakes in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 12th, 2009, 07:44 PM
  5. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM