Advertisement
AngelKejov

Untitled

Nov 1st, 2021
1,006
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ClassBoxDataValidation;
  2.  
  3. public class Box {
  4.  
  5.     private double length;
  6.     private double width;
  7.     private double height;
  8.  
  9.     public Box(double length, double width, double height) {
  10.         this.setHeight(height);
  11.         this.setLength(length);
  12.         this.setWidth(width);
  13.     }
  14.  
  15.     private void setLength(double length) {
  16.         if (length <= 0) {
  17.             throw new IllegalArgumentException("Length cannot be zero or negative.");
  18.         }
  19.         this.length = length;
  20.     }
  21.  
  22.     private void setHeight(double height) {
  23.         if (height <= 0) {
  24.             throw new IllegalArgumentException("Height cannot be zero or negative.");
  25.         }
  26.         this.height = height;
  27.     }
  28.  
  29.     private void setWidth(double width) {
  30.         if (width <= 0) {
  31.             throw new IllegalArgumentException("Width cannot be zero or negative.");
  32.         }
  33.         this.width = width;
  34.     }
  35.  
  36.     public double calculateSurfaceArea() {
  37.         return 2 * (length * width) + 2 * (length * height) + 2 * (width * height);
  38.     }
  39.  
  40.     public double calculateLateralSurfaceArea() {
  41.         return 2 * (length * height) + 2 * (width * height);
  42.     }
  43.  
  44.     public double calculateVolume() {
  45.         return  length * width * height;
  46.     }
  47. }
  48.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement