InterviewSolution
| 1. |
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 superclass Plane is defined to represent a line and a subclass Circle to find the length of the radius and the area of the circle by using the required data members of the superclass.Some of the members of both classes are given below: Class name: Plane Data members/instance variables: x1: to store the x-coordinate of the first endpoint y1: to store the y-coordinate of the first endpoint 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 endpoint y2: to store the y-coordinate of the second endpoint 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 classesvoid 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 voidfindArea(): to find the area of a circle using the formula: πr2 . The value of pie(π) is 22/7 or 3.14 void show(): to display both the coordinates along with the length of the radius and 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 find Area() and voidShow() The main function and algorithm need not be written. |
|
Answer» class Plane { int x1; 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 nx1, int ny1, int nx2, int ny2) { super(nx1, nx2); x2=nx2; y2=ny2; } public void fmdRadius() { radius=Math.sqrt(Math.pow((x2-x1), 2)+Math.pow((y2-y1), 2))/2.0; } public void findArea() { 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 Coordinate { //main method created so that the program can be executed public static void main(String args[]) { Circle obj=new Circle(2, 3, 4, 5); obj.findRadius(); obj.findArea(); obj.show(); } } |
|