Primitive Data Types
An introduction to primitive types using input, output, and finishing with a grading calculator.
College Board Summary
College Board focus is on primitive types of int, double, and boolean. In unit1, String is mentioned, but it is technically a Wrapper Class. The primitive type that is building block for a String is a single character called char. String is an array of char's and the uppercase S in String means it is a Class.
Data Types
Now is a good time to understand data types. There are primitive types in Java, but you should know there are Wrapper Classes. Java likes everything to be a class. String is a Non-Primitive data type that is a Class in Java.
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)
// 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);
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);
Grade Calculator, putting Input and Output together
Primitive types rarely stand alone. This lab uses the primitive type double, but it also introduces the wrapper class Double. Also included is one of the most common Data Structures in Java, the ArrayList. These items are put together to create a grade calculator.
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);
Hacks
Build your own Jupyter Notebook meeting these College Board and CTE competencies
- Define in a Class the following data types
- Demonstrate use of Primitives: int, double, boolean, string
- Demonstrate use of Wrapper Class object: String
- Describe in comments how each data type choice is appropriate to application
- Perform arithmetic expressions and assignment in a program code Code.org Lesson
- Determine what is result is in a variable as a result of an data type and expression (ie integer vs double)
- Perform an arithmetic expressions that uses casting, add comments that show how it produces desired result. Learn more by watching this College Board video
- Perform compound assignment operator (ie +=), add comments to describe the result of operator
Additional requirements
- Multiple inputs and outputs are required
- Jupyter NoteBook when committed to Fastpages must display Outputs
- Building something that helps you study for another class is encouraged, here are some ideas: MPG, GPA, Celsius <--> Fahrenheit, The nth Fibonacci, GCD, Primes in range of numbers, Points per Game