Menu
// imports allow you to use code already written by others. It is good to explore and learn libraries. The names around the dots often give you a hint to the originator of the code.
// these are objects
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
// java style to import library
double mass;
double accel;
double Force;
double ac;
double m;
double m1;
double m2;
double mod1;
double mod2;
double result;
public class Menu {
// Instance Variables
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color // final = not going to change, static means element is not changing (not part of the object)
// Constructor on this Object takes control of menu events and actions
public Menu() {
Scanner sc = new Scanner(System.in); // using Java Scanner Object
this.print(); // print Menu
boolean quit = false;
while (!quit) {
try { // scan for Input
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.print("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
// Print the menu options to Terminal
private void print() {
//System.out.println commands below is used to present a Menu to the user.
System.out.println("-------------------------\n");
System.out.println("Choose from these choices");
System.out.println("-------------------------\n");
System.out.println("1 - Vidhi and Riya's Temp Convertor");
System.out.println("2 - Lily's Average Calculator");
System.out.println("3 - William's Modulus Calculator");
System.out.println("4 - William's F=ma calculator");
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;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye! Thanks for checking this out :D");
quit = true;
break;
case 1:
Scanner input3;
//we used a wrapper class to introduce our program to the user.
String aString = "This is our program to convert Celcius to Kelvin. We are using it for our AP Chemistry class.";
System.out.println(aString);
//we used the string to greet the user
input3 = new Scanner(System.in);
System.out.println("Enter your name as a string: ");
String name = input3.nextLine();
System.out.println("Hello " + name );
input3.close();
//the integer is used to get the age of the user
input3 = new Scanner(System.in);
System.out.println("Enter your age as an integer: ");
String age = input3.nextLine();
System.out.println("You are " + age + " years old." );
input3.close();
//boolean is used to get a true or false answer about whether the user is in AP Chemistry
input3 = new Scanner(System.in);
System.out.println("Are you in AP Chemistry? Enter your answer as a Boolean: ");
String chem = input3.nextLine();
System.out.println("Your answer: " + chem);
input3.close();
//double is used to get a number from the user and convert it using arithmetic expression
input3 = new Scanner(System.in);
System.out.println("Enter a degree in Celsius as a double: ");
double celsius = input3.nextDouble();
double kelvin = (celsius + 273.0);
System.out.println( celsius + " degree Celsius is equal to " + kelvin + " in Kelvin");
input3.close();
break;
case 2:
double numDouble = 0;
double sum = 0;
// count = n (sample size to determine mean)
// Sample size is always a whole number (ex: 1, 2, etc.)
int count = 0;
double mean = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter numbers, type 'end' to finish");
while (true) {
// String is used as the input for each number
// The reason why I didn't use int was because I wanted the user to
// be able to end the calculator by typing "end"
System.out.println("Number: ");
String numStr = sc.next();
System.out.println(numStr);
if ("end".equals(numStr)) {
break;
}
// This performs casting by changing the input, which was a string,
// into a double so that the mean can by determined
numDouble = Double.parseDouble(numStr);
sum += numDouble;
count++;
}
mean = sum/count;
System.out.println("Show detailed info? y/n");
String detail = sc.next();
// Setting showDetail as true/false, this can be used in the future
// as a toggle. (If showDetail = true, show more detail, otherwise,
// only show the result)
// Also showDetail can only be yes/no, so it can be set as a boolean
System.out.println(detail);
boolean showDetail;
if ("y".equals(detail)) {
showDetail = true;
} else {
showDetail = false;
}
if (showDetail) {
System.out.println("Sum: " + sum);
System.out.println("Count: " + count);
}
System.out.println("Mean: " + mean);
break;
case 3:
Scanner input;
// primitive int
input = new Scanner(System.in);
System.out.println("Modulus calculator");
System.out.print("Enter your first number:");
try {
double mod1 = input.nextDouble();
System.out.println(mod1);
m1 = mod1;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
// primitive int
input = new Scanner(System.in);
System.out.print("Enter the second number: ");
try {
double mod2 = input.nextDouble();
System.out.println(mod2);
m2 = mod2;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
System.out.println("Modulus Calculator");
double result = m1%m2;
System.out.print(m1 + "mod" + m2 + '=' + result);
break;
case 4:
// java style to import library
// class must alway have 1st letter as uppercase, CamelCase is Java Class convention
Scanner input2;
// primitive int
input2 = new Scanner(System.in);
System.out.println("F=ma calculator");
System.out.println("Enter the mass: ");
try {
double mass = input2.nextDouble();
System.out.println(mass);
m = mass;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input2.close();
// primitive int
input2 = new Scanner(System.in);
System.out.print("Enter the accel: ");
try {
double accel = input2.nextDouble();
System.out.println(accel);
ac = accel;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input2.close();
System.out.println("Force calculator");
double Force = m * ac;
System.out.print(m + "*" + ac + '=' + Force);
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
// Static driver/tester method
static public void main(String[] args) {
new Menu(); // starting Menu object
}
}
Menu.main(null);