Java Using Objects
// 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
import java.math.*;
public class Menu {
// Instance Variables
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
public final String[][] COLORS = { // 2D Array of ANSI Terminal Colors
{"Default",DEFAULT},
{"Red", "\u001B[31m"},
{"Green", "\u001B[32m"},
{"Yellow", "\u001B[33m"},
{"Blue", "\u001B[34m"},
{"Purple", "\u001B[35m"},
{"Cyan", "\u001B[36m"},
{"White", "\u001B[37m"},
};
// 2D column location for data
public final int NAME = 0;
public final int ANSI = 1; // ANSI is the "standard" for terminal codes
// 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 - Significant Figures");
System.out.println("0 - Quit");
System.out.println("-------------------------\n");
}
public static void significant(String[] args) {
Scanner input;
System.out.print("Enter 1st number: ");
input = new Scanner(System.in);
String input1 = input.nextLine();
System.out.println(input1);
System.out.print("Enter 2nd number: ");
input = new Scanner(System.in);
String input2 = input.nextLine();
System.out.println(input2);
var y = significantDigits(new BigDecimal(input1));
var z = significantDigits(new BigDecimal(input2));
System.out.print("The number of significant figures for your first input: ");
System.out.println(y);
System.out.print("The number of significant figures for your second input: ");
System.out.println(z);
if(y > z) {
System.out.print("The number of significant figures that would be used if these two numbers were multiplied together would be: " + z);
}
else if(z > y) {
System.out.print("The number of significant figures that would be used if these two numbers were multiplied together would be: " + y);
}
else {
System.out.print("The number of significant figures that would be used if these two numbers were multiplied together would be: " + z);
}
}
private static int significantDigits(BigDecimal input) {
input = input.stripTrailingZeros();
return input.scale() < 0 ? input.precision() - input.scale() : input.precision(); // if input.scale is less than 0 (which would be the sig figs after the decimal point), then it subtracts input scale from input precision, which is the number of digits. Other wise it just sticks with input precision.
}
// 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("bye");
quit = true;
break;
case 1:
significant();
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT);
return quit;
}
static public void main(String[] args) {
new Menu();
}
}
Menu.main(null);