Unit 1: Primitives

  • Two main types of data in java
    • primitives
      • int
      • boolean
      • char
      • double
    • objects
      • strings
      • other classes
  • Primitives do not have methods and properties while these are present in classes (ie. String length() method)
  • Homework from student presentation

Casting

  • Casting assigns a different type to a variable when executing code (int to double/float in division to get exact numbers)
  • When going from float/double to int, the number is truncated (takes the numbers to the left of the decimal point as the answer)
// Set numbers to variables
int x = 7;
int y = 2;

// Print division of x and y w/o casting (will do floor division)
System.out.println(x/y);

// Print division of x and y w/ casting (will give exact answer)
System.out.println((float)x/(float)y);

// Can also work in the reverse order
float z = 3.7f;

// Printing "z" w/o casting
System.out.println(z);

// Printing "z" w/ casting as int
System.out.println((int)z);
3
3.5
3.7
3

Wrapper Class

  • Used to convert primitive data types to be used in things like ArrayLists
// assign value to variable
int x = 5;

// instantiate arraylist of ints
ArrayList<Integer> intList = new ArrayList<Integer>();

// create integer from int for wrapper
Integer a_wrapper = new Integer(x);
intList.add(x);

System.out.println(intList);
[5]

Unit 2: Using Objects

  • Objects are just an instance created out of a class created w/ a constructor (which takes in parameters describing the object
  • Methods in objects can be void (returns nothing) or have a return type specified
  • Static methods and properties are tied to class rather than object (ie. same value for all objects)
  • Methods can be overloaded (have different sets of parameters) as long as order of types differs between method definitions even with same name
  • Homework from student presentation

Concatenation

  • Combine strings using "+"
  • Use toString() for non string types
// assignment of variables
String x = "Hello ";
String y = "Hi ";
String z = "Howdy! ";
int a = 15;

// Concatenate all the strings together
System.out.println(x + y + z);

// When we concatenate int a to string z, it converts a to a string
System.out.println(z + a);
Hello Hi Howdy! 
Howdy! 15

Math Class

  • Can use for generating random number
  • Use for conducting more complex calculations than "+, -, *, /, %, or //"
int x = 10;

for (int n = 0; n < 3; n++) {
    int ran_num = (int)(Math.random() * x + 1);
    System.out.println(ran_num);
}
9
1
5

Comparing Numbers, Strings, and Objects

  • Numbers compared using ==
  • Objects compared using .equals()
int a = 1;
int b = 1;
int c = 2;
String x = "Howdy";
String y = "Howdy";
String z = "Hello";

System.out.println(a == b);
System.out.println(a == c);
System.out.println(x.equals(y));
System.out.println(x.equals(z));
true
false
true
false

Unit 3: Booleans and If Statements

Compound Boolean Expression

  • Compound boolean expressions = made up of many boolean expressions
boolean x = true;
boolean y = false;

// compound boolean expressions
boolean compound_boolean = !(x && y) || (x || y) && !(x || y);

// result
System.out.println(compound_boolean);
true

De Morgan's Law

  • !(a && b) = !a || !b and !(a || b) = !a && !b
boolean a = false;
boolean b = true;
boolean c = false;
boolean d = true;

// complicated boolean expression
boolean complicated = !((!(a && b)) || (!(a || b)));

// De Morgan's Law one
boolean simplified_one = !((!a || !b) || (!a && !b));

// De Morgan's Law two
boolean simplified_two = !(!a || !b) && !(!a && !b);

// print result (should all be same)
System.out.println(complicated + " " + simplified_one + " " + simplified_two);
false false false

Unit 4: Iteration

For loop

int[] array = {1, 2, 3, 4, 5};

// looping through even numbers
System.out.println("Loop of even numbers through ten");
for (int i = 0; i<10; i+=2) {
    System.out.print(i);
}

System.out.println("\nLooping through array");
// looping through array with for loop
for (int i = 0; i<array.length; i++) {
    System.out.print(array[i]);
}

System.out.println("\nLooping through array using enhanced loop");

// looping through array with enhanced for loop
for (int i : array) {
    System.out.print(i);
}
Loop of even numbers through ten
02468
Looping through array
12345
Looping through array using enhanced loop
12345

While Loop vs Do While Loop

  • While loops run while a condition is true, the condition is checked before each iteration of the code is run
  • Do while loops also run while a condition is true, but the condition is checked AFTER each iteration of the code is run
    • This means that no matter what the do block runs at least once before the condition is checked
int i = 0;
boolean falseBool = false;

// printing even numbers with while loop
while (i < 10) {
  System.out.println(i);
  i += 2;
}

// if condition is false while loop does not run at all
while (falseBool) {
  System.out.println("while loop");
}

// if condition is false in do while, the loop runs once
do {
  System.out.println("do-while loop");
} while (falseBool);
0
2
4
6
8
do-while loop

Nested Loops

  • Loops can be used inside each other for better iteration
  • Especially useful for 2D arrays
int[][] arr = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
  
// using nested for loops for 2D array
for (int i = 0; i<arr.length; i++) {
    for (int j = 0; j<arr[i].length; j++) {
        System.out.print(arr[i][j] + " ");
    }
    System.out.println();
}
1 2 3 
4 5 6 
7 8 9 

Unit 5: Writing Classes

  • Classes can be used for creating objects and have two main things: properties and methods
  • Properties are used to store information about each object of a class (can be made private/public which determines accessibility outside of class)
  • Methods are used to modify the object & do things
  • Getter and Setter Methods can be used to modify properties of a class which are made private
  • Homework from student presentation

Creating a Class

  • Defined with camelCase
// create a class
class HelloWorld {
    // code
}

Main Method

  • Used as tester method
class HelloWorld {
    public static void main (String[] args) {
        HelloWorld object = new HelloWorld();
    }
}

HelloWorld.main(null);

This

  • The "this" keyword allows you to access properties of the class
  • See constructor example to see use of this keyword

Constructor

  • Constructor is called whenver the object is created, usually initializes fields
  • Does not return anything because the object is automatically given to the user when constructor is called
class HelloWorld {
    int prop1;
    int prop2;

// Constructor here
    public HelloWorld (int prop1input, int prop2input) {
        // setting properties using this to reference prop1 & prop2 of the object
        this.prop1 = prop1input;
        this.prop2 = prop2input;
    }

    // tester method
    public static void main (String[] args) {
        HelloWorld object = new HelloWorld(50, 75);
    }
}

HelloWorld.main(null);

Getter Methods

  • Used to get properties of an object from the outside the class definition
  • Getters can be applied on only the properties which should be accessed outside the class
class HelloWorld {
    int prop1;
    int prop2;
  
    // Constructor here
    public HelloWorld (int prop1input, int prop2input) {
      // setting properties using this to reference prop1 & prop2 of the object
      this.prop1 = prop1input;
      this.prop2 = prop2input;
    }
  
    // getter allows outside class to access prop1
    public int getProp1() {
      return this.prop1;
    }
    
    // tester method
    public static void main (String[] args) {
      HelloWorld object = new HelloWorld(50, 75);
  
      // using getter to access prop1
      System.out.println(object.getProp1());
    }
  }
  
  HelloWorld.main(null);
50

Setter Methods

  • These methods allow the properties which should be modifiable to be changed outside the class definition
  • These methods have a "void" return type since they don't need to return anything, since they are only setting values
class HelloWorld {
  int prop1;
  int prop2;
  
    // Constructor here
    public HelloWorld (int prop1input, int prop2input) {
      // setting properties using this to reference prop1 & prop2 of the object
      this.prop1 = prop1input;
      this.prop2 = prop2input;
    }
  
    // getter allows outside class to access prop1
    public int getProp1() {
      return this.prop1;
    }
  
    // setter allows outside class to set prop1
    public void setProp1 (int propVal) {
      this.prop1 = propVal;
    }
  
    public static void main (String[] args) {
        HelloWorld object = new HelloWorld(50, 75);
  
      // using getter to access prop1
      System.out.println(object.getProp1());
  
      // changing value of prop1
      object.setProp1(10);
  
      // using getter to access new value of prop1
      System.out.println(object.getProp1());
    }
}
  
  HelloWorld.main(null);
50
10

Access Modifiers

  • Access modifiers control whether properties and methods can be accessed outside the class
  • The public means the property/method is accessible outside while if it is private it is not accessible
class HelloWorld {
    // prop1 cannot be directly accessed
    private int prop1;
  
    // prop2 can be directly accessed
    public int prop2;
  
    // Constructor here
    public HelloWorld (int prop1input, int prop2input) {
        // setting properties using this to reference prop1 & prop2 of the object
        this.prop1 = prop1input;
        this.prop2 = prop2input;
    }
  
    // getter allows outside class to access prop1
    public int getProp1() {
        return this.prop1;
    }
  
    // setter allows outside class to set prop1
    public void setProp1 (int propVal) {
        this.prop1 = propVal;
    }
  
    public static void main (String[] args) {
        HelloWorld object = new HelloWorld(50, 75);
  
        // would throw error
        // object.prop1 = 10;

        // works (because public)
        object.prop2 = 100;
        System.out.println(object.prop2);
    }
}
  
  HelloWorld.main(null);
100

Static Methods

  • Static properties and methods are part of the class rather than each object
  • Static methods do not require an object, and static properties only have one instance that is the same for all objects
class HelloWorld {
    // static method
    static String awesomeMethod (String a) {
        return a + " awesome";
    }
  
    // static property
    static int staticProp = 17;
  
    public static void main(String[] args) {
        // no object needed for any of this
        System.out.println(HelloWorld.awesomeMethod("method"));
        System.out.println(HelloWorld.staticProp);
    }
}
  
HelloWorld.main(null);
method awesome
17