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!
public class PrimitiveDivision {
    public static void main(String[] args) {
        int i1 = 7, i2 = 2;
        System.out.println("Integer Division");
        System.out.println("\tint output with concatenation: " + i1 + "/" + i2 + " = " + i1/i2);
        System.out.println(String.format("\tint output with format: %d/%d = %d",i1, i2, i1/i2));
        System.out.printf("\tint output with printf: %d/%d = %d\n",i1, i2, i1/i2);

        double d1 = 7, d2 = 2;
        System.out.println("Double Division");
        System.out.println("\tdouble output with concatenation: " + d1 + "/" + d2 + " = " + d1/d2);
        System.out.println(String.format("\tdouble output with format: %.2f/%.2f = %.2f",d1, d2, d1/d2));
        System.out.printf("\tdouble output with printf: %.2f/%.2f = %.2f\n",d1, d2, d1/d2);

        System.out.println("Casting and Remainders");
        System.out.printf("\tint cast to double on division: %d/%d = %.2f\n",i1, i2, i1/(double)i2);
        System.out.println("\tint using modulo for remainder: " + i1 + "/" + i2 + " = " + i1/i2 + " remainder " + i1%i2);
    }
}
PrimitiveDivision.main(null);
Integer Division
	int output with concatenation: 7/2 = 3
	int output with format: 7/2 = 3
	int output with printf: 7/2 = 3
Double Division
	double output with concatenation: 7.0/2.0 = 3.5
	double output with format: 7.00/2.00 = 3.50
	double output with printf: 7.00/2.00 = 3.50
Casting and Remainders
	int cast to double on division: 7/2 = 3.50
	int using modulo for remainder: 7/2 = 3 remainder 1
// 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("This is not an integer");
        }
        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: 7.9
Enter a boolean: true
Enter a String: false
  • Integer - a number value non negative and not a decimal
  • Double - A number value with a decimal
  • Boolean - True or False values that are usually used in if statements
  • string - Any character that the computer can print

GPA Calculator

public class GPACalculator{
    // introduction to Double wrapper class (object)
    ArrayList<Double> grades;   // similar to Python list

    // constructor, initializes ArrayList and call enterGrades method
    public GPACalculator() {
        this.grades = new ArrayList<>();
        this.enterGrades();
    }


    // enterGrades input method using scanner
    double average = 0;
    private double enterGrades() {
        Scanner input;

        input = new Scanner(System.in);
        System.out.print("How many classes have you taken: \n");
        try {
            double classNum = input.nextInt();
            for (int i = 0; i < classNum; i++) {
                input = new Scanner(System.in);
                System.out.print("Enter your credits for each class (1-5): ");
                try {
                    double sampleInputDouble = input.nextDouble();
                    System.out.println(sampleInputDouble); 
                    this.grades.add(sampleInputDouble);
                } catch (Exception e) {  // if not a number
                    System.out.println("Not an double (form like 9.99), " + e);
                }
                input.close();
            }
            double total = 0;   // running total
            for (double num : this.grades) {    // enhanced for loop
                total += num;   // shortcut add and assign operator
        }
            average = total/ classNum;
        } catch (Exception e) {  // if not an integer
            System.out.println("This is not an integer");
        }
        
        return average;  // double math, ArrayList grades object maintains its size

    }

    // average calculation 
    public double average() {
        return (average);  // double math, ArrayList grades object maintains its size
    }

    public static void main(String[] args) {
        GPACalculator grades = new GPACalculator(); // calls constructor, creates object, which calls enterGrades
        System.out.println("Average: " + String.format("%.2f", grades.average()));  // format used to standardize to two decimal points
    }
}
// IJava activation
GPACalculator.main(null);
How many classes have you taken: 
Enter your credits for each class (1-5): 5.0
Enter your credits for each class (1-5): 4.0
Enter your credits for each class (1-5): 5.0
Enter your credits for each class (1-5): 4.0
Average: 4.50