Plane Inheritance ISC Computer Science 2015 Theory

A line on a plane can be represented by coordinates of the two end points p1 and p2 as p1(x1, y1) and p2(x2, y2).

A super class Plane is defined to represent a line and a sub class Circle to find the length of the radius and the area of circle by using the required data members of super class.

Some of the members of both the classes are given below:

Class name: Plane
Data members/methods:
x1: to store the x-coordinate of the first end point
y1: to store the y-coordinate of the first end point
Member functions/methods:
Plane(int nx, int ny): parameterized constructor to assign the data members x1 = nx and y1 = ny
void show(): to display the coordinates

Class name: Circle
Data members/instance variables:
x2: to store the x-coordinate of the second end point
y2: to store the y-coordinate of the second end point
radius: double variable to store the radius of the circle
area: double variable to store the area of the circle
Member functions/methods:
Circle(...): parameterized constructor to assign values to data members of both the classes
void findRadius(): to calculate the length of the radius using the formula:

assuming that x1, x2, y1, y2 are the coordinates of the two ends of the diameter of a circle
void show(): to display both the coordinates along with the length of the radius and the area of the circle

Specify the class Plane giving details of the constructor and void show(). Using the concept of inheritance, specify the class Circle giving details of the constructor, void findRadius(), void findArea() and void show().

The main() function and algorithm need not be written.

import java.util.Scanner;
class Plane{
    protected int x1;
    protected int y1;
    public Plane(int nx, int ny){
        x1 = nx;
        y1 = ny;
    }
    public void show(){
        System.out.println("p1: " + x1 + ", " + y1);
    }
}
class Circle extends Plane{
    int x2;
    int y2;
    double radius;
    double area;
    public Circle(int a1, int b1, int a2, int b2){
        super(a1, b1);
        x2 = a2;
        y2 = b2;
        radius = 0.0;
        area = 0.0;
    }
    public void findRadius(){
        int x = x2 - x1;
        int y = y2 - y1;
        radius = Math.sqrt(x * x + y * y) / 2;
        area = Math.PI * radius * radius;
    }
    public void show(){
        super.show();
        System.out.println("p2: " + x2 + ", " + y2);
        System.out.println("Radius: " + radius);
        System.out.println("Area: " + area);
    }
}
class MyInheritance{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("x1 of point p1: ");
        int a1 = Integer.parseInt(in.nextLine());
        System.out.print("y1 of point p1: ");
        int b1 = Integer.parseInt(in.nextLine());
        System.out.print("x2 of point p2: ");
        int a2 = Integer.parseInt(in.nextLine());
        System.out.print("y2 of point p2: ");
        int b2 = Integer.parseInt(in.nextLine());
        Circle obj = new Circle(a1, b1, a2, b2);
        obj.findRadius();
        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