Casting

  • Casting is when you convert from one data type to another
  • In this code example, variables x and y are changed from type int to type double
int x;
int y;
double casting1;
double casting2;


x = 10;
y = 15;

casting1 = (double) y / x;

casting2 = y / x;

System.out.printf("With casting: %.2f\nWithout casting: %.2f", casting1, casting2);
With casting: 1.50
Without casting: 1.00
java.io.PrintStream@32478ad1

Wrapper classes

  • In this code example, the value of num is wrapped to type Integer which allows for addition methods that wouldn't be possible with type int
  • num is now an object
Integer num = new Integer(5);

System.out.printf("int: %d\n", num);

num.toString();

System.out.printf("string: \"%s\"", num);
int: 5
string: "5"
java.io.PrintStream@67888624

Concatenation

  • Combining two strings into one
  • In the code example, message1 is being added to message2
  • num is an integer, so when it is concatenated to finalMessage2, it is turned into type String
String message1;
String message2;
int num;
String finalMessage;
String finalMessage2;

message1 = "Hello";
message2 = "World";
num = 5;

finalMessage = message1 + message2;
finalMessage2 = message1 + message2 + num;

System.out.printf("%s\n%s", finalMessage, finalMessage2);
HelloWorld
HelloWorld5
java.io.PrintStream@1238da

Math Class

  • Math.random returns a random double between 0 and 1
double x;
double y;
double randomX;
double randomY;

x = 5;
y = 12;

randomX = Math.random() * x;
randomY = Math.random() * y;

System.out.printf("%.2f\n%.2f", randomX, randomY);
1.03
10.81
java.io.PrintStream@32478ad1

Boolean Expressions

  • AND, OR, NOT, etc.
  • In the class GreatestNumber below, 3 numbers are being compared to see which one is the largest

Comparing Numbers

  • when comparing int, <, >, <=, >=, ==, and != can be used
public class GreatestNumber
{
    public static boolean num1Largest(int num1, int num2, int num3)
    {
        // num1 is the greatest
        if ((num2 > num3) && (num1 > num2))
        {
            return true;
        }
        return false;
    }

    public static boolean num2Largest(int num1, int num2, int num3)
    {
        // num2 is the greatest
        if (((num1 > num3) && (num2 > num1)) || ((num3 > num1) && (num3 < num2)))
        {
            return true;
        }
        return false;
    }

    public static boolean num3Largest(int num1, int num2, int num3)
    {
        // num3 is the greatest
        if((num1 < num2) && (num2 < num3))
        {
            return true;
        }
        return false;
    }

    public static void main (String[] Args)
    {
        int num1 = 10;
        int num2 = 15;
        int num3 = 5;
        boolean num1Largest;
        boolean num2Largest;
        boolean num3Largest;

        num1Largest = num1Largest(num1, num2, num3);
        num2Largest = num2Largest(num1, num2, num3);
        num3Largest = num3Largest(num1, num2, num3);

        System.out.printf("%d, %d, %d\nnum1 is the largest: %b\nnum2 is the largest: %b\nnum3 is the largest: %b\n",
        num1, num2, num3, num1Largest, num2Largest, num3Largest);
    }
}
GreatestNumber.main(null);
10, 15, 5
num1 is the largest: false
num2 is the largest: true
num3 is the largest: false

Truth Tables and De Morgan's Law

LINK

Comparing Strings & Comparing Objects

  • you must use .equals() method to compare Strings
  • If you use ==, you are comparing if two String instances/objects are the same

For Loop & Enhanced For Loop

  • In this code example, the regular for loop iterates through every even element of nums and prints it
  • Int the enhanced for loop, i is iterating through every element in nums
int[] nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// regular
for(int i = 0; i < nums.length; i+=2)
{
    System.out.printf("%d\n", nums[i]);
}

System.out.printf("\n");

// enhanced
for(int i : nums)
{
    System.out.printf("%d\n", i);
}
0
2
4
6
8

0
1
2
3
4
5
6
7
8
9

While Loops VS Do-While Loops

  • Do-While loops executes code at least once
  • While loops checks condition before code is executed

LINK

Nested Loops

  • In Tic-Tac-Toe, the board is a 2D-Array which is initialized by two for loops iterating through each column and each row

Creating Classes

  • Conventions are rules to help make code readable
  • Class names are capitalized
  • if you put a _ in front of class member variables, they tell us that they are members of the class and not local varibles

Constructors

  • Constuctors should never fail, the object is not fully created if it fails
  • No return type means success/failure or returning a restult

Setter/Getter Methods

  • setter - resets the value of a private variable
  • getter - returns the value of a private variable
// getter
private double getGPA(double gpa)
{
    return gpa;
}

// setter
private String name(String name)
{
    String student = "";

    name = student;

    return student;
}

Access Modifiers

  • Public - accessible everywhere
  • Private - accessible within the same class
  • Protected - accessible by the same package and subslasses

LINK

Static VS Non Static Methods

  • A static method is a method that does not require an object to be called
  • A non static method requires an object to be created in order to access the method

LINK

.this Keyword

  • In Java, everything is passed my reference
  • .this is a reference to the most recent object inside a method/constructor

LINK

  • In the first code segment, .this is used to initialize secret inside the WordMatch constructor

Main/Tester Methods

  • Tests different methods
  • First method that runs
  • Can be placed in any class
  • public static void main (String[] Args)

Inheritance/Extends/Subclass

  • In this code example, TicTacToeComputer is a subclass to TicTacToePlayer

LINK

Overloading a Method

  • Allows you to have a class with more than one method wiht the same name, as long as there are...
    • Different number of parameters
    • Different types of parameters

Overriding a Method

  • Subclass has access to the same methods as the parent class
  • EX: subclass Boy has parent class named Human

Abstract Class/Methods

  • When a class is declared as abstract, it gives implementations for all of the parent classes' abstract methods
  • An abstract method does not have any implementation
  • Never be final because the class must implement all abstract methods

Standard Methods

  • .toString() - converts an object to type String
  • .equals() - compares two strings (< and > for Java)
  • .hashCode() - returns the number of given input objects
    • Returns the hash value of an object

Late Binging of Object

  • Method is not called by the Compiler - overriding
    • both parent and child classes have the same methods

Polymorphism

  • Classes can provide a different implementation for a method
    • depends on type of object that is passed
  • EX: Person who has different characteristics at the same time
  • A way to sort an array
  • Faster than a linear search
    • uses "divide and conquer" method of sorting
  • Repeatedly divide array in half and sort
    • Eventually, every element will be sorted
  • Can use iteration or recursion