-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
75 lines (59 loc) · 1.77 KB
/
Main.java
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
import java.util.Scanner;
abstract class Shape {
double dim1;
double dim2;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first dimension ");
dim1 = sc.nextDouble();
System.out.print("Enter second dimension ");
dim2 = sc.nextDouble();
}
abstract double compute_area(double one, double two); //abstract method
}
class Triangle extends Shape {
//implementing abstract method in child class
double compute_area(double one, double two) {
return 0.5 * one * two;
}
}
class Rectangle extends Shape {
//implementing abstract method in child class
double compute_area(double one, double two) {
return one * two;
}
}
public abstract class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
while (true) {
System.out.print("\n\t\tMenu\n\t1. AREA OF TRIANGLE\n\t2. AREA OF RECTANGLE\n\t3. EXIT");
Scanner s = new Scanner(System.in);
int ch = s.nextInt();
switch (ch) {
case 1:
/* Reference is of Shape type and object is Triangle type*/
Shape obj1 = new Triangle(); //dynamic binding
System.out.println("Enter dimensions of triangle ");
obj1.input();
System.out.print("\nThe area of triangle with given dimension is ");
System.out.println(obj1.compute_area(obj1.dim1, obj1.dim2));
break;
case 2:
/* Reference is of Shape type and object is Rectangle type*/
Shape obj2 = new Rectangle(); //dynamic binding
System.out.println("\n\nEnter dimensions of rectangle ");
obj2.input();
System.out.print("\nThe area of rectangle with given dimension is ");
System.out.println(obj2.compute_area(obj2.dim1, obj2.dim2));
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid Choice");
break;
}
}
}
}