Sunday , 22 July 2018
Latest Posts
Home » Inheritance Related Programs » [Question 11 (Inheritance)] ISC 2013 (Theory) Paper Solved

# [Question 11 (Inheritance)] ISC 2013 (Theory) Paper Solved

#### Question:

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( … ), void 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.

[Note: We will be writing the main() function also in this program so as to familiarize the students on how to run programs based on the concept of inheritance.]

#### Programming Code:

```/**
* The superclass Perimeter calculate the perimeter of a parallelogram and
* the subclass Area calculates the area of a parallelogram
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 11
*/

import java.io.*;
class Perimeter //superclass
{
double a,b;
Perimeter(double x, double y)
{
a=x;
b=y;
}
double Calculate()
{
double p=2*(a+b);
return p;
}
void show()
{
System.out.println("Length = "+a);
System.out.println("Perimeter = "+Calculate()); //printing perimeter by calling Calculate()
}
} //end of superclass Perimeter

class Area extends Perimeter //subclass
{
double h, area;
Area(double x, double y, double z)
{
super(x,y); //initializing data members of superclass by calling its constructor
h=z;
}
void doarea()
{
area=b*h;
}
void show()
{
super.show(); //calling the superclass function show()
System.out.println("Height = "+h);
System.out.println("Area = "+area);
}
} //end of subclass Area

/* In your exams you don't need to write the below given code
We are writing it so as to familiarize the students on how to run
programs based on the concept of inheritance.*/

public class Question11_ISC2013 //Class which will contain the main() method and execute the program
{
public static void main(String args[])throws IOException
{
System.out.print("Enter The Length : ");
System.out.print("Enter The Breadth : ");
System.out.print("Enter The Height : ");

Area ob=new Area(x,y,z); //creating object of subclass
System.out.println("*** Output ***");
ob.doarea();
ob.show(); //calling show() function of subclass
}
}
```

#### Output:

Enter The Length : 5
Enter The Breadth : 7
Enter The Height : 9
*** Output ***
Length = 5.0