-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
291 lines (223 loc) · 7.46 KB
/
Program.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// MakeShapes();
// MakeStacks();
// MakeUsers(); // demos overide methods
// UseRef();
// DemoShipping(); // demos inheritance, constructors calling their base classes
// MakeMoreShapes();
// TestInterface();
// FixedStack(); // with interface
// DynamicStack(); // with interface
// StackWithInterfaceReference();
EnumDemo();
void EnumDemo()
{
Apple ap;
ap = Apple.GoldenDel;
Console.WriteLine("Value of apple: " + ap);
switch (ap)
{
case Apple.Jonathan:
Console.WriteLine("Jonathan is red");
break;
case Apple.GoldenDel:
Console.WriteLine("Golden delicious is yellow");
break;
case Apple.RedDel:
Console.WriteLine("Red delicious is red");
break;
case Apple.Winesap:
Console.WriteLine("Wine apple is red");
break;
}
}
public enum Apple
{
Jonathan, GoldenDel, RedDel, Winesap
}
// void StackWithInterfaceReference()
// {
// IIntStack myStack; // creates an interface reference variable
// DynamicStack ds = new DynamicStack(5);
// FixedStack fs = new FixedStack(8);
// myStack = ds; // load dynamic stack referring it to the interface
// for (int i = 0; i < 12; i++)
// {
// myStack.Push(i);
// }
// myStack = fs;
// for (int i = 0; i < 8; i++)
// {
// myStack.Push(i);
// }
// myStack = ds;
// for (int i = 0; i < 12; i++)
// {
// Console.WriteLine(myStack.Pop());
// }
// myStack = fs;
// for (int i = 0; i < 8; i++)
// {
// Console.WriteLine(myStack.Pop());
// }
// }
// void DynamicStack()
// {
// DynamicStack myStack1 = new DynamicStack(5);
// DynamicStack myStack2 = new DynamicStack(8);
// // in a fixed stack, these would be over the allocated size
// for (int i = 0; i < 12; i++) myStack1.Push(i);
// for (int i = 0; i < 20; i++) myStack2.Push(i);
// Console.WriteLine("Each item popped off myStack1: ");
// for (int i = 0; i < 12; i++)
// {
// Console.WriteLine(myStack1.Pop());
// }
// Console.WriteLine("Each item popped off myStack2: ");
// for (int i = 0; i < 20; i++)
// {
// Console.WriteLine(myStack2.Pop());
// }
// }
// void FixedStack()
// {
// FixedStack myStack1 = new FixedStack(5);
// FixedStack myStack2 = new FixedStack(8);
// for (int i = 0; i < 5; i++) myStack1.Push(i);
// for (int i = 0; i < 8; i++) myStack2.Push(i);
// Console.WriteLine("Each item popped off myStack1: ");
// for (int i = 0; i < 5; i++)
// {
// Console.WriteLine(myStack1.Pop());
// }
// Console.WriteLine("Each item popped off myStack2: ");
// for (int i = 0; i < 8; i++)
// {
// Console.WriteLine(myStack2.Pop());
// }
// }
// void TestInterface()
// {
// ICallback c = new Client();
// AnotherClient ac = new AnotherClient();
// c.Callback(42);
// c = ac; // c now refers to AnotherClient
// c.Callback(42); // Because only the method signature is defined, the implementation
// // can be different.
// // use a nested interface reference
// ClientA.NestedIF nesty = new ClientB();
// if (nesty.isNotNegative(10))
// {
// Console.WriteLine("10 is not negative");
// }
// else if (nesty.isNotNegative(-10))
// {
// Console.WriteLine("this won't be displayed");
// }
// }
// void MakeMoreShapes()
// {
// Figure r = new Rectangle(9, 5); // type inferred as Figure
// Figure t = new Triangle(10, 8); // type inferred as Figure
// RectangleOnWheels rw = new RectangleOnWheels(9, 5);
// // This is not allowed:
// /*
// r.x = 5;
// t.y = 5;
// */
// // Becuase types are inferred as the BASE class (i.e. Figure)
// // the variables are not accessible becuase they exist only
// // on the derived classes.
// Figure figref;
// /*
// figref = f;
// Console.WriteLine($"Area is {figref.Area()}"); // will cause runtime error
// */
// figref = r;
// Console.WriteLine($"Area is {figref.Area()}");
// figref = t;
// Console.WriteLine($"Area is {figref.Area()}");
// figref = rw;
// Console.WriteLine($"Area is {figref.Area()}");
// }
// void DemoShipping()
// {
// Shipment shipment1 = new Shipment(2, 2, 2, 1.9, 1.30);
// Shipment shipment2 = new Shipment(4, 4, 4, 2.5, 3.48);
// Console.WriteLine($"Volume of myShipment1 is {shipment1.Volume()}");
// Console.WriteLine($"Weight of shipment1 is {shipment1.Weight}kg");
// Console.WriteLine($"Shipping cost is ${shipment1.Cost}");
// Box box = new Box();
// BoxWeight boxWeight = new BoxWeight();
// Shipment shipment = new Shipment();
// List<Box> boxes = new List<Box>() { box, boxWeight, shipment };
// foreach (Box b in boxes)
// {
// Console.WriteLine(b.tellMeWhoYouAre());
// }
// }
// void MakeShapes()
// {
// Ball myBall = new Ball(2);
// Console.WriteLine($"The volume of myBall is {myBall.Volume()}");
// List<Box> boxes = new List<Box>();
// boxes.Add(new Box(15, 10, 10));
// boxes.Add(new Box());
// boxes.Add(new Box(length: 10));
// foreach (Box b in boxes)
// {
// Console.WriteLine(b.Volume());
// }
// Box myClone = new Box(boxes[0]);
// Console.WriteLine($"myClone vol: {myClone.Volume()} should be {boxes[0].Volume()}");
// BoxWeight myWeightedBox = new BoxWeight(15, 15, 15, 450);
// Box box = new Box();
// double vol = myWeightedBox.Volume();
// Console.WriteLine($"Volume of box is {box.Volume()}");
// Console.WriteLine($"Volume of weighted box is {vol}");
// Console.WriteLine($"Weight of weighted box is {myWeightedBox.Weight}");
// box = myWeightedBox;
// vol = box.Volume();
// Console.WriteLine($"Volume of box is {box.Volume()}"); // this proves that value has passed by reference
// }
// void MakeStacks()
// {
// Stack myStack1 = new Stack(10);
// Stack myStack2 = new Stack(10);
// for (int i = 0; i < 5; i++) myStack1.Push(i);
// for (int i = 5; i < 10; i++) myStack2.Push(i);
// Console.WriteLine("Each item popped off myStack1: ");
// for (int i = 0; i < 5; i++)
// {
// Console.WriteLine(myStack1.Pop());
// }
// Console.WriteLine("Each item popped off myStack2: ");
// for (int i = 5; i < 10; i++)
// {
// Console.WriteLine(myStack2.Pop());
// }
// }
// void MakeUsers()
// {
// Student me = new Student("Tommy", "Edward");
// Teacher you = new Teacher("Sophia", "Diggy");
// User other = new User("John", "Smith");
// ITalk them = new Student("Ali", "Fab"); // implements interface
// List<User> users = new List<User>() { me, you, other };
// foreach (User u in users)
// {
// u.sayHello(); // implements derived versions of SayHello()
// }
// }
// void UseRef()
// {
// // with ref, the value of x will increase
// // without ref, the i is only know within the scope of Test
// // and therefore x remains untouched
// int x = 50;
// Test(ref x);
// Console.WriteLine(x);
// void Test(ref int i)
// {
// i++;
// }
// }