You are on page 1of 3

import java.util.

Scanner; public class Main{ public static void main(String args[]){ display_box_menu(); } public static void display_box_menu(){ Box sq = new Box(); int Selection =0; do { int x = 0; do{ // use try-catch to handle inval id inputs such as characters/symbols try{ System.out.println("What would you like to do? ( Enter 1,2,3 or 4 to quit)"); // main menu System.out.println(" 1. Find out if the box is a cube \n 2. Find out the volume of the box \n 3. Find out the surface area of th e box \n 4. Quit"); Scanner selection = new Scanner(System.in); Selection = selection.nextInt(); switch (Selection){ case 1: if (sq.cube == true){ System.out.print("****** **** "); System.out.print("The bo x is a cube"); System.out.println(" *** *******\n"); }else { System.out.print ("********** "); System.out.print ("The box is not a cube"); System.out.print ln(" **********\n"); } break; case 2: System.out.print("****** **** "); System.out.print("The bo x volume is " + sq.vol); System.out.println(" *** *******\n"); break; case 3: System.out.print("****** **** "); System.out.print("The bo x surface area is " + sq.surfa); System.out.println(" *** *******\n"); break; case 4: x=1; default: break; } }catch(Exception e){

System.out.println("Enter valid input"); x=0; } }while (x==0); } while (Selection !=4); } }

class Box{ int l,w,h; boolean cube; float vol; float surfa; Box(){ System.out.println("Welcome, enter the following dimensions about your prism object to proceed > "); int[] dimensions = getDimensions(); setDimVal(dimensions); } void setDimVal(int[] x){ this.l = x[0]; this.w = x[1]; this.h = x[2]; cube = Math.isCube(l,w,h); vol = Math.volume(l,w,h); surfa = Math.sr_ar(l,w,h); } //get box dimensions input method int[] getDimensions(){ int[] sides = new int[3]; for (int i = 0; i<3; i++){ boolean x=false; do{ try{ Scanner in = new Scanner(System.in); System.out.print("Side " + (i+1) + " length: "); sides[i]= in.nextInt(); x=true; } catch ( Exception es){ System.out.println("not valid input, try again"); x=false; } } while (x ==false); } return sides; } // call static method from Math class // call static method from Math class // call static method from Math class

} class Math{ // for static calculations //test if the box is a cube static boolean isCube(int x, int y, int z){ if (x==y && y==z) return true; return false; } // volume method static float volume(int x , int y, int z){ return x*y*z; } // surface area method static float sr_ar(int x, int y, int z){ return (x*y*2) + (x*z*2) + (y*z*2); } }

You might also like