Using Java Objects
Learning how to use Java 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
// CLASS IS DEFINED HERE
public class Menu {
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"},
};
public final int NAME = 0;
public final int ANSI = 1; // ANSI is the "standard" for terminal codes
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 + ": ");
// OBJECT CALLS A METHOD
quit = this.action(choice);
} 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 - For Mr. M");
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:
System.out.print(COLORS[3][ANSI] + "Hello Mr. M!");
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);
Hacks
Documentation
Class is defined:
// CLASS IS DEFINED HERE
public class Menu {
}
Instance of a class is defined:
Menu.main(null);
Object calling method:
// OBJECT CALLS A METHOD
quit = this.action(choice);
Object mutating data:
boolean quit = false;
Console vs Code.org differences:
- Code.org shows how to extend classes
- Both have objects (instantiated classes) and methods that are called by those objects
Java Menu for Physics
See below
- Converts between different unit types
- Experiments with math.round() method for the BasetoMicro method.
- A known limitation is that Java only uses 32-bit integers and floats, so using abnormally large/small values for conversion may reduce precision
import java.util.Scanner;
import java.lang.Math;
public class Menu {
public Menu() {
Scanner scan = new Scanner(System.in);
this.printMenu();
boolean quit = false;
while (!quit) {
try {
int choice = scan.nextInt();
System.out.print("" + choice + " - ");
quit = this.action(choice);
} catch (Exception e) {
scan.nextLine();
System.out.println(e + ": Not a number, try again.");
}
};
}
private void printMenu() {
System.out.println("-------------------------");
System.out.println("-- Choose a conversion --");
System.out.println("-------------------------");
System.out.println("1 - Base to milli-");
System.out.println("2 - Base to micro-");
System.out.println("3 - Base to mega-");
System.out.println("4 - Base to kilo-");
System.out.println("0 - Quit");
System.out.println("-------------------------");
}
private boolean action(int selection) {
boolean quit = false;
Scanner scan = new Scanner(System.in);
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.println("Goodbye, World!");
quit = true;
break;
case 1:
// Base to Milli Converter
System.out.println("Enter a base measurement: ");
float basefloat = scan.nextFloat();
System.out.println("Your base conversion in milli- is: " + BasetoMilli(basefloat));
break;
case 2:
// Base to Micro- converter
System.out.println("Enter a base measurement: ");
basefloat = scan.nextFloat();
System.out.println("Your base conversion in micro- is: " + BasetoMicro(basefloat));
break;
case 3:
// Base to Mega- converter
System.out.println("Enter a base measurement: ");
int baseint = scan.nextInt();
System.out.println("Your base conversion in mega- is: " + BasetoMega(baseint));
break;
case 4:
// Base to Kilo- converter
System.out.println("Enter a base measurement: ");
baseint = scan.nextInt();
System.out.println("Your base conversion in kilo- is: " + BasetoKilo(baseint));
break;
default:
// Error message
System.out.print("Unexpected choice, try again.");
}
// System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
static public void main(String[] args) {
new Menu();
}
private int BasetoMilli(float baseFloat) {
int output = (int) (baseFloat * 1000);
return output;
}
private int BasetoMicro(float baseFloat) {
// Experimenting with using Math.round method instead of casting
int output = Math.round((baseFloat * 1000000));
return output;
}
private float BasetoKilo(int baseInt) {
float output = (float) (baseInt / 1000);
return output;
}
private float BasetoMega(int baseInt) {
float output = (float) (baseInt / 1000000);
return output;
}
}
Menu.main(null)
Pair Collaboration
- This week, Everitt was gone during the pair coding session, so I worked with Armaan instead.
- I helped Armaan set up the tools for his workspace and get started with using vscode/fastpages.
- Armaan helped me with code.org working with objects and extending classes.
- However, during the entire week I did occasionally collaborate with Everitt as well as Sophie and Natalie in order to solve some code.org problems together (for ones that did not have straightforward instructions).
Sprint Backlog
Ideas for learning Java through PBL
- Building upon the frontend menu idea - using this to create useful tools for various classes/subjects
- Or replacing frontend menu with a frontend site that can be accessed by everyone