Week 1 - Java Objects Post
Blog post demonstrating Java objects for AP CSA
import java.util.Scanner;
import java.lang.Math;
public class Menu {
public Menu() {
Scanner sc = new Scanner(System.in);
this.print();
boolean quit = false;
while (!quit) {
try {
int choice = sc.nextInt();
System.out.print("" + choice + ": ");
quit = this.action(choice);
} catch (Exception e) {
sc.nextLine();
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
private void print() {
System.out.println("-------------------------\n");
System.out.println("Java Menu");
System.out.println("-------------------------\n");
System.out.println("1 - NBA Player Personality Quiz");
System.out.println("2 - Which kinematics equation should I use?");
System.out.println("3 - Print out digits of PI");
System.out.println("0 - Quit");
System.out.println("-------------------------\n");
}
// Private method to perform action and return true if action is to quit/exit
private boolean action(int selection) {
boolean quit = false;
Scanner typeInput = new Scanner(System.in);
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
System.out.println("\nNBA Players Personality Test");
int[] questionNumberList = {1, 2, 3, 4};
int count = 0;
for (int a = 0; a < questionNumberList.length; a++) {
System.out.println("Question " + questionNumberList[a]);
if (questionNumberList[a] == 1) {
System.out.println("What is your favorite basketball team?");
System.out.println("A) Chicago Bulls");
System.out.println("B) LA Lakers");
System.out.println("C) Golden State Warriors");
System.out.println("D) Oklahoma City Thunder");
String userAnswer1 = typeInput.nextLine();
String userAnswer1Update = userAnswer1.toLowerCase();
if (userAnswer1Update.equals("a")) {
count += + 1;
} else if (userAnswer1Update.equals("b")) {
count += + 2;
} else if (userAnswer1Update.equals("c")) {
count += + 3;
} else if (userAnswer1Update.equals("d")) {
count += 4;
}
} else if (questionNumberList[a] == 2) {
System.out.println("What is your ideal height?");
System.out.println("A) 7'1");
System.out.println("B) 6'6");
System.out.println("C) 6'11");
System.out.println("D) 6'3");
String userAnswer2 = typeInput.nextLine();
String userAnswer2Update = userAnswer2.toLowerCase();
if (userAnswer2Update.equals("a")) {
count += 2;
} else if (userAnswer2Update.equals("b")) {
count += 1;
} else if (userAnswer2Update.equals("c")) {
count += 4;
} else if (userAnswer2Update.equals("d")) {
count += 3;
}
} else if (questionNumberList[a] == 3) {
System.out.println("What is your favorite basketball position?");
System.out.println("A) Small Forward");
System.out.println("B) Point Guard");
System.out.println("C) Center");
System.out.println("D) Shooting Guard");
String userAnswer3 = typeInput.nextLine();
String userAnswer3Update = userAnswer3.toLowerCase();
if (userAnswer3Update.equals("a")) {
count += 4;
} else if (userAnswer3Update.equals("b")) {
count += 3;
} else if (userAnswer3Update.equals("c")) {
count += 2;
} else if (userAnswer3Update.equals("d")) {
count += 1;
}
} else if (questionNumberList[a] == 4) {
System.out.println("What is favorite basketball skill?");
System.out.println("A) Shooting");
System.out.println("B) Dunking");
System.out.println("C) Dribbling");
System.out.println("D) Defending");
String userAnswer4 = typeInput.nextLine();
String userAnswer4Update = userAnswer4.toLowerCase();
if (userAnswer4Update.equals("a")) {
count += 3;
} else if (userAnswer4Update.equals("b")) {
count += 2;
} else if (userAnswer4Update.equals("c")) {
count += 4;
} else if (userAnswer4Update.equals("d")) {
count += 1;
}
}
}
System.out.println("You have completed the NBA personality quiz!");
if (count >= 13) {
System.out.println("You are Kevin Durant!");
} else if (count > 10) {
System.out.println("You are Stephen Curry!");
} else if (count > 7) {
System.out.println("You are Shaq O' Neal!");
} else if (count >= 4) {
System.out.println("You are Michael Jordan!");
}
break;
case 2:
System.out.println("What is the information you need to find?");
String kinematicsInput1 = typeInput.nextLine();
String kinematicsInput1LowerCase = kinematicsInput1.toLowerCase();
String kinematicsEquation1 = "Vf = Vi + a * t";
String kinematicsEquation2 = "x = Vi * t + (a * t^2)/2";
String kinematicsEquation3 = "Vf^2 = Vi^2 + 2 * a * x";
String kinematicsEquation4 = "x = (Vi + Vf)/2 * t";
String kinematicsEquation5 = "x = Vf * t - (a * t^2)/2";
if (kinematicsInput1LowerCase.equals("displacement")) {
System.out.println("The kinematics equation you need is: " + kinematicsEquation1);
} else if (kinematicsInput1LowerCase.equals("final velocity")) {
System.out.println("The kinematics equation you need is: " + kinematicsEquation2);
} else if (kinematicsInput1LowerCase.equals("time")) {
System.out.println("The kinematics equation you need is: " + kinematicsEquation3);
} else if (kinematicsInput1LowerCase.equals("acceleration")) {
System.out.println("The kinematics equation you need is: " + kinematicsEquation4);
} else if (kinematicsInput1LowerCase.equals("initial velocity")) {
System.out.println("The kinematics equation you need is: " + kinematicsEquation5);
}
break;
case 3:
Scanner piInput = new Scanner(System.in);
System.out.println("How many digits of Pi do you want to print out?");
int piDigits = piInput.nextInt();
int[] piArray = new int[]{3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0,5,8,2,0,9,7,4,9,4,4,5,9,2,3,0,7,8,1,6,4,0,6,2,8,6,2,0,8,9,9,8,6,2,8,0,3,4,8,2,5,3,4,2,1,1,7,0,6,7,9};
String piPrint = "";
for (int i = 0; i < piDigits; i++) {
if (piDigits > 1 && i == 0) {
piPrint += piArray[i] + ".";
} else {
piPrint += piArray[i];
}
}
System.out.println(piPrint);
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
return quit;
}
// Static driver/tester method
static public void main(String[] args) {
new Menu();
}
}
Menu.main(null);