2022-09-01 Java Objects Notes
Documentation
- Within the code the Class is defined by 'public Menu', aka the constructor of the object
public Menu() {
- Usage of 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"}, };
- Object calling method main
Menu.main(null);
- Mutation instance
int random = (int) (Math.random() * COLORS.length); // random logic
import java.lang.Math;
import java.util.*;
public class Menu {
public final String DEFAULT = "\u001B[0m";
public final String[][] COLORS = {
{"Default",DEFAULT},
{"Red", "\u001B[31m"},
{"Green", "\u001B[32m"},
{"Yellow", "\u001B[33m"},
{"Blue", "\u001B[34m"},
{"Purple", "\u001B[35m"},
{"Cyan", "\u001B[36m"},
{"White", "\u001B[37m"},
};
public final int NAME = 0;
public final int ANSI = 1;
public Menu() {
Scanner sc = new Scanner(System.in);
this.print();
boolean quit = false;
while (!quit) {
try {
int choice = sc.nextInt();
System.out.print("" + choice + ": ");
quit = this.action(choice);
} catch (Exception e) {
sc.nextLine();
System.out.println(e + ": Not a choice");
}
}
sc.close();
}
private void print() {
System.out.println("-------------------------");
System.out.println("Java Math");
System.out.println("-------------------------");
System.out.println("1 - Degrees to Radians Converter");
System.out.println("2 - Radians to Tangent");
System.out.println("3 - Radians to Cosine");
System.out.println("4 - Radians to Sine");
System.out.println("5 - Random output of numbers and then converted to ASCII");
System.out.println("0 - Quit");
System.out.println("-------------------------");
}
private boolean action(int selection) {
boolean quit = false;
Scanner input = new Scanner(System.in);
switch (selection) {
case 0:
System.out.print("Cya");
quit = true;
break;
case 1:
System.out.println("Enter an amount of degrees: ");
double degrees = input.nextDouble();
System.out.println(Math.toRadians(degrees));
case 2:
System.out.println("Enter radians: ");
double radians = input.nextDouble();
System.out.print("Here is your sine value: " + Math.sin(radians));
break;
case 3:
System.out.println("Enter radians: ");
radians = input.nextDouble();
System.out.print("Here is your tangent value: " + Math.tan(radians));
break;
case 4:
System.out.println("Enter radians: ");
radians = input.nextDouble();
System.out.print("Here is your cosine value: " + Math.cos(radians));
break;
case 5:
int random1 = 0;
int i = 0;
int l = 0;
int[] asciiList = new int[10];
for(i = 0; i < asciiList.length; i++){
random1 = (int)(Math.random()*100 +20000);
asciiList[i] = random1;
}
System.out.println("Here is your list of random Chinese characters using Math.random: ");
for(int element: asciiList){
System.out.print((char)element + ", ");
}
break;
default:
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT);
return quit;
}
static public void main(String[] args) {
new Menu();
}
}
Menu.main(null);