SHARE
TWEET

Untitled

a guest Nov 13th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package classboxdatavalidation;
  2.  
  3. public class Box {
  4.     private double length;
  5.     private double width;
  6.     private double height;
  7.     public Box(double length, double width, double height) {
  8.         setLength(length);
  9.         setWidth(width);
  10.         setHeight(height);
  11.     }
  12.  
  13.     private void setLength(double length) {
  14.         if (length <= 0) {
  15.             String word = "Length";
  16.             throw new IllegalArgumentException(getMessage(word));
  17.         }
  18.         this.length = length;
  19.     }
  20.  
  21.     private void setWidth(double width) {
  22.         if (width <= 0) {
  23.             String word = "Width";
  24.             throw new IllegalArgumentException(getMessage(word));
  25.         }
  26.         this.width = width;
  27.     }
  28.  
  29.  
  30.     private void setHeight(double height) {
  31.         if (height <= 0) {
  32.             String word = "Height";
  33.             throw new IllegalArgumentException(getMessage(word));
  34.         }
  35.         this.height = height;
  36.     }
  37.     public double calculateSurfaceArea(double length, double width, double height){
  38.         return 2*length*width + 2*length*height + 2*width*height;
  39.     }
  40.     public double calculateLateralSurfaceArea(double length, double width, double height) {
  41.         return 2*length*height + 2*width*height;
  42.     }
  43.     public double calculateVolume(double length, double width, double height) {
  44.         return length*width*height;
  45.     }
  46.     private String getMessage(String word) {
  47.         return word+" cannot be zero or negative.";
  48.     }
  49.  
  50.     @Override
  51.     public String toString() {
  52.         return String.format("Surface Area - %.2f%nLateral Surface Area - %.2f%nVolume - %.2f",
  53.                 this.calculateSurfaceArea(length,width,height),
  54.                 this.calculateLateralSurfaceArea(length,width,height),
  55.                 this.calculateVolume(length,width,height));
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top