/* * PaperSoapHarness.java * * Created on April 22, 2003, 1:47 PM */ /** A class used to test classes and methods of the Paper Stree Soap Company * project. * This version is part of the sample solution for Homework #2. * */ public class PaperSoapHarness { /** Creates a new instance of PaperSoapHarness */ public PaperSoapHarness() { } /** Test method. Uses the standard signature for main. * Tests all combinations of Paper within a certain range of the dimensions, * then tests Soap in a similar fashiom. * @param args not used. */ public static void main(String[] args) { final double smallestSize = 0.5; //sise (inches) final double largestSize = 20.0; //size (inches) final double increment = 0.25; //the amount by which to increment a dimension //Test Paper first, all combinations of dimensions within the range. System.out.println("Beginning Paper tests."); int paperTestsSucceeded = 0; int paperTestsFailed = 0; double dimension1 = smallestSize; //starting value while (dimension1 <= largestSize) { double dimension2 = smallestSize; //starting value while (dimension2 < largestSize) { boolean testResult = testOnePieceOfPaper(dimension1, dimension2); if (testResult == true) { paperTestsSucceeded++; } else { paperTestsFailed++; } dimension2 = dimension2 + increment; } dimension1 = dimension1 + increment; } //all paper tests are done. System.out.println("Paper tests completed: " + paperTestsSucceeded + " succeeded, " + paperTestsFailed + " failed."); //Now test Soap. //For variety, nested for-loops are used. System.out.println("Beginning soap tests."); int soapTestsSucceeded = 0; int soapTestsFailed = 0; for (double dim1 = smallestSize; dim1 <= largestSize; dim1 = dim1 + increment) { for (double dim2 = smallestSize; dim2 <= largestSize; dim2 = dim2 + increment) { for (double dim3 = smallestSize; dim3 <= largestSize; dim3 = dim3 + increment) { boolean testResult = testOnePieceOfSoap(dim1, dim2, dim3); if (testResult == true) { soapTestsSucceeded++; } else { soapTestsFailed++; } } } } //all soap tests are done. System.out.println("Soap tests completed: " + soapTestsSucceeded + " succeeded, " + soapTestsFailed + " failed."); //end main } /** Test one piece of paper. * To be valid, the length and width must be in proper order, and both * of the original dimensions must be found. * The "static" on this method means it uses no members of * its own class. This is needed in order to call the method from main, * which is static. */ public static boolean testOnePieceOfPaper(double oneDim, double otherDim) { Paper aPaper = new Paper(oneDim, otherDim); boolean result; double plen = aPaper.getLength(); double pwidth = aPaper.getWidth(); if (plen < pwidth) { result = false; } else { if ( !(oneDim == plen || oneDim == pwidth) || !(otherDim == plen || otherDim == pwidth)) { //error -- one of the dimensions is not found result = false; } else { result = true; } } if (result == false) { String paperinfo = aPaper.toString(); System.out.println("Paper failure (dim1= " + oneDim + " dim2=" + otherDim + ") " + paperinfo); } return result; //end testOnePieceOfPaper } /** Test one piece of soap. * To be valid, the length and width must be in proper order, and all 3 * of the original dimensions must be found. * The "static" on this method means it uses no members of * its own class. This is needed in order to call the method from main, * which is static. */ public static boolean testOnePieceOfSoap(double dim1, double dim2, double dim3) { Soap aSoap = new Soap(dim1, dim2, dim3); boolean result; double plen = aSoap.getLength(); double pwidth = aSoap.getWidth(); double pheight = aSoap.getHeight(); if (plen < pwidth || pheight > pwidth) { result = false; //dimensions are out of order } else { if ( !(dim1 == plen || dim1 == pwidth || dim1 == pheight) || !(dim2 == plen || dim2 == pwidth || dim2 == pheight) || !(dim3 == plen || dim3 == pwidth || dim3 == pheight)) { //error -- one of the dimensions is not found result = false; } else { result = true; } } if (result == false) { String Soapinfo = aSoap.toString(); System.out.println("Soap failure (dim1=" + dim1 + " dim2=" + dim2 + " dim3= " + dim3 + ") " + Soapinfo); } return result; //end testOneBarOfSoap } //end test harness }