-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeProgram.java
More file actions
194 lines (158 loc) · 3.57 KB
/
ShapeProgram.java
File metadata and controls
194 lines (158 loc) · 3.57 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Write a program to provide options of creating objects for different
shapes like, Circle, Cylinder, Rectangle, Square, etc. User should be
able to draw the shape and calculate its area & perimeter. */
import java.util.Scanner;
abstract class Shape
{
public abstract void draw();
public abstract double calculteArea();
public abstract double calculatePerimeter();
}
class Circle extends Shape
{
public double radius;
public Circle(double radius)
{
this.radius = radius;
}
@Override
public void draw()
{
System.out.println("Drawing Circle");
}
@Override
public double calculteArea()
{
return Math.PI*radius*radius;
}
@Override
public double calculatePerimeter()
{
return 2*Math.PI*radius;
}
}
class Cylinder extends Circle
{
private double height;
public Cylinder(double radius,double height)
{
super(radius);
this.height = height;
}
@Override
public void draw()
{
System.out.println("Drawing Cylinder");
}
@Override
public double calculteArea()
{
double baseArea = super.calculteArea();
double laterArea = 2*Math.PI*radius*height;
return 2*baseArea+laterArea;
}
@Override
public double calculatePerimeter()
{
return super.calculatePerimeter();
}
public double getHeight()
{
return height;
}
}
class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle(double length,double width)
{
this.length = length;
this.width = width;
}
@Override
public void draw()
{
System.out.println("Drawing Rectangle");
}
@Override
public double calculteArea()
{
return length*width;
}
@Override
public double calculatePerimeter()
{
return 2*(length+width);
}
}
class Square extends Rectangle
{
public Square(double side)
{
super(side,side);
}
@Override
public void draw()
{
System.out.println("Drawing Square");
}
@Override
public double calculteArea()
{
return super.calculteArea();
}
@Override
public double calculatePerimeter()
{
return super.calculatePerimeter();
}
}
public class ShapeProgram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Select a shape to create : ");
System.out.println("1. Circle");
System.out.println("2.Cylinder");
System.out.println("3. Rectangle");
System.out.println("4. Square");
int choice = sc.nextInt();
Shape shape;
switch(choice)
{
case 1:
System.out.print("Enter the radius of circle : ");
double radius = sc.nextDouble();
shape = new Circle(radius);
break;
case 2:
System.out.print("Enter the radius of the cylinder : ");
radius = sc.nextDouble();
System.out.print("Enter the height of cylinder : ");
double height = sc.nextDouble();
shape = new Cylinder(radius,height);
break;
case 3:
System.out.print("Enter the length of the rectangle : ");
double length = sc.nextDouble();
System.out.print("Enter the width of the rectangle : ");
double width = sc.nextDouble();
shape = new Rectangle(length,width);
break;
case 4 :
System.out.print("Enter the side of the square : ");
double side = sc.nextDouble();
shape = new Square(side);
break;
default:
System.out.print("Invalid choice... Exiting the prograam.");
return;
}
shape.draw();
System.out.println("Area : "+shape.calculteArea());
System.out.println("Perimeter : "+shape.calculatePerimeter());
sc.close();
}
}