Question: [Java] I'm not sure why I can't get the right output You should not print error messages from constructors or methods of object. In the
[Java] I'm not sure why I can't get the right output
You should not print error messages from constructors or methods of object. In the real world, you might throw an exception when the parameter is invalid, but we have not covered exceptions yet. We will just be sure the data in our objects are valid. Follow the directions below to add error checking to each class.
Use proper if structure. Do not do unnecessary tests . Do not have empty if or else blocks
PaintJob
In the constructor, if either the length or the width is <= 0, set both width and length to 0
In setDimensions, only change the width and the length if both are > 0. If either is 0, don't make any changes.
Use the following file:
PaintJobTester.java
/** * Tests the PaintJob class */ public class PaintJobTester { public static void main(String[] args) { //construct a PaintJob with valid parameters PaintJob job = new PaintJob(10.5, 9.0); System.out.printf("Total cost: %.2f%n", job.getJobCost()); System.out.println("Expected: 139.27"); //try to set invalid parameters job.setDimensions(10, -10); System.out.printf("Total cost: %.2f%n", job.getJobCost()); System.out.println("Expected: 139.27"); //construct a PaintJob with invalid parameters job = new PaintJob(0, 12); System.out.printf("Length: %.2f%n", job.getLength()); System.out.println("Expected: 0.00"); System.out.printf("Width: %.2f%n", job.getWidth()); System.out.println("Expected: 0.00"); } } [Here's my code]
/** * Calculate the cost of paint job * @author Jamie J */ public class PaintJob { public static final int SQ_INCHES_PER_SQ_FOOT = 144; public static final double WALL_HEIGHT_IN_FEET = 8; public static final double DOOR_HEIGHT_IN_INCHES = 80; public static final double DOOR_WIDTH_IN_INCHES = 32; public static final double WINDOW_HEIGHT_IN_FEET = 5; public static final double WINDOW_WIDTH_IN_FEET = 4;
private double lengthOfDorm; private double widthOfDorm; private double surfaceArea; private double cost;
/** * Constructor initializes instance variables * @param length length of the dorm room * @param width width of the dorm room */ public PaintJob(double length, double width) { if(length <= 0 || width <= 0) { length = 0; width = 0; } else { lengthOfDorm = length; widthOfDorm = width;
} surfaceArea = 0; cost = 0; }
/** * Get the length of the dorm room * @return length of the dorm room */ public double getLength() { return lengthOfDorm; }
/** * Get the width of the dorm room * @return width of the dorm room */ public double getWidth() { return widthOfDorm; }
/** * Get the new values of width and length * @param theLength new length value * @param theWidth new width value */ public void setDimensions(double theLength, double theWidth) { if(theLength > 0 && theWidth > 0) { lengthOfDorm = theLength; widthOfDorm = theWidth; } else { lengthOfDorm = lengthOfDorm; widthOfDorm = widthOfDorm; } }
/** * Get the surface area to paint * @return surface area */ public double getSurfaceArea() { double doorAreaInFT = DOOR_HEIGHT_IN_INCHES * DOOR_WIDTH_IN_INCHES / SQ_INCHES_PER_SQ_FOOT; surfaceArea = (lengthOfDorm* WALL_HEIGHT_IN_FEET * 2) + (widthOfDorm * WALL_HEIGHT_IN_FEET * 2) + (lengthOfDorm * widthOfDorm) - (WINDOW_HEIGHT_IN_FEET * WINDOW_WIDTH_IN_FEET) - doorAreaInFT; return surfaceArea; }
/** * Get the cost of paint used * @return cost of paint used */ public double getCostOfPaint() { double paintPrice = 31.95; double spaceCovered = 300; cost = (surfaceArea / spaceCovered) * paintPrice; return cost; }
/** * Get the total cost including the labor * @return total cost */ public double getJobCost() { double labor = 100; cost = cost + labor; return cost; } }
[The output I got]
fail fail pass pass
Total cost: 100.00 Expected: 139.27 Total cost: 200.00 Expected: 139.27 Length: 0.00 Expected: 0.00 Width: 0.00 Expected: 0.00
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
