Perimeter Inheritance ISC Computer Science 2013 Theory

A super class 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 super class. The details are given below:

Class name: Perimeter
Data members/instance variables:
a: to store the length in decimal
b: to store the breadth in decimal
Member functions:
Perimeter(...): parameterized constructor to assign values to data members
double calculate(): calculate and return the perimeter of a parallelogram as 2 × (length + breadth)
void show(): to display the data members along with the perimeter of the parallelogram

Class name: Area
Data members/instance variables:
h: to store the height in decimal
area: to store the area of the parallelogram
Member functions:
Area(...): parameterized constructor to assign values to data members of both the classes
void doArea(): compute the area as (breadth 
× height)
void show(): display the data members of both classes along with the area and perimeter of the parallelogram.

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.

import java.util.Scanner;
class Perimeter{
    protected double a;
    protected double b;
    public Perimeter(double x, double y){
        a = x;
        b = y;
    }
    public double calculate(){
        return 2 * (a + b);
    }
    public 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;
    public Area(double x, double y, double z){
        super(x, y);
        h = z;
        area = 0.0;
    }
    public void doArea(){
        area = b * h;
    }
    public void show(){
        super.show();
        System.out.println("Area = " + area);
    }
}
class Parallelogram{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Length: ");
        double l = Double.parseDouble(in.nextLine());
        System.out.print("Breadth: ");
        double b = Double.parseDouble(in.nextLine());
        System.out.print("Height: ");
        double h = Double.parseDouble(in.nextLine());
        Area obj = new Area(l, b, h);
        obj.doArea();
        obj.show();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory