-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3(Practical).java
More file actions
97 lines (93 loc) · 2.53 KB
/
Q3(Practical).java
File metadata and controls
97 lines (93 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Q3. Define an abstract class Shape in package P1. Inherit two more
classes: Rectangle in package P2 and Circle in package P3. Write a program to
ask the user for the type of shape and then using the concept of dynamic method
dispatch, display the area of the appropriate subclass. Also write appropriate
methods to read the data. The main() function should not be in any package.
/**** P1/Shape.java ****/
package P1;
public abstract class Shape {
protected abstract void getData() throws java.io.IOException;
public abstract double area() throws java.io.IOException;
}
/**** P2/Rectangle.java ****/
package P2;
import java.io.*;
import P1.*;
public class Rectangle extends Shape {
private double length;
private double breadth;
protected void getData() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in
));
System.out.print("Enter Length of Rectangle: ");
length = Double.parseDouble(br.readLine());
System.out.print("Enter Breadth of Rectangle: ");
breadth = Double.parseDouble(br.readLine());
}
public double area() throws IOException {
getData();
return length * breadth;
}
}
/**** P3/Circle.java ****/
package P3;
import java.io.*;
import P1.*;
public class Circle extends Shape {
private double radius;
protected void getData() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in
));
System.out.print("Enter Radius of Circle: ");
radius = Double.parseDouble(br.readLine());
}
public double area() throws IOException {
getData();
return Math.PI * radius * radius;
}
}
/**** Main.java ****/
import java.io.*;
import P1.*;
import P2.*;
import P3.*;
public class Main {
static int getShapeType() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in
));
System.out.println("==============\n SHAPE TYPE \n==============");
System.out.println(" (1) Rectangle\n (2) Circle");
System.out.print("Enter Choice: ");
return Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws IOException {
Shape ref;
boolean flag = false;
while (!flag) {
switch (getShapeType()) {
case 1:
flag = true;
ref = new Rectangle();
System.out.println("Area: " + ref.area() + " sq units");
break;
case 2:
flag = true;
ref = new Circle();
System.out.println("Area: " + ref.area() + " sq units");
break;
default:
System.err.println("Invalid Option");
break;
}
}
}
}
Output -
Enter Lenght of Rectangle: 2.5
Enter Breadth of Rectangle: 3.5
Area: 8.75 sq units
Enter Radius of Circle: 2.2
Area: 15.20531 sq units