innovation using object
understanding of objects in java πΆβπ«οΈπ€
import java.lang.Math;
class box {
double width;
double height;
double depth;
// volume of box
double volume() {
double vol = width *height * depth;
System.out.println("Volume "+ vol);
return vol;
}
public box(double w, double h, double d) {
System.out.println("w h d: " + w + " " + h + " " + d);
width = w;
height = h;
depth = d;
}
public box(double w) {
System.out.println("side : " + w );
width = w;
height = w;
depth = w;
}
}
public class sphere {
double radius;
double volume() {
double vol = ((4 * Math.PI * Math.pow(radius, 3))/3) ;
System.out.println("Volume of sphere: " + vol);
return vol;
}
public sphere(double r) {
System.out.println("r :" + r);
radius = r;
}
}
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
Scanner sc; // available to all inside the menu class
// Constructor on this Object takes control of menu events and actions
public Menu() {
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.println("" + 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 - Find volume of box");
System.out.println("2 - Find volume of cube");
System.out.println("3 - Find volume of sphere");
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.println("Enter width: ");
double w = sc.nextDouble();
System.out.println("Enter height: ");
double h = sc.nextDouble();
System.out.println("Enter depth: ");
double d = sc.nextDouble();
box myrectangle = new box(w, h, d);
myrectangle.volume();
break;
case 2:
System.out.println("Enter side length: ");
w = sc.nextDouble();
box mycube = new box(w);
mycube.volume();
break;
case 3:
System.out.println("Enter radius length: ");
double r = sc.nextDouble();
sphere mysphere = new sphere(r);
mysphere.volume();
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);