POOL AREA

 The Swimming Company has asked you to write an application that calculates the volume of the cuboid-shaped pools.

1 Write a class name Rectangle with two variables width and length of type double.
The class needs to have one constructor with parameters width and length both of type double and it needs to initialize the fields. 
In case the width parameter is less than 0 it needs to set the width field value to 0
same for the length.
Write the following method
  • Method named getWidth without parameters, it needs to return the value of the width field.
  • Method named getLength without parameters, it needs to return the value of the length field.
  • Method named getArea without parameters, it needs to return the value of the area field.
2 Write a class with a name cuboid that extends the Rectangle class. The class needs one field(instance variable) with the name height of typ[e double.
The class needs to have one constructor with three parameters width, length, and height all of type double. It needs to call the parent constructor and initialize a height field.
In case the height parameter is less than 0 it needs to set the height field value to 0.
Writ the following methods
  • Method named getHeight without parameters, it needs to return the value of the height field.
  • Method named getVolume without parameters, it needs to return the value of volume field.
Soln: 
package com.cnc;

public class Rectangle {
private double width;
private double length;

public double getWidth() {
return width;
}

public void setWidth(double width) {
if(width<0) this.width = 0;
else {
this.width = width;
}
}

public double getLength() {
return length;
}

public void setLength(double length) {
if(length<0) this.length = 0;
else {
this.length = length;
}
}

public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
public double getArea(){
return length*width;
}
}
class Cuboid extends Rectangle{
private double height;
public Cuboid(double width, double length) {
super(width, length);
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
if(height<0) this.height = 0;
else {
this.height = height;
}
}
public double getVolume(){
return height*getArea();
}

public Cuboid(double width, double length, double height) {
super(width, length);
this.height = height;
}

}

Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

LARGEST PRIME FACTOR OF A NUMBER