Primitives Types HW Post
Primitive Types HW post for AP CSA
2006 FRQ 2a
public interface Item {
double purchasePrice();
}
// Class TaxableItem implements the interface Item, which contains the method purchasePrice()
public abstract class TaxableItem implements Item {
private double taxRate;
public abstract double getListPrice();
// Constructor for initializing the tax rate, if there is any
public TaxableItem(double rate){
taxRate = rate;
}
// Calculates the purchase price of an item by multiplying the tax rate by the list price, then adding that to the original list price
// If there is no tax rate, then the purchase price is simply equal to the list price
public double purchasePrice() {
double purchasePriceValue = getListPrice() + (taxRate * getListPrice());
return purchasePriceValue;
// Alternate way:
// return getListPrice() * (1.0 + taxRate);
}
}
// Since we are working with an abstract class, we would have to extend to it with another class in order to run our program
// As of right now, the class hierarchy seems to be very complicated, so I will further research ways to output the program results
2006 FRQ 3a
public class Customer {
private String cName;
private int cID;
// Constructor to initialize customer name and id
public Customer(String name, int idNum) {
cName = name;
cID = idNum;
}
// Gets the name of the specified customer
public String getName() {
return cName;
}
// Gets the unique ID of the specified customer
public int getID() {
return cID;
}
// Compares given customer with another customer. Returns a positive integer if the customer is greater than the other customer,
// 0 if they are equal, and a negative integer if the customer is less than the other customer.
public int compareCustomer(Customer other) {
int nameComparison = getName().compareTo(other.getName());
if (nameComparison != 0) {
return nameComparison;
} else {
if (getID() != other.getID()) {
return getID() - other.getID();
}
}
return 0;
}
// Running method for visualizing outputs
public static void main(String[] args) {
Customer c1 = new Customer("Smith", 1001);
Customer c2 = new Customer("Anderson", 1002);
Customer c3 = new Customer("Smith", 1003);
Customer c4 = new Customer("Will", 1004);
System.out.println(c1.compareCustomer(c4));
}
}
Customer.main(null);