Apply inheritance to write code to calculate area, surface area, and volume of r
ID: 3682916 • Letter: A
Question
Apply inheritance to write code to calculate area, surface area, and volume of rectangular objects. Write a super class first and then write a subclass that inherits from the super class to compute areas of rectangles including squares. Finally writea another subclass that inherited from the class above to compute surface areasa nd volumes of cuboids. Pay attention to code-reusability in coding. Override toString() methods in all developed classes so it will return the proper data from each object. Write a driver code to test your program. Make sure to create different rectangular objects with a variety of data to compute their areas, surface areas, or/and volumes, whatever applicable to the instantiated objects. Document and print the source code and save all files.
Explanation / Answer
Polygon.java
package org.students;
//Polygon is an abstract class.
public abstract class Polygon
{ //These area abstract methods(Methods with out body)
public abstract double calculateArea();
public abstract double calculateSurfaceArea();
public abstract double calculateVolume();
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
Rectangle.java
package org.students;
public abstract class Rectangle extends Polygon {
protected double length = 0.0;
protected double breadth = 0.0;
//Method to calculate Area of Rectangle
@Override
public double calculateArea() {
double area = length * breadth;
return area;
}
// toString Method To display the contents of the Object.
public String toString() {
System.out.println("==========Rectangle ===============");
System.out.println("Length : " + length);
System.out.println("Breadth : " + breadth);
System.out.println("Area of the Rectangle : " + calculateArea());
return "";
}// end of method toString()
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
RectangularCuboid.java
package org.students;
public class RectangularCuboid extends Rectangle {
protected double length = 0.0;
protected double height = 0.0;
protected double breadth=0.0;
//Method to calculate Surface Area of Rectangular Cuboid
@Override
public double calculateSurfaceArea() {
double sa = 2*length*breadth + 2*breadth*height + 2*height*length;
return sa;
}
//Method to calculate Volume of Rectangular cuboid
@Override
public double calculateVolume() {
double vol=length*breadth*height;
return vol;
}
//Default constructor which doesnt take any arguments.
public RectangularCuboid() {
super();
}
//Parameterized constructor which takes arguments.
public RectangularCuboid(double length, double breadth, double height) {
super();
//to initialize the super class variable
super.length=length;
//to initialize the super class variable
super.breadth=breadth;
this.length=length;
this.height = height;
this.breadth=breadth;
}
// // toString Method To display the contents of the Object.
@Override
public String toString() {
System.out.println(super.toString());
System.out.println("==================Rectangular cuboid=================");
System.out.println("Length : " +length);
System.out.println("Breadth : " +breadth);
System.out.println("Height : " + height);
System.out.println("Surface Area : " + calculateSurfaceArea());
System.out.println("Volume : " + calculateVolume());
System.out
.println("=====================================================");
return "";
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Square.java
package org.students;
public abstract class Square extends Polygon {
protected double side = 0.0;
// Method to calculate Area of the square
public double calculateArea() {
double area = side * side;
return area;
}
// toString Method To display the contents of the Object.
public String toString() {
System.out.println("===================Square================");
System.out.println("Side : " + side);
System.out.println("Area : " + calculateArea());
System.out
.println("=====================================================");
return "";
}// end of method toString()
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
SquareCuboid.java
package org.students;
public class SquareCuboid extends Square {
protected double side = 0.0;
// Method to calculate Surface Area of the Cuboid.
@Override
public double calculateSurfaceArea() {
double sa=6*side*side;
return sa;
}
// Method to calculate Surface Volume of the Cuboid.
@Override
public double calculateVolume() {
double vol=side*side*side;
return vol;
}
//Default constructor which doesnt take any arguments.
public SquareCuboid() {
super();
}
//Parameterized constructor which takes arguments.
public SquareCuboid(double side) {
super();
//to initialize the super class variable
super.side=side;
this.side = side;
}
// toString Method To display the contents of the Object.
@Override
public String toString() {
System.out.println(super.toString());
System.out.println("================Cuboid=====================");
System.out.println("Side : " + side);
System.out.println("Surface Area : " + calculateSurfaceArea());
System.out.println("Volume : " + calculateVolume());
System.out
.println("=====================================================");
return "";
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Test.java
package org.students;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
double base = 0;
double width = 0;
double height = 0;
double side = 0;
//Take the input values from the keyboard
Scanner sc=new Scanner(System.in);
System.out.print("Enter the length of the Rectangle:");
base=sc.nextDouble();
System.out.print("Enter the breadth of the Rectangle:");
width=sc.nextDouble();
System.out.print("Enter the height of the Rectangle:");
height=sc.nextDouble();
System.out.print("Enter the Side of the Square:");
side=sc.nextDouble();
//Created the RectangularCuboid Object.
RectangularCuboid rc=new RectangularCuboid(base, height, width);
System.out.println(rc.toString());
//Created the Square Cuboid Object.
SquareCuboid sqc=new SquareCuboid(side);
System.out.println(sqc.toString());
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
output:
Enter the length of the Rectangle:3
Enter the breadth of the Rectangle:4
Enter the height of the Rectangle:5
Enter the Side of the Square:2
==========Rectangle ===============
Length : 3.0
Breadth : 5.0
Area of the Rectangle : 15.0
==================Rectangular cuboid=================
Length : 3.0
Breadth : 5.0
Height : 4.0
Surface Area : 94.0
Volume : 60.0
=====================================================
===================Square================
Side : 2.0
Area : 4.0
=====================================================
================Cuboid=====================
Side : 2.0
Surface Area : 24.0
Volume : 8.0
=====================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.