Advertisement
cnencheva

Untitled

Nov 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package box;
  2.  
  3. public class Box {
  4. private double length;
  5. private double width;
  6. private double height;
  7.  
  8. public Box(double length, double width, double height) {
  9. setLength(length);
  10. setWidth(width);
  11. setHeight(height);
  12. }
  13.  
  14. private void setLength(double length) {
  15. if (length <= 0) {
  16. String word = "Length";
  17. throw new IllegalArgumentException(getMessage(word));
  18. }
  19. this.length = length;
  20. }
  21.  
  22. private void setWidth(double width) {
  23. if (width <= 0) {
  24. String word = "Width";
  25. throw new IllegalArgumentException(getMessage(word));
  26. }
  27. this.width = width;
  28. }
  29.  
  30.  
  31. private void setHeight(double height) {
  32. if (height <= 0) {
  33. String word = "Height";
  34. throw new IllegalArgumentException(getMessage(word));
  35. }
  36. this.height = height;
  37. }
  38.  
  39. public double calculateSurfaceArea() {
  40. return (2 * this.length * this.width) + (2 * this.length * this.height) + (2 * this.width * height);
  41. }
  42.  
  43. public double calculateLateralSurfaceArea() {
  44. return (2 * this.length * this.height) +
  45. (2 * this.width * this.height);
  46. }
  47.  
  48. public double calculateVolume() {
  49. return this.length * this.width * this.height;
  50. }
  51.  
  52. private String getMessage(String word) {
  53. return word + " cannot be zero or negative.";
  54. }
  55.  
  56. @Override
  57. public String toString() {
  58. return String.format("Surface Area - %.2f%nLateral Surface Area - %.2f%nVolume - %.2f",
  59. this.calculateSurfaceArea(),
  60. this.calculateLateralSurfaceArea(),
  61. this.calculateVolume());
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement