Casting, specifically for Division

  • Change a variable’s type, so for example changing an int to a double or vice versa
int num1 = 50;
int num2 = 56;
double round;
double intaverage = num1 / num2;
double doubaverage = (double)num1 / num2;
System.out.println("dividing with integer(also truncating): " + intaverage);
System.out.println("dividing with double: " + doubaverage);
dividing with integer(also truncating): 0.0
dividing with double: 0.8928571428571429

Casting, specifically for Truncating or Rounding

  • Truncate by changing a double to an int, this removes everything after the decimal
  • A trick to round with casting is to change a double to an int, but before doing this, add .5 if a positive integer and subtract .5 if a negative integer

Wrapper Classes, why wrap int, double. Show examples

  • Wrapper classes allow converting primitives into objects and vice versa, which allows java to be object oriented, as primitives aren’t an object by default
  • Someone may use a wrapper class on int to convert it to an object as they contain many methods to deal with an int value like converting it to a string representation, this is the same for doubles

Concatenation

  • Concatenation is combining 2+ strings to make a string using either the + or += operator or the concat() method
  • concat() is immutable, so you need to set it to a string so a.concat() wouldn't work, but a = a.concat() would
  • to concatenate multiple data types, you need to use + and not concat method, os this would allow concating a string and a integer for example
public class Main{  
    public static void main(String args[]){  
    int a=20;  
    Integer i=Integer.valueOf(a);//converting int into Integer  
    String string = i.toString(); //converting the Integer to a string, this doesn't work with the primitive
    String string1 = " 2";
    String string2 = string.concat(string1); //using concat
    String otherdata = string+a;
    System.out.println(a+" "+i);  
    System.out.println("concat 20 and 2 together: "  +" " +string2);
    System.out.println("concat a string and integer: "  +" " +otherdata);
    }}  
Main.main(null);
20 20
concat 20 and 2 together:  20 2
concat a string and integer:  2020

Math Class

  • java math class provides many math methods that allow finding different things, such as a max or min function
  • math.random will return a number between 0.0 and 1.0, or you can multiply it to get 1 to 100 (seen in code)
for (int i = 0; i <5; i++) {
    int randomNum = (int)(Math.random() * 101);
    System.out.println(randomNum);
}
6
46
11
25
93

Compound Boolean Expression

  • this includes using the &&, ||, and ! operators,
public class TestNum
{
   public static void main(String[] args)
   {
     int score = 10; 
     if (score < 0 || score > 100)
     {
         System.out.println("Score has an illegal value.");
     }
     if (score >= 0 && score <= 100)
     {
         System.out.println("Score is in the range 0-100");
     }
     if (score != 10) {
        System.out.println("pick a different score");
     }

   }
}
TestNum.main(null)
Score is in the range 0-100

Truth Tables

  • define boolean function by choosing value for each possible value of arguments, use truth table to do so

truthtable

De Morgan's Law

  • the complement of the union of two sets A and B is equal to the intersection of the complement of the sets A and B
  • !(a && b) is equivalent to !a || !b
  • !(a || b) is equivalent to !a && !b
if (!(true && false) == ((!true)||(!false))) {
    System.out.print("these statements are equivalent");
}
else {
    System.out.print("not the same");
}

Comparing Numbers

  • can compare numbers using the equals method
  • can compare numbers using compare method
  • operators such as <, >, == can be used
Integer x = 0;
Integer y = 0;
if(x.equals(y)) {
            System.out.println("Both x and y are equal");
}
else {
            System.out.println("x and y are not equal");
}

int comp = Integer.compare(x, y);
if(comp>0) {
    System.out.println("x is greater than y");
}else if (comp<0){
    System.out.println("x is less than y");
}else{
     System.out.println("x and y are equal");
}
Both x and y are equal
x and y are equal

Comparing Strings

  • can use equals method to compare two strings based on content in string
  • equalsIgnoreCase method compares two strings no matter the case
String string = "Sdsad";
String string1 = "sdsad";
System.out.println(string.equals(string1));
System.out.println(string.equalsIgnoreCase(string1));
false
true

Comparing Objects

  • equals and hashCode methods can be used to compare objects
  • equals method compares equality of two objects
import java.io.*;
 
class Pet {

    String name;
    int age;
    String breed;
 

    Pet(String name, int age, String breed)
    {
        this.name = name;
        this.age = age;
        this.breed = breed;
    }
}

public class Main {
 
    public static void main(String args[])
    {
 
        Pet dog1 = new Pet("Snow", 3, "German Shepherd");
        Pet cat = new Pet("Jack", 2, "Tabby");
        Pet dog2 = new Pet("Snow", 3, "German Shepherd");
 
        System.out.println(dog1.equals(dog2));
    }
}

Main.main(null);
false

for loop, enhanced loop

  • enhanced for loops are easier to use and errors will usually happen less, as you don't have to manage steps by themselves, but for loops allows you to control everything about looping
  • for loops you can change by how many steps it iterates and if it goes backwards or forwards
  • enhanced for loops will just go forward and increment its steps by 1

while loop versus do while loop

  • while loops are loos that keep continuing until a boolean isn't met anymore
  • do while loops are the same as while loops except for the loop checking for the conditions after checking a statement

nested loops

  • a nested loop is a loop inside of another loop
class Main {
    public static void main(String[] args) {
  
      int weeks = 3;
      int days = 7;
  
      
      for (int i = 1; i <= weeks; ++i) {
        System.out.println("Week: " + i);
  
  
        for (int j = 1; j <= days; ++j) {
          System.out.println("  Day: " + j);
        }
      }
    }
  }
  Main.main(null);
Week: 1
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7
Week: 2
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7
Week: 3
  Day: 1
  Day: 2
  Day: 3
  Day: 4
  Day: 5
  Day: 6
  Day: 7

Creating a Class

  • a class is an object constructor or something used to make objects
  • use the keyword class to create a class
  • classes can have more than one class, which is called a nested class
  • for the outside class, only public, abstract, strictfp, and final can be used to modify class, but inside, private, protected, and static can be used