-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign1.java
More file actions
154 lines (138 loc) · 3.12 KB
/
Assign1.java
File metadata and controls
154 lines (138 loc) · 3.12 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.google;
/*
Author: Vaibhavi Dixit
code for calculate area of shapes
* */
//----------Abstraction------
abstract class Area
{
final float PI=3.14f; //final variable
abstract public void area();
}
//-------Inheritance--------
//Inheritance Oval->Area
//-----Class----
class Oval extends Area{
int x,y;
float area;
Oval(int x,int y){
this.x=x;
this.y=y;
}
//-------Polymorphism--------
//override abstract method
@Override
public void area() {
area=x*y*PI;
System.out.println("Area of Oval is : "+area);
}
}
//inheritance Triangle->Area
class Triangle extends Area{
int base,height;
float area;
Triangle(int base,int height){
this.base=base;
this.height=height;
}
//override abstract method
@Override
public void area() {
area= (float) (0.5*base*height);
System.out.println("Area of Triangle is : "+area);
}
}
//-------------Interface-----------
interface shape{
float PI=3.14f; //final variable by default
void area();
}
//Inheritance Rectangle->shape
class Rectangle implements shape{
int x,y;
float area;
Rectangle(int x,int y){
this.x=x;
this.y=y;
}
//override area method of interface shape
@Override
public void area() {
area=x*y;
System.out.println("Area of rectangle is: "+area);
}
}
//Inheritance Pentagon->shape
class Pentagon implements shape{
int side,apothem;
float area;
Pentagon(int side,int apothem){
this.apothem=apothem;
this.side=side;
}
@Override
public void area() {
area=(float)(5/2)*side*apothem;
System.out.println("Area of pentagon is: "+area);
}
}
//Inheritance Hexagon->shape
class Hexagon implements shape{
int side;
final double rt=(3*Math.sqrt(3))/2;
double area;
Hexagon(int side){
this.side=side;
}
@Override
public void area() {
area=(double)rt*side*side;
System.out.println("Area of Hexagon is: "+area);
}
}
//Inheritance Trapezium->shape
class Trapezium implements shape{
int a,b,h;
float area;
Trapezium(int a,int b,int h){
this.a=a;
this.b=b;
this.h=h;
}
@Override
public void area() {
area=(float)(0.5)*a*b*h;
System.out.println("Area of Trapezium is: "+area);
}
}
//Inheritance Circle->shape
class Circle implements shape{
double radius;
double area;
Circle(double radius){
this.radius=radius;
}
@Override
public void area() {
area=PI*radius*radius;
System.out.println("Area of circle is : "+area);
}
}
public class Assign1 {
public static void main(String[] args) {
Area a=new Oval(17,7); //------Objects-----
a.area();
a=new Triangle(10,12);
a.area();
shape sp=new Rectangle(30,18);
sp.area();
sp=new Pentagon(5,3);
sp.area();
sp=new Hexagon(8);
sp.area();
sp=new Trapezium(2,7,4);
sp.area();
sp=new Circle(5.8);
sp.area();
}//main method ends
}//Assign1 ends