Console Menu
Console Menu
The following is a console that incorporates our group's calculators.
// 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.
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
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
// 2D column location for data
// 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 - Lily's Average Calculator");
System.out.println("2 - Vidhi and Riya's Temp Convertor");
System.out.println("3 - Vidhi's Print a Random Number");
System.out.println("4 - Modulus Calculator");
System.out.println("5 - 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, World!");
quit = true;
break;
case 1:
// numDouble: User's input is a string, will be changed into a double
// Double is used to allow user to input decimal numbers
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;
double maxNum = 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);
maxNum = Math.max(maxNum, numDouble);
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("Max: " + maxNum);
}
System.out.println("Mean: " + mean);
break;
case 2:
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 3:
Scanner input5;
System.out.println("This option will print a random number from 0 to 1");
double random = (double) (Math.random());
System.out.println(random);
break;
case 4:
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 5:
// 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);
// 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.
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class Menu {
// Instance Variables
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
// 2D column location for data
// 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 - Average Calculator");
System.out.println("2 - Z Score 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;
if (selection == 0) {
System.out.print("Goodbye, World!");
quit = true;
}
else if (selection == 1) {
// numDouble: User's input is a string, will be changed into a double
// Double is used to allow user to input decimal numbers
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;
double maxNum = 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);
maxNum = Math.max(maxNum, numDouble);
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("Max: " + maxNum);
}
System.out.println("Mean: " + mean);
}
else if (selection == 2) {
double x = 0;
double avg = 0;
double standardDeviation = 0;
double z = 0;
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter x: ");
x = sc2.nextDouble();
System.out.println(x);
System.out.println("Enter avg: ");
avg = sc2.nextDouble();
System.out.println(avg);
System.out.println("Enter standard deviation: ");
standardDeviation = sc2.nextDouble();
System.out.println(standardDeviation);
z = (x-avg)/standardDeviation;
System.out.println("Your z score is: " + z);
} else {
//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);