-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_5.Abstraction_(sides+areas_constructor method).java
More file actions
110 lines (107 loc) · 2.98 KB
/
_5.Abstraction_(sides+areas_constructor method).java
File metadata and controls
110 lines (107 loc) · 2.98 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
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Scanner;
import java.lang.Math;
abstract class Shape
{
abstract void numberOfSides();
abstract void area();
}
class Rectangle extends Shape
{
void numberOfSides()
{
System.out.println("\nThe number of sides of the Rectangle is 4.");
}
void area()
{
double a,b;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length : ");
a = scan.nextDouble();
System.out.println("Enter the breadth : ");
b = scan.nextDouble();
System.out.println("The area of the Rectangle is "+a*b+" square units.\n");
}
}
class Triangle extends Shape
{
void numberOfSides()
{
System.out.println("\nThe number of sides of the Triangle is 3.");
}
void area()
{
double a,b,c,s;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length of side 1 : ");
a = scan.nextDouble();
System.out.println("Enter the length of side 2 : ");
b = scan.nextDouble();
System.out.println("Enter the length of side 3 : ");
c = scan.nextDouble();
s = (a+b+c)/2;
System.out.println("S : "+s);
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("The area of the Triangle is "+area+" square units.\n");
}
}
class Hexagon extends Shape
{
void numberOfSides()
{
System.out.println("\nThe number of sides of the Hexagon is 6.");
}
void area()
{
double a;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length of side of Hexagon : ");
a = scan.nextDouble();
double area = (3*Math.sqrt(3)*a*a)/2;
System.out.println("The area of the Hexagon is "+area+" square units.\n");
}
}
public class Abstraction
{
public static void main(String[] args)
{
Rectangle rec = new Rectangle();
Triangle tri = new Triangle();
Hexagon hex = new Hexagon();
Scanner scan = new Scanner(System.in);
char ch;
do
{
System.out.println("\nshapes...\n1.Rectangle\n2.Triangle\n3.Hexagon\nchoose shape : ");
int choice = scan.nextInt();
switch(choice)
{
case 1:
{
rec.numberOfSides();
rec.area();
break;
}
case 2:
{
tri.numberOfSides();
tri.area();
break;
}
case 3:
{
hex.numberOfSides();
hex.area();
break;
}
default:
{
System.out.println("choose correct shape.");
break;
}
}
System.out.println("Do you want to continue(Type y for Yes OR n for No) : ");
ch = scan.next().charAt(0);
}while(ch=='y' || ch=='Y');
System.gc();
}
}