Why Java

  • Java syntax easier compared to other languages
  • focuses on classes, objects, and methods
  • easy connection with APIs
  • once compiled, it will run on any machine

Anatomy

  • main.java is run by the java compiler
  • classname.java are names for the java files, public allows being called from other classes
  • {} is the stuff inside the class, static means that instance of main class isn't needed, void doesn't return to another function

Primitive Data Types

  • int: integer number
  • double: decimal number up to 15 decimal places
  • boolean: true or false

Variables

  • to initialize variable: (scope) dataType variableName = dataValue
  • private only allowed in a class
  • public any class can access
  • protected allowed to all classes in package and any subclass
  • variables known as constants don't change

Basic Math

  • equal sign is assignment operator, can be used to change the value of the variable
  • modulo operator is % (a % b, which is integer remainder of a divided by b)

Compound Assignment Operators

  • operators to change numbers (ie: integer += 1;)
  • *=,+=,-=,/=,%=,i++,i--

Vocab

Casting, specifically for Division

  • Change a variable’s type, so for example changing an int to a double or vice versa

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
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

Constructor - A constructor initializes an object, a constructor has no return value because it is not actually used by the code, instead it simply initializes the object.

public class Customer { //Intializing class Customer in this case with a constructor - no return value

Accessor methods - AKA “getters”, return the values in a defined class/reads the value, it is usually named get + Class Name

public String getName(){
    return name;
};

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
public class Example {

    public static void main(String[] args) {

        System.out.print("Hello World");  
        System.out.print(" From Team Oops!");
        System.out.println(); //done to separate two different lines 
        System.out.println("Welcome to our presentation!");
        System.out.print("We hope you learn something from it!");

    }
}

Example.main(null);
Hello World From Team Oops!
Welcome to our presentation!
We hope you learn something from it!

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

Big O notation for Hash map, Binary Search, Single loop, Nested Loop Good for determining time efficiency in a Java code, good for reducing and improving time.

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);
}
82
99
73
76
5

Hack 1.1

Print your name and your team name in separate lines!

public class Printing {
    public static void main(String[] args) {
        System.out.println("Samuel");  
        System.out.println("Team 10"); //simple printing
    }
}

Printing.main(null);
Samuel
Team 10
public class Example {

    public static void main(String[] args) {

        int Herbo = 10;
        double gasPrices = 7.99;
        final boolean Hot = true;
        String name = "Team Oops is hot:";

        System.out.println(Herbo);
        System.out.println(gasPrices);
        System.out.println(name + Hot);

        // Hot = false; cannot assign a value to final variable Hot 

    }
}

Example.main(null);
10
7.99
Team Oops is hot:true

Hack 1.2

Create variables for your biodata (name, age, underclassmen or not, height in feet)

public class Biodata {

    public static void main(String[] args) {
        String name = "Samuel";
        int age = 17;
        boolean underclassman = false; //making variables and printing them
        int height = 6;
        System.out.println(name);
        System.out.println(age);
        System.out.println(underclassman);
        System.out.println(height);

    }
}

Biodata.main(null);
Samuel
17
false
6
public class Math {

    public static void main(String[] args) {

        int number = 2;
        int number2 = 5;
        double number3 = 2.0;
        double number4 = 5.0;

        System.out.println(number+number2);
        System.out.println(number3+number4);
        System.out.println(number-number2);
        System.out.println(number3-number4);
        System.out.println(number * number2);
        System.out.println(number3 * number4);
        System.out.println(number/number2);
        System.out.println(number3/number4);
        System.out.println(number4 % number3);
        System.out.println(number2 % number);
    }
}

Math.main(null);
7
7.0
-3
-3.0
10
10.0
0
0.4
1.0
1

Hack 1.3

  • Compute the remainder of 6 multiplied by 1234124 divided by 11345 minus 890809 plus 90800 (use order of operations) is divided by 980098, and store this in a variable called num (get an exact number as opposed to a whole number)
  • Divide num by 100
  • Print num
public class Num {

    public static void main(String[] args) {
        Double num = (((6.0*1234124.0)/(11345.0-890809.0+90800.0))%980098.0); //the math function, using parentheses to make sure pemdas works well
        num = num/100;
        System.out.println(num);
    }
}

Num.main(null);
-0.09388971729405679

Hack 1.4

  • Create a code which performs mathmatical calculations with assignment operators!
public class Operators {

    public static void main(String[] args) {
        double n = 1;
        double b = 2;
        System.out.println(n+=b); // a bunch of diferent math functions
        System.out.println(n-=b);
        System.out.println(n*=b);
        System.out.println(n/=b);
        System.out.println(n%=b);
        System.out.println(n++);
        System.out.println(n--);
    }
}

Operators.main(null);
3.0
1.0
2.0
1.0
1.0
1.0
2.0
public class Cast {

    public static void main(String[] args) {
        double num = 10.5;
        int num2 = 100;
        int numInt = (int)num;
        double num2Double = (double)num2;

        System.out.println(num);
        System.out.println(num2);
        System.out.println(numInt);
        System.out.println(num2Double);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);


    }
}

Cast.main(null);
10.5
100
10
100.0
2147483647
-2147483648

Hack 1.5

  • Convert 123456.123456 into an integer
  • Set 678901234567890 into an integer (what do you think will happen?)
public class CastActivity {

    public static void main(String[] args) {
        double data = 123456.123456;
        int value = (int)data; //using casting to truncate
        System.out.println(value);
        
        // int num = 678901234567890;
        // System.out.println(num); this turns out to be an error

    }
}

CastActivity.main(null);
123456

Code Example!

public class Main {
    public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("What is your name?");
      String name = sc.next(); //string
      System.out.println(name);
      System.out.println("How many pizzas do you want to buy?");
      int pizzas = sc.nextInt(); //integer
      System.out.println(pizzas);
      System.out.println("Do you have the discount (true/false)?");
      boolean hasDiscount = sc.nextBoolean(); //boolean
      System.out.println(hasDiscount);
  
      double price; //double, defaults to 0
      if (hasDiscount) {
        price = 1.20;
      } else {
        price = 2.10;
      }
  
      char firstChar = name.charAt(0); //character
      double finalPrice = price * pizzas * 1.08; // adding taxes
  
      System.out.println("Hi " + firstChar + "! You have to pay " + (finalPrice) + " dollars.");
    }
  }
  
  Main.main(null);
What is your name?
Kinish
How many pizzas do you want to buy?
6
Do you have the discount (true/false)?
true
Hi K! You have to pay 7.776 dollars.