Homework #2

### CS35 Spring 2002 Homework 2 Due: Thursday February 7, before 2am (late Wed. night)

#### CONTENTS

You should work by yourself on this assignment.

#### ASSIGNMENT

Problem 1: GradBookEntry Class Starting with the classes given below that are similar to the Person and Student classes we covered in class, implement a GradeBookEntry class as shown in
Figure 1.

Include a main method to the GradeBookEntry class that does the following:

• Creates an array of 10 GradeBookEntry objects, and for each element i in the array, create a new GradeBookEntry object with:
```       student field:
firstName = "first_i"
lastName = "last_i"
age = a random age between 18 and 22
assignments field:
each element in the array = random grade between 50 and 100
```

• For each GradeBookEntry object, print the student's name, the student's max, min, and average grade. Print this as a table. Something like this is fine:
```       Student's Name     Max     Min     Ave
--------------     ---     ---     ---
first_0 last_0     96      50      69.25
first_1 last_1     97      51      71.1
first_2 last_2     99      59      79.75
.                  .       .       .
.                  .       .       .
.                  .       .       .
```
• Print each student's grade on assignment #4
• Print the Student information for the student with the highest grade on assignment #4

Problem 2: Credit Cards
You will write a Java program that allows a user to create a new credit card of one of three types: a regular card, a gold card, or a last-chance card. Next your program will enter a loop that will:

• print a menu of credit card actions
• get the user's menu selection
• perform the action, or print an error message if the user enters invalid input values
• print information about the current status of the credit card account
The menu will have the following options:
1. Use credit card
2. Make a payment
3. Quit
When the user selects option 3, to quit, your program should terminate.

Characteristics of each type of credit card:

• A regular credit card has an interest rate of 18%, and an upper limit of \$5,000.
• A gold card has an interest rate of 10% and an upper limit of \$500,000. In addition, gold card subscribers can earn frequent flyer miles for every dollar they spend (spend 1 dollar earn 1 frequent flyer mile).
• A last chance card is for people with a bad credit history. The interest rate is 25% and there is a \$1,000 charge limit. A last chance card holder can get better rates by paying off their complete balance three times in a row; if a holder of a LastChance card makes complete payments three payments in a row, their interest rate drops to 20% and their limit increases to \$1,500.

#### Implementation Details and Requirements

Starting with the CreditCard class given in the book implement features specified in the book problems R-1.3 and R-1.4. For R-1.4, use the regular credit card interest rate and limit; in your program, regular credit card objects will be instances of the CreditCard class. Each time a user makes a payment, you should charge interest on any unpaid balance (note: unlike a real credit card, you do not need to keep track of time in your program and charge interest when a payment is late, instead every time a user chooses to make a credit card payment your program should check to see if interest should be charged based on the remaining balance alone).

Once you have added these changes to the CreditCard class and you have tested that they work correctly, do the following:

1. Create a GoldCard class that is a subclass of CreditCard. The status information of a GoldCard should include the number of frequent flyer miles accumulated so far.
2. Create a LastChanceCard class that is a subclass of CreditCard. Override the makePayment method to check if three full payments have been made in a row, and if so modify the interest rate and limit accordingly.
Specific Requirements
• You should create a RunCreditCard class containing a main method that implements the main loop of your program. Use good modular design when writing this class; all the code should not be in the main method. Instead, break the program into well-defined subtasks and utilities, and implement each as as a private method called by the main method.
• You must define and use an exception class NonPositiveValueException that will be thrown by all methods that have dollar amount parameters. The method will throw this exception when it is passed a non-positive dollar amount. Code in RunCreditCard.java (the main program) should catch and handle this exception in a reasonable way. Terminating the program is not a reasonable way to handle this exception.
• After each transaction (menu option selected and handled) print the new balance, the interest rate, and the credit limit.
• You must write and use a makefile for compiling Problem#2 .java files. Hand in your makefile along with all your program .java files. An example Java makefile is available off the cs35 homepage.

To implement some of the functionality in the GoldCard and LastChanceCard classes, you may need to add (or change) methods in the CreditCard class as well as add data members to this class.

Do not worry about formating floating point values for this assignment. However, if you would like to play around with number formating, take a look at the API documentation for the DecimalFormat and NumberFormat classes. Here is an example of how you could format dollar amounts to print the retail price of an Auto object from your last homework assignment:

```        NumberFormat mf = NumberFormat.getCurrencyInstance(Locale.US);
String priceOutput = mf.format(this.retailPrice());
System.out.println(" retail price is " + priceOutput );
```

#### Classes

You should use the following classes as starting points for your programs:

#### HAND IN

Using cs35handin, hand in a single tar file containing all the Homework 2 .java files necessary for compiling and running your solutions to both problem 1, and 2 (including all of the files that we give you that you use in your solution), and a copy of your Makefile.