• All code that runs must be in the main method
  • 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
  • To comment your code, use // for single line comments and /**/ for multiple lines
  1. In order to perform mathmatical calculations on integers and doubles, you can use operators
  2. Main ones are +, -, *, /
  3. Modulus is %, used to get remainder when two numbers are divided

Example

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

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)

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

Assignment Operators

  • += 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

Casting: "When you assign a value of one primitive type to another type." You can use it to round a number or truncuate it

Casting Example

int myInt = 9;
 double myDouble = myInt; // Automatic casting: int to double
  
System.out.println(myInt);      // Outputs 9
System.out.println(myDouble);   // Outputs 9.0
9
9.0

Trunacte: to trim some digits of a float or double-type number or some characters of a string from the right

Truncate Example

import java.lang.Math;

public class Program {
    public static void main(String[] args) {
        double test1 = 1.234;
        double test2 = -1.567;
        
        // ... Math.floor will always go towards negative infinity.
        System.out.println(Math.floor(test1));
        System.out.println(Math.floor(test2));
        
        // ... Math.round may go towards negative or positive infinity.
        System.out.println(Math.round(test1));
        System.out.println(Math.round(test2));
        
        // ... Casting to int will remove the fractional part.
        // This truncates the number.
        System.out.println((int) test1);
        System.out.println((int) test2);
    }
}

Homework

Hack 1.1

public class Printing {
    public static void main(String[] args){
        System.out.println("Kian Pasokhi");
        System.out.println("Period 2");
}  
}
Printing.main(null);
Kian Pasokhi
Period 2

Hack 1.2

public class Biodata {

    public static void main(String[] args) {
        String name = "Kian Pasokhi";
        int age = 17;
        boolean underclassman = false;
        double height = 5.7;

        System.out.println(name);
        System.out.println(age);
        System.out.println(underclassman);
        System.out.println(height);
    }
}

Biodata.main(null);
Kian Pasokhi
17
false
5.7

Hack 1.3

public class Num {

    public static void main(String[] args) {
    double num = ( ( 123 * 15003 ) / 11345 );
    System.out.println(num);

}
}

Num.main(null);
162.0

Hack 1.4

public class Operators {

    public static void main(String[] args) {
        long num = 23948294;
        num %= 4;
        System.out.println("Remainder when divided by 4 is " + num);
    }
}

Operators.main(null);
Remainder when divided by 4 is 2

Hack 1.5

public class CastActivity {

    public static void main(String[] args) {
        double dNum = 123456.123456;
        int dInt = (int) dNum;
        System.out.println("the double to the integer is: " + dInt);

    }
}

CastActivity.main(null);
the double to the integer is: 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);

GPA Calculator

import java.util.Scanner;
import java.lang.Math;

public class GradeCalculator {

    private String category;
    private double current;
    private double percent;
    private double desired;
    private double test;

    private double percent_test;
    private double current_test;
    private int points_test;
    private int points_final;

    public double calculate() {
        this.enterCat();

        if (category.equals("separate")) {
            this.enterVals();
            // current * (1-percent) + final * percent = desired
            test = (desired - (current * (1-percent/100))) / (percent/100); // Ex. 95 - (90 * (1 - .20)) / .20
        }

        else if (category.equals("tests")) {
            this.enterPoints();
            // current_points = current * 100 - percent_test * points_test
            // percent_test * (points_test + points_final) + (1-percent_test) * (current_points) = desired
            percent_test /= 100;
            int current_points = (int)current - (int)(percent_test) * points_test;
            test = ((desired - ((1-percent_test)*current_points)/percent_test) - points_test);
        }

        return test;
    }

    private void enterCat() {
        while (true) {
            Scanner dd = new Scanner(System.in);
            System.out.print("Enter final category (separate/tests): ");
            try {
                category = dd.nextLine();
                System.out.println(category);
                break;

            } catch (Exception e) { // catch non-numerical input
                System.out.println("Not a string, " + e);
            }
            dd.close();
        }
    }

    private void enterVals() {
        while (true) {
            Scanner dd2 = new Scanner(System.in);
            try {
                System.out.print("Current grade: ");
                current = dd2.nextDouble();
                System.out.println(current);

                System.out.print("Percentage of grade that is final: ");
                percent = dd2.nextDouble();
                System.out.println(percent);

                System.out.print("Desired grade: ");
                desired = dd2.nextDouble();
                System.out.println(desired);

                break;
            } catch (Exception e) { // catch non-numerical input
                System.out.println(current + "not a double, " + e);
            }
            dd2.close();
        }
    }

    private void enterPoints() {
        while (true) {
            Scanner dd3 = new Scanner(System.in);
            try {
                System.out.print("Current grade: ");
                current = dd3.nextDouble();
                System.out.println(current);

                System.out.print("Percentage of grade that is tests: ");
                percent = dd3.nextDouble();
                System.out.println(percent_test);
                
                System.out.print("Current percent in tests: ");
                current_test = dd3.nextDouble();
                System.out.println(current_test);
                
                System.out.print("Current amount of points in tests: ");
                points_test = dd3.nextInt();
                System.out.println(points_test);

                System.out.print("Amount of points in final: ");
                points_final = dd3.nextInt();
                System.out.println(points_final);

                System.out.print("Desired grade: ");
                desired = dd3.nextDouble();
                System.out.println(desired);

                break;
            } catch (Exception e) { // catch non-numerical input
                System.out.println(current + "not a double, " + e);
            }
            dd3.close();
        }
    }

    public static void main(String[] args) {    // main method
        GradeCalculator calc = new GradeCalculator();
        System.out.println("You need a " + String.format("%.2f", calc.calculate()) + " on the test");
    }
}

GradeCalculator.main(null);
Enter final category (separate/tests): separate
Current grade: 90.0
Percentage of grade that is final: 50.0
Desired grade: 20.0
You need a -50.00 on the test