Primitives Jupyter Notebook
about primitives
- Class with primitives & wrapper classes
- Casting w/ Arithmetic Operations
- Compound Assignment Operators
- GPA Calculator
public class Human {
private double heightIn; //a height can be a decimal
private String name; //name is a sequence of words
private int age; //is a whole nummber
private boolean hasPets; //can either be true or false
public Human (String name) {
this.name = name;
}
public double getHeight () {
return this.heightIn;
}
public void setHeight (double height) {
this.heightIn = height;
}
public String getName () {
return this.name;
}
public int getAge () {
return this.age;
}
public void setAge (int age) {
this.age = age;
}
public boolean getHasPets () {
return this.hasPets;
}
public void setHasPets (boolean hasPets) {
this.hasPets = hasPets;
}
public String getDetails () {
return this.getName() + " is " + this.getAge() + " years old and is " + this.getHeight() + " in tall and " + (this.getHasPets() ? "does" : "doesn't") + " have pets.";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
double height;
int age;
boolean hasPets;
//input name w/ error handling
try {
System.out.println("What is the name of the human?");
name = sc.next();
} catch (InputMismatchException e) {
System.out.println("Inputted name is not a string");
return;
}
//input height w/ error handling
try {
System.out.println("What is the height of the human (in inches)?");
height = sc.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Inputted height is not a double");
return;
}
//input age w/ error handling
try {
System.out.println("What is the age of the human?");
age = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("Inputted age is not an integer");
return;
}
//input hasPets w/ error handling
try {
System.out.println("Does the human have pets (true/false)?");
hasPets = sc.nextBoolean();
} catch (InputMismatchException e) {
System.out.println("Inputted value is not a boolean (true/false)");
return;
}
//create human
Human rohan = new Human(name);
rohan.setHeight(height);
rohan.setAge(age);
rohan.setHasPets(hasPets);
System.out.println(rohan.getDetails());
}
}
Now we can test the code using the Human::main
method
Human.main(null);
public class ArithmeticOps {
public static void main (String[] args) {
System.out.println("Integer Division Calculator");
Scanner sc = new Scanner(System.in);
int a, b;
try {
System.out.println("Enter value for a");
a = sc.nextInt();
System.out.println("Enter value for b");
b = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("Input was not an integers");
return;
}
int integerDivision = a/b; //this will round down due to integer division
double doubleDivision = ((double)a)/((double)b); //a and b are converted to doubles before division giving exact results
System.out.println("a is " + a + " and b is " + b);
System.out.println("Integer Division Result " + integerDivision);
System.out.println("Double Division Result " + doubleDivision); //note that the result for this may be slightly inaccurate due to base-2 to base-10 conversion
}
}
ArithmeticOps.main(null);
public class CompoundOps {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter first number: ");
double a = sc.nextDouble();
System.out.println("The number is now " + a);
System.out.println("Increase number by: ");
a += sc.nextDouble();
System.out.println("The number is now " + a);
System.out.println("Decrease number by: ");
a -= sc.nextDouble();
System.out.println("The number is now " + a);
System.out.println("Multiply number by: ");
a *= sc.nextDouble();
System.out.println("The number is now " + a);
System.out.println("Divide number by: ");
a /= sc.nextDouble();
System.out.println("The number is now " + a);
} catch (InputMismatchException e) {
System.out.println("Input was invalid");
}
}
}
CompoundOps.main(null);
//stats for a Grade (A,B,C NOT 9th,10th,11th)
public class GradeStats {
private int total_count; // -- int because whole number
private int total_count_ap; // int because whole number
private int uc_count; //UC count only includes sophmore/junior years -- int because whole number
private int uc_count_ap; //UC count only includes sophmore/junior years -- int because whole number
private double ap_gpa; //gpa for AP Classes (ie. 4.0, 3.0, etc.) -- double because it can have decimals
private double normal_gpa; //gpa for Normal Classes (ie. 5.0, 4.0, etc.) -- double because it can have decimals
private String grade; //A, B, C, etc. -- string because it is characters
public GradeStats (String grade, double ap_gpa, double normal_gpa) {
this.grade = grade;
this.ap_gpa = ap_gpa;
this.normal_gpa = normal_gpa;
}
public void setTotalCount (int total_count) {
this.total_count = total_count;
}
public int getTotalCount () {
return this.total_count;
}
public void setTotalCountAP (int total_count_ap) {
this.total_count_ap = total_count_ap;
}
public int getTotalCountAP () {
return this.total_count_ap;
}
public void setUCCount (int uc_count) {
this.uc_count = uc_count;
}
public int getUCCount () {
return this.uc_count;
}
public void setUCCountAP (int uc_count_ap) {
this.uc_count_ap = uc_count_ap;
}
public int getUCCountAP () {
return this.uc_count_ap;
}
public String getGrade () {
return this.grade;
}
public double getAPGPA () {
return this.ap_gpa;
}
public double getNormalGPA () {
return this.normal_gpa;
}
}
public class GPACalculator {
GradeStats[] gradeStats = {
new GradeStats("A", 5.0, 4.0),
new GradeStats("B", 4.0, 3.0),
new GradeStats("C", 3.0, 2.0),
new GradeStats("D", 2.0, 1.0),
new GradeStats("F", 1.0, 0.0)
};
// gets input from the scanner
public GPACalculator (Scanner sc) throws InputMismatchException {
for (int i = 0; i<5; i++) {
GradeStats curGradeStats = gradeStats[i];
System.out.print("How many total " + curGradeStats.getGrade() + "s do you have? ");
curGradeStats.setTotalCount(sc.nextInt());
System.out.println(curGradeStats.getTotalCount());
System.out.print("How many total (AP) " + curGradeStats.getGrade() + "s do you have? ");
curGradeStats.setTotalCountAP(sc.nextInt());
System.out.println(curGradeStats.getTotalCountAP());
System.out.print("How many sophmore/junior " + curGradeStats.getGrade() + "s do you have? ");
curGradeStats.setUCCount(sc.nextInt());
System.out.println(curGradeStats.getUCCount());
System.out.print("How many sophmore/junior (AP) " + curGradeStats.getGrade() + "s do you have? ");
curGradeStats.setUCCountAP(sc.nextInt());
System.out.println(curGradeStats.getUCCountAP());
}
}
public double getUnweighted () {
double total = 0;
double count = 0;
for (int i = 0; i<5; i++) {
GradeStats curGradeStats = gradeStats[i];
count += curGradeStats.getTotalCount();
total += curGradeStats.getTotalCount() * curGradeStats.getNormalGPA();
}
return count != 0.0 ? total/count : 0.0;
}
public double getWeighted () {
double total = 0;
double count = 0;
for (int i = 0; i<5; i++) {
GradeStats curGradeStats = gradeStats[i];
count += curGradeStats.getTotalCount();
int normalCount = curGradeStats.getTotalCount() - curGradeStats.getTotalCountAP();
total += normalCount * curGradeStats.getNormalGPA() + curGradeStats.getTotalCountAP() * curGradeStats.getAPGPA();
}
return count != 0.0 ? total/count : 0.0;
}
public double getUnweightedUC () {
double total = 0;
double count = 0;
for (int i = 0; i<5; i++) {
GradeStats curGradeStats = gradeStats[i];
count += curGradeStats.getUCCount();
total += curGradeStats.getUCCount() * curGradeStats.getNormalGPA();
}
return count != 0.0 ? total/count : 0.0;
}
public double getWeightedUC () {
double total = 0;
double count = 0;
int ap_count = 0; //limited to 8 AP trimesters
for (int i = 0; i<5; i++) {
GradeStats curGradeStats = gradeStats[i];
count += curGradeStats.getUCCount();
if (ap_count > 8) {
total += curGradeStats.getUCCount() * curGradeStats.getNormalGPA();
continue;
}
ap_count += curGradeStats.getUCCountAP();
if (ap_count > 8) {
int normalAPs = curGradeStats.getUCCountAP() - ap_count;
int normalCount = curGradeStats.getUCCount() - curGradeStats.getUCCountAP() + normalAPs;
total += normalCount * curGradeStats.getNormalGPA() + (curGradeStats.getUCCountAP() - normalAPs) * curGradeStats.getAPGPA();
} else {
int normalCount = curGradeStats.getUCCount() - curGradeStats.getUCCountAP();
total += normalCount * curGradeStats.getNormalGPA() + curGradeStats.getUCCountAP() * curGradeStats.getAPGPA();
}
}
return count != 0.0 ? total/count : 0.0;
}
//runs GPA Calculator class
public static void main (String[] args) {
try {
Scanner sc = new Scanner(System.in);
GPACalculator gpaCalc = new GPACalculator(sc);
System.out.println("Unweighted GPA: " + gpaCalc.getUnweighted());
System.out.println("Weighted GPA: " + gpaCalc.getWeighted());
System.out.println("Unweighted GPA (UC): " + gpaCalc.getUnweightedUC());
System.out.println("Weighted GPA (UC): " + gpaCalc.getWeightedUC());
} catch (InputMismatchException e) {
System.out.println("Bad Input (all input should be integers)!");
}
}
}
GPACalculator.main(null);