Team Objects Hacks
Group console menu for objects hacks
// 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
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 - Say Hello");
System.out.println("2 - Output colors");
System.out.println("3 - Loading in color");
System.out.println("4 - Temp Converter (Shraddha)");
System.out.println("5 - GPA Calculator (Meena)");
System.out.println("6 - Simple Calculator(Pranavi)");
System.out.println("7 - Statistics Calculator (Madhumita)");
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:
System.out.print("Hello, World!");
break;
case 2:
for(int i = 0; i < COLORS.length; i++) // loop through COLORS array
System.out.print(COLORS[i][ANSI] + COLORS[i][NAME]);
break;
case 3:
System.out.print("Loading...");
for (int i = 0; i < 20; i++) { // fixed length loading bar
int random = (int) (Math.random() * COLORS.length); // random logic
try {
Thread.sleep(100); // delay for loading
} catch (Exception e) {
System.out.println(e);
}
System.out.print(COLORS[random][ANSI] + "#");
}
break;
case 4:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in 1 to convert from Fahrenheit to Celsius or 2 to convert from Celsius to Fahrenheit.");
int conversionDeterminer = keyboard.nextInt();
System.out.println("Please enter a temperature.");
int originalTemp = keyboard.nextInt();
double convertedTemp;
if(conversionDeterminer == 1)
{
convertedTemp = (originalTemp - 32) * (5.0/9);
System.out.println(convertedTemp);
}
else if(conversionDeterminer == 2)
{
convertedTemp = originalTemp * (9.0/5) + 32;
System.out.println(convertedTemp);
}
else
{
System.out.println("Sorry, that input is not valid. Please try running the program again.");
}
break;
case 5:
System.out.println("GPA Calculator");
System.out.println("Code by Meena Annamalai");
int count = -1;
Scanner input;
boolean exitProgram = false;
input = new Scanner(System.in);
System.out.print("How many course would you like to enter? ");
int numCourses = input.nextInt();
System.out.println(numCourses);
input.close();
int[] gradePoints = new int[numCourses];
int[] creditHours = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
// get course
input = new Scanner(System.in);
System.out.print("\nEnter a course, type 0 to exit: ");
/** Wrapper class object string is used here since the user enters the course name **/
String course = input.nextLine();
System.out.println(course);
input.close();
if (course.equals("0")) {
exitProgram = true;
System.out.println("Bye!");
break;
}
//compound assignment operator used here - the result is that the count is increased by 1 each time the loop runs
count += 1;
// get credits
input = new Scanner(System.in);
System.out.print("Enter the number of credits for that course: ");
/** primitive data type: an integer is used here since the number of credits will later be used for calculations so a string shouldn't be used and since the credit number will always
be a whole number, an integer data type can be used **/
int credits = input.nextInt();
System.out.println(credits);
input.close();
// get letter grade
input = new Scanner(System.in);
System.out.print("Enter the letter grade you got: ");
/** Wrapper class object string is used here since the user enters the letter grade they got which will then
be matched with the corresponding point value with the below switch statement **/
String letterGrade = input.nextLine();
System.out.println(letterGrade);
input.close();
int pointValue = 0;
//convert letter grade to gpa
switch (letterGrade) {
case "A":
pointValue = 4;
break;
case "B":
pointValue = 3;
break;
case "C":
pointValue = 2;
break;
case "D":
pointValue = 1;
break;
case "F":
pointValue = 0;
break;
}
creditHours[count] = credits;
gradePoints[count] = pointValue * credits;
}
int totalGradePoints = 0;
for (int i = 0; i < gradePoints.length; i++) {
totalGradePoints += gradePoints[i];
}
int totalAttemptedCredits = 0;
for (int i = 0; i < creditHours.length; i++) {
totalAttemptedCredits += creditHours[i];
}
//here the Primitive data type boolean is used since I only want the final gpa to be shown if the user doesn't exit the program
if (exitProgram == false) {
//here the primitive data type double is used since the the total grade pts divided by the total attempted credits will not always be a whole number
//casting is also used for this reason as both variables were originally integers
double gpa = (double) totalGradePoints / (double) totalAttemptedCredits;
System.out.println("Your GPA is: " + String.valueOf(gpa));
System.out.println("Bye! Thank you for using GPA Calculator");
break;
}
//ScanPrimitives.main(null);
break;
case 6:
System.out.println("Simple Calculator");
System.out.println("Code by Pranavi Inukurti");
Scanner Scan = new Scanner(System.in);
System.out.println("\n Please enter two numbers");
int xe;
int xo;
System.out.print("\n First number: ");
xe = Scan.nextInt();
System.out.print("\n Second number: ");
xo = Scan.nextInt();
System.out.println("\n Select between (*,/,+,-)\n Type out the character in a single letter: ");
String Operation = Scan.next();
String EO = "You have selected ";
switch (Operation) {
case "*": System.out.println(EO + "* \n Your Result: "+( xe * xo )); break;
case "/": System.out.println(EO + "/ \n Your Result: "+ ( xe / xo )); break;
case "+": System.out.println(EO + "+ \n Your Result: "+ ( xe + xo ));break;
case "-": System.out.println(EO + "- \n Your Result: "+( xe - xo )); break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
//Close
Scan.close();
System.out.println(" Closing Application ");
}
break;
case 7:
ArrayList<Double> data = new ArrayList<>();
Scanner statisticsInput = new Scanner(System.in);
System.out.print("Enter an numerical datapoint, type a non-numerical value to exit: \n");
while(true) {
try {
double userInput = statisticsInput.nextDouble();
System.out.println(userInput);
data.add(userInput);
} catch (Exception e) {
statisticsInput.close();
break;
}
}
//find mean
double total = 0;
for (double num : data) {
total += num;
}
double mean = total/data.size();
//find standard deviation
double standardDeviation = 0;
for (double num : data) {
standardDeviation += Math.pow(num - mean, 2);
}
standardDeviation = Math.sqrt(standardDeviation/(data.size() - 1));
//find min
double min = data.get(0);
for (double num : data) {
if (num < min) {
min = num;
}
}
//find max
double max = data.get(0);
for (double num : data) {
if (num > max) {
max = num;
}
}
System.out.println("Mean: " + mean);
System.out.println("Max: " + max);
System.out.println("Min: " + min);
System.out.println("Standard Deviation: " + standardDeviation);
}
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);