-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram2.cs
More file actions
302 lines (219 loc) · 6.52 KB
/
Program2.cs
File metadata and controls
302 lines (219 loc) · 6.52 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
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
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
delegate void Mydelegate(String Name);
delegate void Heatwave(String city, int temp);
delegate void lowbal(int acno, int bal);
class Program
{
static void Main()
{
//Dictionary<int,string> students = new Dictionary<int,string>();
//students.Add(1, "xyz");
//students.Add(2, "abc");
//foreach (var student in students)
//{
// Console.WriteLine($"{student.Key},{student.Value}");
//}
////generate a dictionary for 3 students having rolno and name, ask the user to enter name of n student and your
////program should show the details if the student exists
//Dictionary<int, string> students = new Dictionary<int, string>();
//students.Add(1, "xyz");
//students.Add(2, "abc");
//students.Add(3, "pqr");
//String n=Console.ReadLine();
//int f = 0;
//foreach (var student in students)
//{
// if (n == student.Value)
// {
// Console.WriteLine($"{student.Key},{student.Value}");
// f = 1;
// }
//}
//if (f == 0) Console.WriteLine("not found");
//Car c1 = new Car(1111,"BMW","Jetshree");
//c1.display();
//Bank b = new Bank("abc", 1, 10000);
//Console.WriteLine(b.add(10000));
//Console.WriteLine(b.withdraw(200));
//Overloading o1=new Overloading();
//o1.add(1, 2);
//o1.add(3, 4,3);
//Animal a=new Animal();
//a.speak();
//Animal b = new Dog();
//b.speak();
//Animal c = new Bulldog();
//c.speak();
//Animals a = new Dogs();
//a.speak();
//a.eat();
//Mydelegate del = GreetEnglish;
//del += GreetHindi;
//del += GreetGuj;
//del("abc");
//WeatherStorm w = new WeatherStorm("Ahmedabad", 45);
//w.handler += OnAlert;
//w.updatetemp(48);
BankAccount b = new BankAccount(1, 5000);
b.lh += Alert;
b.withdraw(4500);
}
public static void OnAlert(String city, int temp)
{
Console.WriteLine("alert");
}
public static void Alert(int acno, int temp)
{
Console.WriteLine("low balance");
}
// delegate and events
// develop an approach delegate to greet user (in 3 different languages)
static void GreetEnglish(String name)
{
Console.WriteLine("Hello " + name);
}
static void GreetHindi(String name)
{
Console.WriteLine("Hello (hindi)" + name);
}
static void GreetGuj(String name)
{
Console.WriteLine("Hello (Guj)" + name);
}
}
//create a class for car, assign 2 objects to it. Attributes : car no., car brand and customer name. consider action as
//all the details to be printed
class Car
{
public int carno;
public String brand;
public String customer;
public Car(int no, String br, String cus)
{
carno = no;
brand = br;
customer = cus;
}
public void display() {
Console.WriteLine(carno);
Console.WriteLine(brand);
Console.WriteLine(customer);
}
}
// consider class of bank account in which you have details like account holder name, acc no and balance
// you want to provide 2 options: add money or withdraw
// after any action is done show acc no, balance and customer name
class Bank {
public String cust;
public int accno;
public int balance;
public Bank(String c, int a, int b)
{
cust= c;
accno= a;
balance= b;
}
public int add(int i) {
balance += i;
return balance;
}
public int withdraw(int w) {
if (w < balance) balance = balance - w;
else Console.WriteLine("not enough balance");
return balance;
}
}
// overloading
class Overloading {
public void add(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
public void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
// overriding => virtual, override, sealed override
class Animal
{
public virtual void speak() {
Console.WriteLine("Animal makes sound");
}
}
class Dog : Animal
{
public override void speak() // writing sealed gives error when dog is parent
{
Console.WriteLine("Dog barks");
}
}
class Bulldog : Dog
{
public override void speak()
{
Console.WriteLine("Bulldog barks");
}
}
// abstract => must be in child class also
abstract class Animals
{
public abstract void speak();
public void eat() => Console.WriteLine("Eating");
}
class Dogs : Animals
{
public override void speak() // writing sealed gives error when dog is parent
{
Console.WriteLine("Dog barks");
}
}
//create delegate and events , class is weather storm with field city and temp
//methods : update temp, define del and ev for heatwave method, raise the event if temp>40
// in main create weather storm object, subscribe to the event an ddisplay a warning msg whne triggered
class WeatherStorm
{
public String city;
public int temp;
public event Heatwave handler;
public WeatherStorm(String City, int Temp)
{
city = City;
temp = Temp;
}
public void updatetemp(int temperature)
{
temp = temperature;
if (temp > 43)
{
handler ?. Invoke(city, temperature);
}
}
}
/*
create a class bankaccount. attr: acno and balance, there is a method withdraw that decreases the balance
define del and event lowbalance alert
the event should be raised when the balance goes below 1000 event trigger
in the withdraw method after the deduction check if the balance is < 1000 if true trigger the low balance
alert and pass the current balance in main part create bankaccount obj with initial bal 5000.
perform certain withdrwals and raise bal trigger.
*/
class BankAccount
{
public int acno;
public int bal;
public event lowbal lh;
public BankAccount(int a, int b)
{
acno = a;
bal = b;
}
public void withdraw(int b)
{
bal -= b;
if (bal < 1000) {
lh?.Invoke(acno, bal);
}
}
}