Nathan Java Object Menu
Nathan Object NB
import java.util.Scanner;
import java.lang.Math;
import java.util.Random;
class Guesser {
public Guesser(){
this.playGame();
}
// Random Number to 100
boolean check = false;
Random rand = new Random();
int answer = rand.nextInt(100);
public static void main(String[] args){
Guesser guessGame = new Guesser();
}
// Function that checks whether or not the guess was correct
public boolean checker(int guess, int ans){
if(guess == ans){
check = true;
}
return check;
}
public void playGame(){
Scanner scan = new Scanner(System.in);
System.out.println("Input your guess: ");
int inp = Integer.parseInt(scan.nextLine());
boolean output = checker(inp, answer);
while(output == false){
if(inp == 0){
break;
}
if(inp < answer){
System.out.println("Wrong, you guessed " + inp + ", guess higher silly goose!");
}
if(inp > answer){
System.out.println("Wrong, guessed " + inp + ", lower silly goose!");
}
System.out.println("Input your guess again: ");
inp = Integer.parseInt(scan.nextLine());
output = checker(inp, answer);
}
System.out.println("You got it right! The answer was: " + answer);
}
}
class FlipCoin {
public void coinFlip(int count){
Random rando = new Random();
for(int i = 0; i < count; i++){
int side = rando.nextInt(2);
if(side == 1){
System.out.println("Heads");
}
else{
System.out.println("Tails");
}
}
}
}
class Animals {
// Function that uses if, else if, and else to decide which animal to print out.
public void ifs(int choice){
// checks if statement first
if(choice == 1){
System.out.println("ʕ •ᴥ•ʔ");
}
// runs down if else statement list
else if(choice == 2){
System.out.println("εїз");
}
else if(choice == 3){
System.out.println("(=•́ܫ•̀=)");
}
else if(choice == 4){
System.out.println("˄·͈༝·͈˄");
}
else if(choice == 5){
System.out.println("くコ:彡");
}
// runs else block if nothing applies above
else{
System.out.println("You did not choose an option for an animal :(");
}
}
// Function that is converted to switch, case, and other to decide which animal to print out.
public void switches(int choice){
// takes in choice and chooses the case based off of it
switch(choice){
case 1:
System.out.println("ʕ •ᴥ•ʔ");
break;
case 2:
System.out.println("εїз");
break;
case 3:
System.out.println("(=•́ܫ•̀=)");
break;
case 4:
System.out.println("˄·͈༝·͈˄");
break;
case 5:
System.out.println("くコ:彡");
break;
// Otherwise case: same as else
default:
System.out.println("You did not choose an option for an animal :(");
}
}
}
class Turtle{
public void weight(int weight){
System.out.println("I weigh " + weight + " pounds!");
}
public void name(String name){
System.out.println("My name is " + name + "!");
}
}
class RussianTort extends Turtle{
public void terrain(){
System.out.println("I live on land!");
}
}
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 - Number guessing game");
System.out.println("2 - Coinflipper");
System.out.println("3 - Loading in color");
System.out.println("4 - Learn about Shellby");
System.out.println("5 - See animals");
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:
Guesser guess = new Guesser();
guess.playGame();
break;
case 2:
FlipCoin flip = new FlipCoin();
int flips = 10;
flip.coinFlip(flips);
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:
RussianTort shellby = new RussianTort();
shellby.weight(1);
shellby.name("Shellby");
shellby.terrain();
break;
case 5:
Scanner animal = new Scanner(System.in);
System.out.println("Input 1 for koala, 2 for butterfly, 3 for cat, 4 for bird, and 5 for squid: ");
int animalChoice = Integer.parseInt(animal.nextLine());
Animals animalObj = new Animals();
System.out.println("This was printed using if-elseif-else: ");
animalObj.ifs(animalChoice);
System.out.println("This was printed using switch-case-otherwise: ");
animalObj.switches(animalChoice);
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);