Perimeter and Area Inheritance Program in Java and Python
ISC 2013 inheritance solution using superclass Perimeter and subclass Area with algorithm, explanation, Java program and simple Python program.
Question:
A superclass Perimeter has been defined to calculate the perimeter of a parallelogram. Define a subclass Area to compute the area of the parallelogram by using the required data members of the superclass. The details are given below:
Specify the class Perimeter giving details of the constructor, double Calculate() and void show(). Using the concept of inheritance, specify the class Area giving details of the constructor, void doarea() and void show().
The main function and algorithm need not be written in the original theory answer, but a main method is included below to show how the classes can be executed.
Algorithm:
Step 1: Start.
Step 2: Define a superclass named Perimeter.
Step 3: Declare decimal variables a and b to store length and breadth.
Step 4: In the Perimeter constructor, accept two values and assign them to a and b.
Step 5: In Calculate(), return 2 * (a + b) as the perimeter.
Step 6: In show() of Perimeter, display length, breadth and perimeter.
Step 7: Define a subclass named Area that extends Perimeter.
Step 8: Declare decimal variables h and area.
Step 9: In the Area constructor, call the superclass constructor using super(x, y) and store height in h.
Step 10: In doarea(), calculate area = b * h.
Step 11: In show() of Area, call super.show() to display inherited data and perimeter.
Step 12: Display height and area.
Step 13: In main(), accept length, breadth and height.
Step 14: Create an object of Area, call doarea(), then call show().
Step 15: Stop.
Explanation:
This program demonstrates single inheritance. The superclass Perimeter stores the two values that are needed for calculating the perimeter of a parallelogram: length and breadth. These values are placed in the superclass because they form the basic data for the shape. The subclass Area reuses the breadth from the superclass and adds the height, which is needed only for the area calculation.
The constructor of Perimeter receives two decimal values and stores them in a and b. The method Calculate() returns the perimeter using the formula 2 * (a + b). The show() method of the superclass displays the length, breadth and perimeter. This keeps all perimeter-related work inside the superclass.
The subclass Area extends Perimeter, so it inherits a, b, Calculate() and the superclass show() method. Its constructor accepts three values: length, breadth and height. The first two values are passed to the superclass constructor using super(x, y). This is important because inherited data members should be initialized by the superclass constructor. The third value is stored in the subclass variable h.
The area of a parallelogram is calculated as breadth multiplied by height, so doarea() stores b * h in area. The subclass also defines its own show() method. This is method overriding. Inside it, super.show() first displays the length, breadth and perimeter using the superclass version. Then the subclass adds its own output: height and area. This structure avoids repeating perimeter-display code in the subclass and clearly shows how inherited members can be reused.
Java Program:
/**
* The superclass Perimeter calculates the perimeter of a parallelogram.
* The subclass Area calculates the area of the parallelogram.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 11
*/
import java.util.Scanner;
class Perimeter
{
double a;
double b;
Perimeter(double x, double y)
{
a = x;
b = y;
}
double Calculate()
{
return 2 * (a + b);
}
void show()
{
System.out.println("Length = " + a);
System.out.println("Breadth = " + b);
System.out.println("Perimeter = " + Calculate());
}
}
class Area extends Perimeter
{
double h;
double area;
Area(double x, double y, double z)
{
super(x, y); // Initialize inherited length and breadth.
h = z;
}
void doarea()
{
// Area of a parallelogram is breadth multiplied by height.
area = b * h;
}
void show()
{
super.show(); // Display length, breadth and perimeter.
System.out.println("Height = " + h);
System.out.println("Area = " + area);
}
}
public class Question11_ISC2013
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Length: ");
double x = sc.nextDouble();
System.out.print("Enter The Breadth: ");
double y = sc.nextDouble();
System.out.print("Enter The Height: ");
double z = sc.nextDouble();
Area ob = new Area(x, y, z);
System.out.println("*** Output ***");
ob.doarea();
ob.show();
}
}Equivalent Python Program:
class Perimeter:
def __init__(self, x, y):
self.a = x
self.b = y
def Calculate(self):
# Perimeter of a parallelogram is 2 * (length + breadth).
return 2 * (self.a + self.b)
def show(self):
print("Length =", self.a)
print("Breadth =", self.b)
print("Perimeter =", self.Calculate())
class Area(Perimeter):
def __init__(self, x, y, z):
super().__init__(x, y) # Initialize inherited length and breadth.
self.h = z
self.area = 0
def doarea(self):
# Area of a parallelogram is breadth multiplied by height.
self.area = self.b * self.h
def show(self):
super().show()
print("Height =", self.h)
print("Area =", self.area)
x = float(input("Enter The Length: "))
y = float(input("Enter The Breadth: "))
z = float(input("Enter The Height: "))
ob = Area(x, y, z)
print("*** Output ***")
ob.doarea()
ob.show()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.