Write a Rectangle class that has two instance variables: length and width of the
ID: 3677286 • Letter: W
Question
Write a Rectangle class that has two instance variables: length and width of the rectangle (both are floating-point numbers). The class should have two constructors: one with two parameters for the two instance variables, and a no-argument constructor (set both instance variables to zero). Include the following:
Three accessor methods: one to return the length, one to return the width and one called computeArea that returns the area of the rectangle.
Two mutator methods: one to set the value of the length and the other to set the value of the width.
Then write another class called Cuboid that is derived from Rectangle and has an additional instance variable: height of the cuboid. This class also has two constructors: one with three parameters for the three instance variables, and a no-argument constructor (set all instance variables to zero). The class also includes:
Two accessor method: one that returns the height of the cuboid and one called computeVolume that returns the volume of the cuboid.
A mutator method that set the value of the heigth.
Override the computeArea to return the area of the cuboid.
Finally, write a driver program that tests the two classes and all the methods in both classes.
Explanation / Answer
// Rectangle class
public class Rectangle {
private float length;
private float width;
public Rectangle() {
length = 0;
width = 0;
}
public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}
public float getLength() {
return length;
}
public void setLength(float length) {
this.length = length;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float computeArea(){
return length*width;
}
}
// Cuboid class
class Cuboid extends Rectangle{
private float height;
public Cuboid() {
super(0,0);
height = 0;
}
public Cuboid(float length, float width,float height) {
super(length,width);
this.height = height;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public float computeArea(){
return getLength()*getWidth();
}
public float computeVolume(){
return computeArea()*height;
}
}
//Tester class
class TestRecAndCuboid{
public static void main(String[] args) {
// creating Rectangle object
Rectangle r1 = new Rectangle();
//setting length and width
r1.setLength(10);
r1.setWidth(20);
System.out.println("Length and Width of R1: "+r1.getLength()+", "+r1.getWidth());
System.out.println("Area of R1: "+r1.computeArea());
// creating Cuboid
Cuboid c1 = new Cuboid(3, 4, 5);
System.out.println("Dimension of cuboid: "+c1.getLength()+","+c1.getWidth()+","+c1.getHeight());
System.out.println("Area and Volume of C1: "+c1.computeArea()+", and "+c1.computeVolume());
}
}
/*
Output:
Length and Width of R1: 10.0, 20.0
Area of R1: 200.0
Dimension of cuboid: 3.0,4.0,5.0
Area and Volume of C1: 12.0, and 60.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.