Primitives

  • predefined
  • lowercase
  • cannot call methods
  • need to know: boolean, int, double

Operators

  • (+) addition
  • (-) subtraction
  • (/) division
    • casting forces products to be a double even if numerator and denominator are integers
  • (*) multiplication
  • (%) modulus (remainder)
  • ++ add 1
  • -- subtract 1
System.out.println((double) 1 / 3); // cast answer is double even though 1 ad 3 are integers
System.out.println((int) 2.7 + 4); // truncates answer to nearest integer
double x = 3.6;
System.out.println((int)(x + 0.5)); // adding 0.5 rounds
0.3333333333333333
6
4

2006 FRQ 2a

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 $6.50 would be $7.15.

// returns the price of the item including the tax
public double purchasePrice() {
    return (1 + taxRate) * getListPrice(); 
}

2006 FRQ 3a

Write the Customer method compareCustomer, which compares this customer to a given customer, other. Customers are ordered alphabetically by name, using the comparTo 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.

// 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 getID() - other.getID();
    }
    else {
        return nameCompare;
    } 
}