Notes

Declaring Variables

  • Variables defined within a method are local variables
  • Int holds wh ole numbers
  • You need an initial value in order to change it or increment it (usually set to 0)
public class Variables {
    public static void main(String[] args) {
        int x;
        x = 90;
        x = 5;
        x = x + 1;

        System.out.println("The variable x = " + x);
    }
}
Variables.main(null)
The variable x = 6

Primitive Data Types

  1. Byte: minimum value of -128 and maximum value of 127 (inclusive). It is useful for saving memory in large arrays
  2. Short: Minimum value of -32,768 and maximum value of 32,767. Same purpose as Byte
  3. Int: Any integer or whole number
  4. Long: Greater range than int
  5. Float: floating point numbers that tend to have decimals
  6. Double: not good for precise data
  7. Boolean: logic that evaluates whether a condition is true or false
  8. Char: data type that is used to store a single character (must be in 'single quotes')
public class PrimitiveDataTypes {
    public static void main(String[] args) {
        int a = 5;
        double b = 5.0;
        b = 3; // only works for double
        a = (int) 5.999; // casting cuts off everything after the decimal
        System.out.println(a);

        boolean c = true;
        c = false;
        System.out.println(c);

        char d = 'D';
        System.out.println(d);
    }
}
PrimitiveDataTypes.main(null)
5
false
D

Homework

2006 FRQ 2A

public double purchasePrice () {
    double taxTotal = getListPrice() * taxRate;
    return taxTotal + getListPrice();
}

2006 FRQ 3A

public int compareCustomer (Customer other) {
    int c = getName().compareTo(other.getName());

    if (c != 0) {
        return c;
    }
    else {
        int rd = other.getID();
        
        if (getID() = rd) {
            return 1;
        }
        else if (getID() < rd) {
            return -1;
        }
        else {
            return 0;
        }
    }
}