CIS 27P

Java For Programmers

Homework #2

Due: 7/19/2001

 

Assignment:

 

Write two Java classes named Customer and FrequentCustomer.? Customer should have private instance variables firstName (String), lastName (String), totalPurchases (int), and highestPurchase (float).? FrequentCustomer should be derived from Customer and have the additional instance variable discount (float as percentage).

 

Both Customer and FrequentCustomer should have constructors with parameters for each instance variable.? Additionally, the classes should have ?setter? and ?getter? methods to modify each of those variables, such as setFirstName( String ) and getFirstName();

 

Customer should have a static report method that accepts an array of Customer objects and prints out a report with their lastName, firstName, and totalPurchases in the format $X.XX.? FrequentCustomer should override the report method to also display the discount percentage in the format XX%.? Both reports should have an appropriate title.

 

Test Class:

 

You can use the following Controller class to test your classes.

 

class CustomerController

{

public static void main( String[] args )

 

{

??? Customer ca[] = new Customer[2];

??? ca[0] = new Customer( "George", "Washington",? 5, 5.25f );

??? System.out.println("New customer: "+ca[0].getFirstName()+" "+ca[0].getLastName());

??? ca[1] = new Customer("Martha", "Washington", 4, 7.37f);

??? System.out.println("New customer: "+ca[1].getFirstName()+" "+ca[1].getLastName());

??? FrequentCustomer fca [] = new FrequentCustomer[2];

??? fca[0] = new FrequentCustomer("Bugs", "Bunny", 20, 12.37f, 0.10f);

??? System.out.println("New customer: "+fca[0].getFirstName()+""+fca[0].getLastName());

??? fca[1] = new FrequentCustomer("Daffy", "Duck", 18, 14.27f,? 0.23f);

??? System.out.println("New customer: "+fca[1].getFirstName()+" "+fca[1].getLastName());

??????

??? fca[0].setFirstName("Olive");

??? fca[0].setLastName("Oil");

 

??? System.out.println("\n");

??? Customer.report(ca);

??? System.out.println("\n");

??? FrequentCustomer.report(fca);

}

}


Expected Output:

 

Your test class should have the following output:

 

New Customer: George Washington

New Customer: Martha Washington

New Customer: Buggs Bunny

New Customer: Daffy Duck

 

Customer Report

First???? Last ???? ???? Total???? Highest?

George???? Washington???? 5??? ???? $5.25

Martha???? Washington???? 4??? ???? $7.37

 

Frequent Customer Report

First???? Last ???? ???? Total???? Highest???? Discount

Olive???? Oil? ???? ???? 20?? ???? $12.37?? 10%

Daffy???? Duck ???? ???? 18?? ???? $14.27?? 23%