class Box { private double mLength; private double mBreadth; private double mHeight;
public Box(double _mLength, double _mBreadth, double _mHeight) { mLength = _mLength; mBreadth = _mBreadth; mHeight = _mHeight; }
public Box(double _mLength, double _mBreadth) { mLength = _mLength; mBreadth = _mBreadth; mHeight = _mBreadth; }
public Box(double _mside) { mLength = _mside; mBreadth = _mside; mHeight = _mside; }
public Box() { mLength = 10; mBreadth = 10; mHeight = 10; }
public double volume() { return mLength * mBreadth * mHeight; }
public double surfaceArea() { return (mLength * mBreadth * 2) + (mLength * mHeight * 2) + (mHeight * mBreadth * 2); } }
import java.io.*; public class BoxTest {
public static void main(String args[]) { int keepGoing = 1; int commaLocation0; int commaLocation1; int commaLocation2; int commaCounter; int length; double dimension1; double dimension2; double dimension3; String dimensionsStr = ""; String response = ""; String substring1; String substring2; String substring3; BufferedReader reader; reader = new BufferedReader(new InputStreamReader(System.in));
while (keepGoing == 1) { commaLocation0 = 0; commaLocation1 = 0; commaLocation2 = 0; commaCounter = 0; System.out.println("Enter box dimensions, separated by commas");
try { dimensionsStr = reader.readLine(); } catch (IOException ioe) { System.out.println("Please enter valid number(s)."); }
length = dimensionsStr.length(); commaLocation1 = dimensionsStr.indexOf(','); commaLocation2 = dimensionsStr.indexOf(',', commaLocation1 + 1);
if (commaLocation1 > 0) { commaCounter++;
if (commaLocation2 > commaLocation1) commaCounter++; }
if (dimensionsStr.length() == 0) {
Box box1 = new Box(); System.out.println("No dimensions entered. Using default values.\n"); System.out.println("The volume of default box is: " + box1.volume() + " Surface area is " + box1.surfaceArea()); }
else if (commaCounter == 1) { substring1 = dimensionsStr.substring(commaLocation0, commaLocation1); dimension1 = Double.parseDouble(substring1); substring2 = dimensionsStr.substring(commaLocation1 + 1, length); dimension2 = Double.parseDouble(substring2);
Box box2 = new Box(dimension1,dimension2); System.out.println("Surface area of box is " + box2.volume() + " Surface area is " + box2.surfaceArea()); }
else if (commaCounter == 2) { substring1 = dimensionsStr.substring(commaLocation0, commaLocation1); dimension1 = Double.parseDouble(substring1); substring2 = dimensionsStr.substring(commaLocation1 + 1, commaLocation2); dimension2 = Double.parseDouble(substring2); substring3 = dimensionsStr.substring(commaLocation2 + 1, length); dimension3 = Double.parseDouble(substring3);
Box box3 = new Box(dimension1,dimension2,dimension3); System.out.println("Volume of box is " + box3.volume() + " Surface area is " + box3.surfaceArea()); }
else { dimension1 = Double.parseDouble(dimensionsStr);
Box box4 = new Box(dimension1); System.out.println("Volume of box is " + box4.volume() + " Surface area is " + box4.surfaceArea()); } System.out.println("\nCalculate another box?"); response = "g";
while ((response.charAt(0) != 'y' && response.charAt(0) != 'Y') && (response.charAt(0) != 'n' && response.charAt(0) != 'N') && (response.length() > 0)) { System.out.println("y or n");
try { response = reader.readLine(); } catch (IOException ioe) { System.out.println("I/O Exception Occurred"); } }
if (response.length() == 0) keepGoing = 1; else if (response.charAt(0) == 'y' || response.charAt(0) == 'Y') keepGoing = 1; else if (response.charAt(0) == 'n' || response.charAt(0) == 'N') { System.out.println("Ending program..."); keepGoing = 0; } } }}