IJava Kernel Notebook
Data Types Primitive: Boolean (boolean) and Numeric {Character (char), Integral {Integer (byte, short, int, long)}, Floating Point {float, double}}
Non-Primitive: String, Array etc.
// Here's an example of a Grade Calculator, through the utilization of Primitives, created by Mr. Mort (for reference)
public class GradeCalculator {
// introduction to Double wrapper class (object)
ArrayList<Double> grades; // similar to Python list
// constructor, initializes ArrayList and call enterGrades method
public GradeCalculator() {
this.grades = new ArrayList<>();
this.enterGrades();
}
// double requires test for zero versus threshold, DO NOT compare to Zero
private boolean isZero(double value){
double threshold = 0.001;
return value >= -threshold && value <= threshold;
}
// enterGrades input method using scanner
private void enterGrades() {
Scanner input;
while (true) {
input = new Scanner(System.in);
System.out.print("Enter a double, 0 to exit: ");
try {
double sampleInputDouble = input.nextDouble();
System.out.println(sampleInputDouble);
if (isZero(sampleInputDouble)) break; // exit loop on isZero
else this.grades.add(sampleInputDouble); // adding to object, ArrayList grades
} catch (Exception e) { // if not a number
System.out.println("Not an double (form like 9.99), " + e);
}
input.close();
}
}
// average calculation
public double average() {
double total = 0; // running total
for (double num : this.grades) { // enhanced for loop
total += num; // shortcut add and assign operator
}
return (total / this.grades.size()); // double math, ArrayList grades object maintains its size
}
// static main method, used as driver and tester
public static void main(String[] args) {
GradeCalculator grades = new GradeCalculator(); // 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
GradeCalculator.main(null);
// Hack 1 --> Demonstrating Key Learnings of Primitives Wk1
public class DefinePrimitives {
public static void main(String[] args) {
int anInt = 828329297;
double aDouble = 90.8;
boolean aBoolean = false;
String aString = "Clippers are winning the NBA Championship";
System.out.println("anInt: " + anInt);
System.out.println("aDouble: " + aDouble);
System.out.println("aBoolean: " + aBoolean);
System.out.println("aString: " + aString);
}
}
DefinePrimitives.main(null)
//Hack 2 Compound Assignment Operators
public class Compound {
public static void main(String[] args) {
int a = 50;
int b = 25;
int add = a + b;
int subtract = a - b;
int multiply = a * b;
float divide = a / b;
System.out.println("When a and b are added: " + add);
System.out.println("When a and b are subtracted: " + subtract);
System.out.println("When a and b are multiplied: " + multiply);
System.out.println("When a and b are divided: " + divide);
}
}
Compound.main(null)