/* * SoapBar.java * * Created on April 13, 2003, 2:19 PM */ /** A bar of soap. The bar has three dimensions: length (longest), * width (middle), and depth (smallest). The bar may be wrapped or * unwrapped. * */ public class Soap { /** The longest dimension. */ private double length; /** The middle dimension (between longest and shortest). */ private double width; /** The smallest dimension. */ private double height; /** true iff the bar of soap is wrapped. (Not required in this sample solution). */ private boolean wrapped; /** Creates a new instance of SoapBar. Each bar has three measured dimensions. * As parameters, the dimensions are not in any particular order. * The parameter values should be positive. * Within the object, length must be the largest, height must be smallest. */ public Soap(double dimension1, double dimension2, double dimension3) { this.wrapped = false; this.length = dimension1; //this.length = 300; //just for testing the test --- will cause many failures this.width = dimension2; this.height = dimension3; if (dimension1 < dimension2) { //reverse length and width this.length = dimension2; this.width = dimension1; } double num2 = this.width; if (num2 < dimension3) { //reverse width and height this.width = dimension3; this.height = num2; this.height = dimension3; //error -- just for testing. } //At this point, we can be sure that height is the smallest of the three. double num1 = this.length; num2 = this.width; if (num1 < num2) { //reverse length and width this.length = num2; this.width = num1; } //end constructor } public double getLength() { return this.length; } public double getWidth () { return this.width; } public double getHeight () { return this.height; } //////////////////////////////////////////////////////////////////////// /** Return a string representation of a SoapBar. * Needed only for testing -- not part of the solution. */ public String toString() { String wrappedMessage; if (wrapped) { wrappedMessage = "(wrapped)"; } else { wrappedMessage = "(not wrapped)"; } return "[SoapBar " + wrappedMessage + " L=" + getLength() + " W=" + getWidth() + " H=" + getHeight() + "]"; } /** Generate one random bar, returning it. * Needed only for testing -- not required for solution. */ public static Soap createRandomBar() { double dim1, dim2, dim3; double largestLength = 7.0; double smallestHeight = 0.5; double dimensionRange = largestLength - smallestHeight; dim1 = Math.random() * dimensionRange + smallestHeight; dim2 = Math.random() * dimensionRange + smallestHeight; dim3 = Math.random() * dimensionRange + smallestHeight; Soap oneBar = new Soap(dim1, dim2, dim3); return oneBar; } //end class SoapBar }