Java:

  • Has simple syntax
  • Allows for automatic garage collection
  • More flexible and efficient because its OOP
  • It can be run on any platform
  • It allows for multi threading
  • Secure language because it doesn't have pointers

Primitives vs. Non primitives

  • Primitives

    • Predefined
    • Lowercase
    • Cant call methods
  • Non primitives

    • Uppercase
    • Can call methods and be null
    • Regular Java: String is considered a Non-Primitive data type
  • Wrapper Classes: is a class and has methods ### Primitives
  • Boolean
    • true/false
    • one but
  • Integer
    • integer values
    • 2-3 bots
  • Double
    • decimal values
    • 64 bits
  • Extra Primitive Examples; char, float, long, etc.

New vocab

Casting with truncating/rounding

  • One of the applications of using a cast is rounding to the nearest integer. Since casting doubles to integers simply truncates the decimal, we need to modify the original input in order to achieve true rounding. Positive Rounding
  • With truncation, casting any negative decimal will covert the decimal to the rounded-up integer. However, we want decimals less than 0.5 to round up and the rest to round down. In the same opposite nature, we subtract 0.5 to the double before casting rounding: (int) (a + 0.5); Examples: a = 1.3 which would normally round to 1: (int) (1.3 + 0.5) = (int) (1.8) = 1 a = 1.8 which would normally round to 2: (int) (1.8 + 0.5) = (int) (2.3) = 2 a = 1.5 which would normally round to 2: (int) (1.5 + 0.5) = (int) (2) = 2 a = 1 which would normally round to 1: (int) (1 + 0.5) = (int) (1.5) = 1 truncating (int) (a - 0.5); Examples: a = -1.3 which would normally round to -1: (int) (-1.3 - 0.5) = (int) (-1.8) = -1 a = -1.8 which would normally round to -2: (int) (-1.8 - 0.5) = (int) (-2.3) = -2 a = -1.5 which would normally round to -2: (int) (-1.5 - 0.5) = (int) (-2) = -2 a = -1 which would normally round to -1: (int) (-1 - 0.5) = (int) (-1.5) = -1

Casting with division

  • If you divide 5/3 using integer division, Java will truncate 1.67 to 1 to give an int result. However, we usually round up any answer .5 and above. Using the formula below, if we add 1.67 + 0.50, we get 2.17 and then casting it to an int throws away what’s after the decimal point, just leaving 2 Example: int nearestInt = (int)(number + 0.5); int nearestNegInt = (int)(negNumber – 0.5);

casting tangibles

int total = 100;
        int numPeople = 40;
        double average = total / (double) numPeople;
        System.out.print("Total: ");
        System.out.println(total);
        System.out.print("People: ");
        System.out.println(numPeople);
        System.out.print("Average: ");
        System.out.println(average);
Total: 100
People: 40
Average: 2.5

2006 Collegeboard FRQ

2a

public double purchasePrice(){
    return ((1 + taxrate) * getListPrice());  
  }

3a

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