Vocab

Casting (division)

When you divide a double by a double (known as real division), the output (result) is a double. When both expressions are an integer, the result is an integer, which can cause complications. For example, 5/2 realistically does not produce and integer result.

Without casting, the result will be the nearest lower integer, and the remainder is disregarded.

With casting, you can cast the denominator to a double. When one value is a double and the other is an int, java casts the int into a double, which allows it to perform real division.

int x = 5/2;
System.out.println(x);
2
double y = 7/(double)3;
System.out.println(y);
2.3333333333333335

Casting (Truncating or Rounding)

When rounding, double values can be rounded by adding .5 (subtracting for negative numbers) and casting with (int). When you cast as an integer, java always rounds down, so by adding .5, it ensures that doubles that end in .5 or higher are casted as the integer greater than their value

When truncating, you can just cast as an int, which will always result in the integer just less than the double value, which is exactly what truncating is.

double x = 8.0/3;
    int rounded = (int)(x + 0.5);
    System.out.println("8.0/3 = " + x);
    System.out.println("8.0/3 rounded = " + rounded);
    System.out.println("8.0/3 truncated = " + (int)x);
8.0/3 = 2.6666666666666665
8.0/3 rounded = 3
8.0/3 truncated = 2

2006 FRQ

Problem 2a

A set of classes is used to represent various items that are available for purchase. Items are either taxable or nontaxable. The purchase price of a taxable item is computed from its list price and its tax rate. The purchase price of a nontaxable item is simply its list price. Part of the class hierarchy is shown in the diagram below.

*Diagram not shown The definitions of the Item interface and the TaxableItem class are shown below

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()
    { /* to be implemented in part (a) */}
}

Write the TaxableItem method purchasePrice. The purchase price of a TaxableItem is its list price plus the tax on the item. The tax is computed by multiplying the list price by the tax rate. For example, if the tax rate is 0.10 (representing 10%), the purchase price of an item with a list price of 7.15. Complete method purchasePrice below.

// returns the price of the item including the tax
public double purchasePrice(){
    double price = 1.1 * getListPrice();
    return price;
}

Problem 3a

Consider the following incomplete class that stores information about a customer, which includes a name and unique ID (a positive integer). To facilitate sorting, customers are ordered alphabetically by name. If two or more customers have the same name, they are further ordered by ID number. A particular customer is "greater than" another customer if that particular customer appears later in the ordering than the other customer.

public class Customer
{
    //constructs a Customer with a 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 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)
    { /* to be implemented in part (a) */ }

    // There may be fields, constructors, and methods that are not shown.
}

Write the Customer method compareCustomer, which compares this customer to a given customer, other. Customers are ordered alphabetically by name, using the compareTo method of the String class. If the names of the two customers are the same, then the customers are ordered by ID number. Method compareCustomer should return a positive integer if this customer is greater than other, a negative integer if this customer is less than other, and 0 if they are the same.

public int compareCustomer(Customer other){
    int nameCompare = this.getName().compareTo(other.getName());
    if(nameCompare == 0){
        return this.getID() - other.getID();
    }
    else{
        return nameCompare;
    }
}