//Primitive Data Types
public class PrimitiveDataTypes {
    int integer = 0;
    boolean bool = true;
    char character = 'a';
    double doubleVal = 3.2;
}

public static void main(String[] args) {
    PrimitiveDataTypes type = new PrimitiveDataTypes();
    System.out.println(type.integer + 12);
    System.out.println(type.doubleVal+2.2);
    System.out.println(type.character);
}
main(null);
12
5.4
a

wrapper

//Wrapper Classes
public class Wrappers {
    Integer ageWrapper = 17;
    int age = Integer.parseInt("17");
    String gpaString = "3.9";
    double gpaDouble = Double.parseDouble(gpaString);
}

public static void main(String[] args) {
    Wrappers wrapper = new Wrappers();
    System.out.println(wrapper.ageWrapper);
    System.out.println(wrapper.age);
    System.out.println(wrapper.gpaDouble);
}

main(null);
17
17
3.9

Enums

What are they?

Enums are a type of data, which allows a variable to be a predetermined set of values

Uses

  • Examples: days of the week

Things you can do with Enums

  • ordinal
  • switch
  • for loops
public class EnumTest { 
    enum Units {
    PRIMITVE_DATA_TYPES,
    CLASSES,
    BOOLEAN,
    ITERATION,
    WRITING_CLASSES,
    ARRAY,
    ARRAY_LIST,
    TWO_DIMENSIONAL_ARRAY,
    INHERITANCE,
    RECURSION;
}
public static void main(String[] args) { 
  System.out.println("What is the third unit in AP CSA? Answer: " + Units.BOOLEAN);
  Units classUnit = Units.CLASSES;
  System.out.println("What is the unit is Classes in AP CSA? Answer: " + (classUnit.ordinal() + 1));
  Units selectedUnit = Units.ARRAY_LIST;

  switch(selectedUnit) {
    case PRIMITVE_DATA_TYPES:
      System.out.println("The selected unit is: primitive data types");
      break;
    case BOOLEAN:
       System.out.println("The selected unit is: boolean");
      break;
    case CLASSES:
      System.out.println("The selected unit is: classes");
      break;
    case ITERATION:
      System.out.println("The selected unit is: iteration");
      break;
    case WRITING_CLASSES:
      System.out.println("The selected unit is: writing classes");
      break;
    case ARRAY:
      System.out.println("The selected unit is: array");
      break;
    case ARRAY_LIST:
      System.out.println("The selected unit is: array list");
      break;
    case TWO_DIMENSIONAL_ARRAY:
      System.out.println("The selected unit is: 2d array");
      break;
    case INHERITANCE:
      System.out.println("The selected unit is: inheritance");
      break;
    case RECURSION:
      System.out.println("The selected unit is: recursion");
      break;
  }
  for (Units allUnits: Units.values()) {
    System.out.println(allUnits);
  }
} 
}
EnumTest.main(null);
What is the third unit in AP CSA? Answer: BOOLEAN
What is the unit is Classes in AP CSA? Answer: 2
The selected unit is: array list
Primitive_data_types
CLASSES
BOOLEAN
ITERATION
WRITING_CLASSES
ARRAY
ARRAY_LIST
TWO_DIMENSIONAL_ARRAY
INHERITANCE
RECURSION

Casting

Casting is converting one type of variable to another ex: double to int

public class Casting {
    public static void main(String[] args) {
        double castTest = 3.2;
        System.out.println((int) castTest);
        castTest = 3.7;
        System.out.println((int) castTest);
        System.out.println((int) (castTest+0.5));

        int castTest2 = 3;
        System.out.println(castTest2/2);
        System.out.println(castTest2/2.0);
    }
}
Casting.main(null);

What are the greatest values integers and doubles can store?

public class GreatestValue {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Double.MAX_VALUE);
        System.out.println(Double.MIN_VALUE);
    }
}
GreatestValue.main(null)

Assignment Operators Assignment operators are things like +=, -=, *=, %=

public class AssignmentOperators {
    public static void main(String[] args) {
        int firstInt = 9;
        int secondInt = 1;
        secondInt = firstInt+secondInt;
        System.out.println(secondInt);
        secondInt = 1;
        secondInt += firstInt;
        System.out.println(secondInt);

        //Modulo
        firstInt = 5;
        secondInt = 3;
        System.out.println(firstInt%secondInt);
        System.out.println(firstInt%=3);
    }
}
AssignmentOperators.main(null);

