Unit 1: Primitives
2a. Multiplying the tax rate + 1 gives the actual multiplier we want to use for the new price, since it includes the tax and existing price.
public interface Item
{
double purchasePrice();
}
public abstract class TaxableItem implements Item
{
private double taxRate;
public abstract double getListPrice();
public TaxableItem(double rate)
{ taxRate = rate; }
// returns the price of the item including the tax
public double purchasePrice()
{ return (1 + taxRate) * getListPrice();}
}
3a. Returning nameCompare if it's not 0, subtracting IDs otherwise
public class Customer
{
// constructs a Customer with given name and ID number
public Customer(String name, int idNum)
{ /* implementation not shown */ }
// returns the customer's name
public String getName()
{ /* implementation not shown */ }
// returns the customer's id
public int getID()
{ /* implementation not shown */ }
// 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)
{
int nameCompare = getName().compareTo(other.getName());
if (nameCompare != 0) {
return nameCompare;
}
else {
return getID() - other.getID();
}
}
// There may be fields, constructors, and methods that are not shown.
}