2006 Customer
<< 2006TaxableItem | APQuestionsTrailIndex | 2005 Hotel >>
Question from the College Board
Customer
public class Customer
{
private String name;
private int idNum;
// constructs a Customer with given name and ID number
public Customer(String name, int idNum)
{ this.name = name; this.idNum = idNum; }
// returns the customer's name
public String getName()
{ return name; }
// returns the customer's id
public int getID()
{ return idNum; }
// returns 0 when this customer is equal to other;
// a positive integer when this customer is greater than other;
// a negative integer when this customer is less than other
public int compareCustomer(Customer other)
{
//part a here
}
public String toString()
{
return name + " " + idNum;
}
}
Company
public class Company
{
/*** Part (b) ***/
// fills result with customers merged from the
// beginning of list1 and list2;
// result contains no duplicates and is sorted in
// ascending order by customer
// precondition: result.length > 0;
// list1.length >= result.length;
// list1 contains no duplicates;
// list2.length >= result.length;
// list2 contains no duplicates;
// list1 and list2 are sorted in
// ascending order by customer
// postcondition: list1, list2 are not modified
public static void prefixMerge(Customer[] list1,
Customer[] list2,
Customer[] result)
{
//part b here
}
}
CustomerTester
public class CustomerTester
{
public static void main(String[] args)
{
System.out.println("Part a:");
Customer c1=new Customer("Smith", 1001);
Customer c2=new Customer("Anderson", 1002);
Customer c3=new Customer("Smith", 1003);
System.out.println(c1.compareCustomer(c1)+
"\t Should be 0");
System.out.println(c1.compareCustomer(c2)+
"\t Should be positve");
System.out.println(c1.compareCustomer(c3)+
"\t Should be negative");
Customer[] list1 =
{
new Customer("Arthur", 4920),
new Customer("Burton", 3911),
new Customer("Burton", 4944),
new Customer("Franz", 1692),
new Customer("Horton", 9221),
new Customer("Jones", 5554),
new Customer("Miller", 9360),
new Customer("Nguyen", 4339)
};
Customer[] list2 =
{
new Customer("Aaron", 1729),
new Customer("Baker", 2921),
new Customer("Burton", 3911),
new Customer("Dillard", 6552),
new Customer("Jones", 5554),
new Customer("Miller", 9360),
new Customer("Noble", 3335),
};
Customer[] exp =
{
new Customer("Aaron", 1729),
new Customer("Arthur", 4920),
new Customer("Baker", 2921),
new Customer("Burton", 3911),
new Customer("Burton", 4944),
new Customer("Dillard", 6552),
};
Customer[] result = new Customer[6];
System.out.println("Part b:");
Company.prefixMerge(list1, list2, result);
for (int i=0;i<result.length;i++)
System.out.println(result[i]+" (Should be "+exp[i]+")");
}
}
