Score: 0.9/1.0

Why to use Java? (1.1)

Java is an important programming language that is widely used throughout the tech industry and contains many important programmming concepts such as object oriented programming.

Basic Java (1.1)

  • All code that runs must be in the main method (shown below)
  • To print something, use System.out.print(); and inside the parenthesis put text in quotes (String)
    • To print with a new line, use System.out.println();
  • To comment your code, use // for single line comments and /**/ for multiple lines (example will be shown below)
    • It is important to describe how and why your code works, however dont describe obvious things
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!

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("Jazair Tallman");
        System.out.println("JEHB");
    }
}

Printing.main(null);
Jazair Tallman
JEHB

List of Data Types (1.2)

  • Data types are different categories in which one can store various types of data.
  • The main Primitve data types are:
    • Integer (int): used for whole numbers
    • Double (double): used for numbers with decimals
    • Boolean (boolean): used for true or false conditionals
  • For Primitive types, variables store actual data instead of reference
  • If the variable is declared final, it cannot be edited
  • A non Primitive type which is commonly used is String
    • Stores text
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 = "Jazair Tallman";
        int age = 17;
        boolean underclassmen = false;
        double height = 5.75;

        System.out.println("My name is " + name + " and I am " + age + " years old.");
        System.out.println(underclassmen);
        System.out.println(height);
        
    }
}

Biodata.main(null);
My name is Jazair Tallman and I am 17 years old.
false
5.75

Operators (1.3)

  • In order to perform mathmatical calculations on integers and doubles, you can use operators
  • Main ones are +, -, *, /
    • These are what you expect
    • When dividing integers, it always rounds down because output must be an integer
    • When dividing by 0, will get the ArithemticException Error
  • Modulus is %, used to get remainder when two numbers are divided
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) {
        int num1 = 6 * 1234124;
        int num2 = num1 % 11345;
        int num3 = num2 - 890809 + 90800;
        int num = num3 / 980098;
        System.out.println(num / 100);
    }
}

Num.main(null);
0

Assignment operators (1.4)

  • += adds value of a variabe to another variable and assigns total value to first variable
  • -= subtracts value of a variabe to another variable and assigns total value to first variable
  • *= multiplies value of a variabe to another variable and assigns total value to first variable
  • /= multiplies value of a variabe to another variable and assigns total value to first variable
  • %= takes the remainder of a variable with a second variable and assigns remainder to first variable
  • ++ increments a variable by 1, to incrememt by more change second plus to number which you want to incrememnt by
  • -- subracts a variable by 1, to incrememt by more change second plus to number which you want to subtract by

Hack 1.4

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

    public static void main(String[] args) {
        
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
       
    }
}

Operators.main(null);
0
1
2
3
4
5
6
7
8
9

Casting and Ranges (1.5)

  • Doubles and Integers can be converted to each other using (int) or (double)
    • When converting from doubles to integers, will round down
  • Integers are 4 bytes of data, can store between Integer.MAX_VALUE and Integer.MIN_VALUE
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 a = 123456.123456;
        int b = (int)a;
        
        System.out.println(a);
        System.out.println(b);
        
    }
}

CastActivity.main(null);
123456.123456
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);