Reference no: EM131310188
Introduction to Programming
Java Programming Assignment: Objects and Loops
Your previous Alice programs implemented the count (for) and while loops.
This assignment will apply the same concepts to Java, along with a third type of loop, the do-while loop. You will again use an object to store data. Then looping statements will be used to run some of the statements repeatedly.
Problem Summary and Equations
Write a program to compare how long it will take to pay off a credit card balance by paying only the minimum required payment each month, or by paying a larger amount each month. The program will work for any credit card balance of $500 or more.
• The credit card will have an annual interest rate between 3% and 25%.
• Interest will be applied monthly. This means that 1/12th of the annual interest will be added to the current balance each month.
• The user can choose to pay down anywhere from 6% to 33% of the balance every month. The payment made each month will always be at least the minimum required payment, but will never be more than the remaining balance.
This program will implement a "generic" input reading method called readPercent that can be used to read and validate a percentage is within a specified range. The method will include 3 parameters:
- the lowest percentage allowed
- the highest percentage allowed
- a String description of what percentage is being read from the user
By passing in these values as parameters, you can re-use the method to read any range of percentages needed for any data value.
For example, if you were to call the method to read a mortgage rate, which is a percentage between 3.5% and 20%, the call would be:
double mortgageRate = readPercent(3.5, 20, "annual mortgage rate");
Or to read a down payment percent, which is a percentage between 10% and 30%, the call would be:
double downRate = readPercent(10, 30, "down payment percent");
Required Classes and Methods
Two separate classes will be required for this program.
1. Define a Java class with properties and methods for a Credit Card Account, named:
The class will have the following private properties:
- current balance
- annual interest rate (percentage - e.g. 12.5%)
- percent of current balance to pay each month (e.g. 10%)
Within the CreditCardAccount class you will define methods, as follows:
• Define a constructor, with parameters that pass in the user entered values for only two of the properties, the initial balance and annual interest rate.
o Use the parameters to initialize:
- the current balance property to the initial balance parameter value
- the annual interest rate property to the annual interest rate parameter value
o Initialize the percent to pay off each month to 0.
• Define a second constructor, with parameters that pass in the user entered values for all three of the properties, and use them to initialize the property values.
• Define a getter for the percent of current balance to pay each month.
• Define an instance method to determine and return the minimum required payment for a month:
o Define and use three local constants:
- A low balance minimum payment ($50)
- A high balance percentage (5%)
- A low balance limit ($1000)
o If the current balance is below the low balance limit, then the minimum required payment will be the low balance minimum payment.
o Otherwise, the minimum required payment will be the high balance percentage of the current balance.
o Return the correct minimum required payment
• Define a make payment instance method to record and display a payment for one month (note that there will be no loops in this method). This method will:
o Calculate values and display one line of output, containing the data about one month's payment, as follows:
- Display the current (starting) balance.
- Use the current balance to calculate the interest to be applied for the month, and add that interest to the current balance.
- Display the interest charge and current balance (with interest).
- Using the current balance (with interest), calculate the payment for the month, based on the percent of current balance to pay each month data field.
- Call the instance method to determine the minimum required payment.
- Check to see if the calculated payment is less than the minimum required payment.
o If so, set the calculated payment to the minimum required payment.
- The calculated payment may be higher than the remaining balance. So check to see if the calculated payment is higher than the remaining balance.
o If so, set the calculated payment to the remaining balance.
- Display the calculated payment that will be paid for the month.
- Subtract the calculated payment from the current balance (with interest).
- Display the new current balance (i.e. the ending balance, after the payment has been made).
• Define a payoff instance method to calculate and display information each month until the credit card is paid off. This method will:
o Display a line describing the percent of current balance that will be paid each month
- If the percent is 0, display "minimum payment" instead of a percent.
o Display headers for the results (see sample output below).
o Use a while loop to update and display information about the credit card account every month, as follows:
- Display the month number (starting with month 1, and incrementing by 1 each time the code loops).
- Call the above defined make payment method to update and display the data values for the month.
o Stop looping when the current balance reaches 0.
o Return the number of months needed to pay off the card.
NOTE: When one instance method needs to call another instance method, the second instance method should be called using the this reference to reference the current object.
2. Define a second class containing a main method, and additional methods, to compare paying only the minimum required payment each month with paying a larger amount each month. Name the class:
Within the PayoffComparison class:
• Define a static method to prompt for, read, and validate an initial credit card balance. The method will:
o Define and use a constant to hold the lowest initial balance allowed ($500). This constant should be used to specify the lowest value allowed in the prompts, and to test for it within conditions.
o Prompt for and read the initial balance.
o If the user enters an initial balance that is below the lowest initial balance allowed:
- Issue an error message, and loop to re-prompt the user and read another value
o Loop until a valid value is entered.
o Return a valid initial balance.
• Define the generic static readPercent method described at the top of this assignment, to prompt for, read, and validate a percentage. This method will:
o Implement a loop that uses a boolean variable as the condition.
o Prompt for and read a percentage.
- The prompt should use the parameters to specify the percentage being read (description) and the lowest and highest percentage rates allowed.
o If the percent entered is not valid (i.e. not between the lowest and highest rate, inclusive):
- Issue an error message, and loop to re-prompt the user and read another value
o Loop until the percent entered is valid.
o Return a valid percentage.
• Define a static method to prompt for, read, and validate the percentage of the balance to be paid off each month. This method will:
o Define constants to hold the lowest and highest paydown percentages allowed (6 and 33).
o Within a loop, until the user enter a valid choice:
- Let the user choose from a menu of five choices on what minimum payment to make: 1 - 10% of remaining balance each month
2 - 20% of remaining balance each month 3 - 30% of remaining balance each month
4 - Some other percent of the balance each month between 6 and 33
- If the user enters 4, call the generic readPercent method to read the percentage of the balance to be paid off each month, between 6 and 33.
- If the user enters an invalid value, loop to display the menu and read another choice.
o Return a valid percentage of the balance to be paid off each month.
• Define a main method that will:
o Define constants to hold the lowest and highest annual interest rates allowed (3 and 25).
o Display a description of what the program will do to the user.
o Read input from the user as follows:
- Call the static methods to read the initial balance.
- Call the static readPercent method to read the annual interest rate.
- Call the static method to read the percentage of the balance to pay off each month.
o After reading all the input, create two new objects of the CreditCardAccount class
- Create the first object using the first constructor and the first two user input values.
- Create the second object using the second constructor and all the user input values.
NOTE: After creating the objects, the variables used to store the values read from the user should not be used again.
o Display a couple of blank lines after reading all of the user input.
o Call the payoff method with the first object, and store the returned number of months.
o Call the payoff method with the second object, and store the returned number of months.
o Display a statement comparing the number of months required to pay off the credit card with only the minimum monthly payments, vs paying off the credit card with a larger monthly payment (use a getter to get the payoff percentage per month).
(The figures in each column should line up with each other on the right - see Sample Output on next page for example)
3. The program must implement both a while and do-while loop somewhere in the code.
4. The program must follow the CS210 Coding Standards from Content section 6.10. Be sure to include the following comments:
o Comments at the top of each code file describing what the class does
- Include tags with the author's name (i.e. your full name) and the version of the code (e.g. version 1.0, Java Assn 5)
o Comments at the top of each method, describing what the method does
- Include tags with names and descriptions of each parameter and return value.
WARNING: The objects, classes, and methods must be implemented exactly as specified above. If your program produces correct output, but you did not create and use the object as specified, and implement the required classes and methods, you will lose a significant number of points.
• Run, debug, and test your Java program with different inputs, until you are sure that all control structures within your program work correctly.
• The sample inputs and output can be used as the initial test to test your program. But be sure you thoroughly test it using other values as well.