Java Terminal
Using CSA terminal code as base to create a new terminal with math functions
import java.util.Scanner; // scanner for input, Math for more complex operations
import java.lang.Math;
public class Menu {
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
// Constructor on this Object takes control of menu events and actions
public Menu() {
Scanner sc = new Scanner(System.in); // using 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 + "is not a valid input, please 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 a math operation");
System.out.println("-------------------------\n");
System.out.println("1 - Add");
System.out.println("2 - Subtract");
System.out.println("3 - Multiply");
System.out.println("4 - Divide");
System.out.println("5 - Square Root");
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
// i forgot to put curly brackets for each case which is why all the scanner variable names are different
case 0:
System.out.print("Bye!");
quit = true;
break;
case 1: //addition
Scanner add = new Scanner(System.in);
System.out.println("Enter first number:");
double firstNumber = add.nextDouble();
System.out.println("Enter second number: ");
double secondNumber = add.nextDouble();
double result = firstNumber + secondNumber;
// Output input by user
System.out.println(firstNumber + " + " + secondNumber + " = " + result);
break;
case 2: // subtraction
Scanner subtract = new Scanner(System.in);
System.out.println("Enter first number:");
double first = subtract.nextDouble();
System.out.println("Enter second number: ");
double second = subtract.nextDouble();
double and = first - second;
// Output input by user
System.out.println(first + " - " + second + " = " + and);
break;
case 3: // multiplication
Scanner multiply = new Scanner(System.in);
System.out.println("Enter first number:");
double a = multiply.nextDouble();
System.out.println("Enter second number: ");
double b = multiply.nextDouble();
double c = a * b;
// Output input by user
System.out.println(a + " * " + b + " = " + c);
break;
case 4: //division
Scanner divide = new Scanner(System.in);
System.out.println("Enter first number:");
double x = divide.nextDouble();
System.out.println("Enter second number: ");
double y = divide.nextDouble();
double z = x / y;
// Output input by user
System.out.println(x + " / " + y + " = " + z);
break;
case 5: //square root
Scanner root = new Scanner(System.in);
System.out.println("Enter a number:");
double aaa = root.nextDouble();
double square = Math.sqrt(aaa);
// Output input by user
System.out.println("The square root of " + aaa + " is " + square);
break;
default: // if input is outside of range
//Prints error message from console
System.out.print("Please enter a valid input");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
public static void main(String args[]) {
System.out.println("hello");
new Menu(); // starting Menu object
}
}
Menu.main(null); // calling function