Overview

  • Primitive data types

    • ints
    • booleans
    • doubles
    • chars
  • String literals: String created when you double quote String

  • Literal: Something that means exactly what you define it to
  • Errors Types:
    • syntax error
    • exception
    • logic error

Variables

  • defined when you put the type of variable that it is before hand and then initiate it
  • must use camel case when naming variables
  • example:
int score;

Arithmetic Operators

  • There are + - * and /
  • Represent add, subtract, multiply, and divide
  • Integer operations result in integers
  • Both int and double produce double
  • only doubles produces double
  • dividing by 0 is an arithmetic exception
int x = 5;
int y = 6;
System.out.println(x+y);
11

Compound operators

  • +=, -=, *=, /=, %=
  • Changes value of variable by doing arithmetic operation with that variable

Increment operators

  • ++ or -- which either go up 1 or go down 1
    • useful for for loops

Casting

  • Treating a variable as another type of variable
  • Operators: (int) or (double)

Division

  • Two integers become 1 integer
  • A double and an integer become a double

2006 FRQ

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.

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;
}

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.

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;
    }
}