Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package classboxdatavalidation;
- public class Box {
- private double length;
- private double width;
- private double height;
- public Box(double length, double width, double height) {
- setLength(length);
- setWidth(width);
- setHeight(height);
- }
- private void setLength(double length) {
- if (length <= 0) {
- String word = "Length";
- throw new IllegalArgumentException(getMessage(word));
- }
- this.length = length;
- }
- private void setWidth(double width) {
- if (width <= 0) {
- String word = "Width";
- throw new IllegalArgumentException(getMessage(word));
- }
- this.width = width;
- }
- private void setHeight(double height) {
- if (height <= 0) {
- String word = "Height";
- throw new IllegalArgumentException(getMessage(word));
- }
- this.height = height;
- }
- public double calculateSurfaceArea(double length, double width, double height){
- return 2*length*width + 2*length*height + 2*width*height;
- }
- public double calculateLateralSurfaceArea(double length, double width, double height) {
- return 2*length*height + 2*width*height;
- }
- public double calculateVolume(double length, double width, double height) {
- return length*width*height;
- }
- private String getMessage(String word) {
- return word+" cannot be zero or negative.";
- }
- @Override
- public String toString() {
- return String.format("Surface Area - %.2f%nLateral Surface Area - %.2f%nVolume - %.2f",
- this.calculateSurfaceArea(length,width,height),
- this.calculateLateralSurfaceArea(length,width,height),
- this.calculateVolume(length,width,height));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement