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("Linda"); 
    System.out.println("Divya"); 
    System.out.println("Valshavi"); 
    System.out.println("Rebecca"); 
    }
}

Printing.main(null);
Linda
Divya
Valshavi
Rebecca

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 = "Linda";
        int age = 15;
        boolean underclassmen = false;
        int height = 163; //in cm

    }
}

Biodata.main(null);

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

    }
}

Num.main(null);

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

        int b = 0;
        System.out.println(b+=3);
    }
}

Operators.main(null);
3

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

Casting

1 / 3 - return 0 division with integers - truncate and throw away the part after the decimal point

1.0 / 3 - return 0.33333333333333 a mixture of integers (int) and decimal (double) numbers, return a double result

if want to return a integer -> casting

double can be rounded to the nearest integer by adding or subtracting .5 and casting with (int) using formulas

int nearestInt = (int)(number + 0.5); int nearestNegInt = (int)(negNumber – 0.5);

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 num = 123456.123456;
        int numInt = (int)num;
        System.out.println(numInt);

    }
}

CastActivity.main(null);
123456
int num2 = 678901234567890;
System.out.println(num2);
//interger number too large
|   int num2 = 678901234567890;
integer number too large

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