Hacks

import java.util.Scanner;

public class Calculator {

  public static void main(String[] args) {
    Scanner calcInput = new Scanner(System.in);
    String input;
    String num1;
    String num2;

    System.out.println("Pick your first number: ");
    num1 = calcInput.nextLine();

    System.out.println("Please input your desired calculator operation (+, -, *, /, %): ");
    input = calcInput.nextLine();

    System.out.println("Pick your second number: ");
    num2 = calcInput.nextLine();

    int numberInt1 = Integer.parseInt(num1);
    int numberInt2 = Integer.parseInt(num2);
    double numberDoub1 = Double.parseDouble(num1);
    double numberDoub2 = Double.parseDouble(num2);

    if( input.equals("+") ) {
      int resultInt = numberInt1 + numberInt2;
      double resultDoub = numberDoub1 + numberDoub2;
      System.out.println("Integer: " + resultInt);
      System.out.println("Double: " + resultDoub);
    }

    if( input.equals("-") ) {
      int resultInt = numberInt1 - numberInt2;
      double resultDoub = numberDoub1 - numberDoub2;
      System.out.println("Integer: " + resultInt);
      System.out.println("Double: " + resultDoub);
    }

    if( input.equals("*") ) {
      int resultInt = numberInt1 * numberInt2;
      double resultDoub = numberDoub1 * numberDoub2;
      System.out.println("Integer: " + resultInt);
      System.out.println("Double: " + resultDoub);
    }

    if( input.equals("/") ) {
      int resultInt = numberInt1 / numberInt2;
      double resultDoub = numberDoub1 / numberDoub2;
      System.out.println("Integer: " + resultInt);
      System.out.println("Double: " + resultDoub);
    }

    if( input.equals("%") ) {
      int resultInt = numberInt1 % numberInt2;
      double resultDoub = numberDoub1 % numberDoub2;
      System.out.println("Integer: " + resultInt);
      System.out.println("Double: " + resultDoub);
    }
  }
}
Calculator.main(null);
Pick your first number: 
Please input your desired calculator operation (+, -, *, /, %): 
Pick your second number: 
Integer: 8
Double: 8.0
import java.util.Scanner;

public class Calculator {

    public double add(double x, double y){
        return x + y;
    }

    public double sub(double x, double y){
        return x - y;
    }

    public double mult(double x, double y){
        return x * y;
    }

    public double div(double x, double y){
        if(y == 0){
            System.out.println("Not possible");
            return 0; // Return 0 to avoid proceeding with the division
        } 

        return x / y;
    }

    public static void main(String[] args){
        Calculator calculator = new Calculator();
        Scanner sc = new Scanner(System.in);

        while(true){
            System.out.println("Choose an operation: 0. Exit, 1. Add, 2. Subtract, 3. Multiply, 4. Divide");
            int choice = sc.nextInt();

            if(choice == 0){
                break;
            }

            System.out.println("Enter two numbers: ");
            double num1 = sc.nextDouble();
            double num2 = sc.nextDouble();

            switch(choice){
                case 1:
                    System.out.printf("You chose to add the two numbers %f and %f. Result: %f%n", num1, num2, calculator.add(num1, num2));
                    break;

                case 2:
                    System.out.printf("You chose to subtract the two numbers %f and %f. Result: %f%n", num1, num2, calculator.sub(num1, num2));
                    break;

                case 3:
                    System.out.printf("You chose to multiply the two numbers %f and %f. Result: %f%n", num1, num2, calculator.mult(num1, num2));
                    break;

                case 4:
                    System.out.printf("You chose to divide the two numbers %f and %f. Result: %f%n", num1, num2, calculator.div(num1, num2));
                    break;

                default:
                    System.out.println("Invalid choice!");
            }            
        }

        sc.close(); 
    }
}

Calculator.main(null);
Choose an operation: 0. Exit, 1. Add, 2. Subtract, 3. Multiply, 4. Divide
Enter two numbers: 
You chose to add the two numbers 2.000000 and 3.000000. Result: 5.000000
Choose an operation: 0. Exit, 1. Add, 2. Subtract, 3. Multiply, 4. Divide