• Terminal for testing and runtime
  • Run button > JavaC > Bytecode > Java runs
  • Lots of data types
  • Primitives (booleans, characters) are fixed (defined by the language)
  • Non-primitives (wrapper classes) store functions (methods) and objects
  • Examples: wrappers are uppercase, primitives are lowercase

Code.org learnings

  • Format is myObject.function();
  • Classes must be capitalized
  • Create new instance with ClassName objectName = new ClassName();
public class DefinePrimitives {
  public static void main(String[] args) {
    int anInt = 100;
    double aDouble = 89.9;
    boolean aBoolean = true;

    // not primitives but essential
    String aString = "Hello, World!";   // wrapper class shortcut assignment
    String aStringFormal = new String("Greetings, World!");

    System.out.println("anInt: " + anInt);
    System.out.println("aDouble: " + aDouble);
    System.out.println("aBoolean: " + aBoolean);
    System.out.println("aString: " + aString);
    System.out.println("aStringFormal: " + aStringFormal);
  }
}
DefinePrimitives.main(null)
anInt: 100
aDouble: 89.9
aBoolean: true
aString: Hello, World!
aStringFormal: Greetings, World!
// java style to import library
import java.util.Scanner;

// class must alway have 1st letter as uppercase, CamelCase is Java Class convention
public class ScanPrimitives {
    public static void main(String[] args) {    
        Scanner input;

        // primitive int
        input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        try {
            int sampleInputInt = input.nextInt();
            System.out.println(sampleInputInt);
        } catch (Exception e) {  // if not an integer
            System.out.println("Not an integer (form like 159), " + e);
        }
        input.close();

        // primitive double
        input = new Scanner(System.in);
        System.out.print("Enter a double: ");
        try {
            double sampleInputDouble = input.nextDouble();
            System.out.println(sampleInputDouble);
        } catch (Exception e) {  // if not a number
            System.out.println("Not an double (form like 9.99), " + e);
        }
        input.close();

        // primitive boolean
        input =  new Scanner(System.in);
        System.out.print("Enter a boolean: ");
        try {
            boolean sampleInputBoolean = input.nextBoolean();
            System.out.println(sampleInputBoolean);
        } catch (Exception e) {  // if not true or false
            System.out.println("Not an boolean (true or false), " + e);
        }
        input.close();

        // wrapper class String
        input =  new Scanner(System.in);
        System.out.print("Enter a String: ");
        try {
            String sampleInputString = input.nextLine();
            System.out.println(sampleInputString);
        } catch (Exception e) { // this may never happen
            System.out.println("Not an String, " + e);
        }
        input.close();
    }
}
ScanPrimitives.main(null);
Enter an integer: Not an integer (form like 159), java.util.InputMismatchException
Enter a double: 22.2
Enter a boolean: false
Enter a String: Hello World

Calculator function

This is a simple function that uses the Scanner class to take user input. It uses double and char primitives to store them and the calculation result.

import java.util.Scanner;
// uses scanner to take input
public class Calculator{
  public static void main(String[] args) {
    //setting variables
    char operator;
    Double number1, number2, result;

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // taking user input
    System.out.println("Choose an operator: +, -, *, or /");
    operator = input.next().charAt(0);

    System.out.println("Enter first number");
    number1 = input.nextDouble();

    System.out.println("Enter second number");
    number2 = input.nextDouble();

    switch (operator) {
      // addition
      case '+':
        result = number1 + number2;
        System.out.println(number1 + " + " + number2 + " = " + result);
        break;

      // subtraction
      case '-':
        result = number1 - number2;
        System.out.println(number1 + " - " + number2 + " = " + result);
        break;

      // multiplication
      case '*':
        result = number1 * number2;
        System.out.println(number1 + " * " + number2 + " = " + result);
        break;

      // division
      case '/':
        result = number1 / number2;
        System.out.println(number1 + " / " + number2 + " = " + result);
        break;
      // error handling
      default:
        System.out.println("Invalid operator!");
        break;
    }

    input.close();
  }
}
// calling function
Calculator.main(null);
Choose an operator: +, -, *, or /
Enter first number
Enter second number
25.0 / 3.0 = 8.333333333333334