Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package box;
- 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() {
- return (2 * this.length * this.width) + (2 * this.length * this.height) + (2 * this.width * height);
- }
- public double calculateLateralSurfaceArea() {
- return (2 * this.length * this.height) +
- (2 * this.width * this.height);
- }
- public double calculateVolume() {
- return this.length * this.width * this.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(),
- this.calculateLateralSurfaceArea(),
- this.calculateVolume());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement