-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsgn04.cs
222 lines (187 loc) · 5.67 KB
/
Asgn04.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Asgn04_Shapes
{
public class Program
{
static void Main(String[] args)
{
AbstractShape[] myShapes = new AbstractShape[16];
myShapes[0] = new Rectangle(10, 5);
myShapes[1] = new Rectangle(20, 30);
myShapes[2] = new Rectangle(50, 10);
myShapes[3] = new Rectangle(-10, -20);
myShapes[4] = new Circle(20);
myShapes[5] = new Circle(10);
myShapes[6] = new Circle(30);
myShapes[7] = new Circle(-20);
myShapes[8] = new Cuboid(20, 10, 6);
myShapes[9] = new Cuboid(40, 5, 50);
myShapes[10] = new Cuboid(30, 20, 20);
myShapes[11] = new Cuboid(-20, 30, -10);
myShapes[12] = new Sphere(15);
myShapes[13] = new Sphere(25);
myShapes[14] = new Sphere(12);
myShapes[15] = new Sphere(-10);
for (int i = 0; i < myShapes.Length; i++)
{
Console.WriteLine("***********************\n");
Console.WriteLine("Shape #" + (i + 1));
myShapes[i].printShapeDimensions();
Console.WriteLine("Area = " + myShapes[i].Area());
Console.WriteLine("Perimeter = " + myShapes[i].Perimeter());
Console.WriteLine("Volume = " + +myShapes[i].Volume());
Console.WriteLine();
}
Console.WriteLine("***********************");
Console.ReadLine();
}
}
public interface Shape
{
double Area();
}
public interface TwoDShape : Shape
{
double Perimeter();
}
public interface ThreeDShape : Shape
{
double Volume();
}
public abstract class AbstractShape : TwoDShape, ThreeDShape
{
private static int numShapesCreated;
public void IncrementNumShapesCreated()
{
numShapesCreated++;
}
public int GetNumShapesCreated()
{
return numShapesCreated;
}
public abstract double Area();
public abstract double Perimeter();
public abstract double Volume();
public abstract void printShapeDimensions();
}
public class Rectangle : AbstractShape
{
private static int NumRectanglesCreated;
private double length;
private double width;
public Rectangle(double len, double wid)
: base()
{
length = len;
width = wid;
NumRectanglesCreated++;
}
public override double Area()
{
return length * width;
}
public override double Perimeter()
{
return 2 * (length + width);
}
public override double Volume()
{
return 0.0;
}
public override void printShapeDimensions()
{
Console.WriteLine("Dimensions: " + this.length + " X " + this.width);
}
}
public class Cuboid : AbstractShape
{
private static int NumCuboidsCreated;
private double length;
private double width;
private double height;
public Cuboid(double len, double wid, double heigh)
: base()
{
length = len;
width = wid;
height = heigh;
NumCuboidsCreated++;
}
public override double Area()
{ //Surface area
return 2 * (length * width + width * height + length * height);
}
public override double Perimeter()
{
return 0.0;
}
public override double Volume()
{
return length * width * height;
}
public override void printShapeDimensions()
{
Console.WriteLine("Dimensions: " + this.length + " X " + this.width + " X " + this.height);
}
}
public class Circle : AbstractShape
{
private static int NumSpheresCreated;
private double radius;
private readonly double pi = Math.PI;
public Circle(double rad)
: base()
{
radius = rad;
NumSpheresCreated++;
}
public override double Area()
{
return radius * radius * pi;
}
public override double Perimeter()
{
return 2 * radius * pi;
}
public override double Volume()
{
return 0.0;
}
public override void printShapeDimensions()
{
Console.WriteLine("Dimensions: r = " + this.radius);
}
}
public class Sphere : AbstractShape
{
private static int NumSpheresCreated;
private double radius;
private readonly double pi = Math.PI;
public Sphere(double rad)
: base()
{
radius = rad;
NumSpheresCreated++;
}
public override double Area()
{
return 4 * radius * radius * pi;
}
public override double Perimeter()
{
return 0.0;
}
public override double Volume()
{
return (4 * pi * radius * radius * radius) / 3;
}
public override void printShapeDimensions()
{
Console.WriteLine("Dimensions: r =" + this.radius);
}
}
}