-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 10.4 (Completed)
584 lines (495 loc) · 13.7 KB
/
Exercise 10.4 (Completed)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
1. (Practice) Enter and run Program 10.5 on your computer.
Done!!
2. (Modify) Modify the main() function in Program 10.5 to create a second room with a length
of 9 and a width of 12. Have the program calculate this new room’s area.
#include <iostream>
using namespace std;
// class declaration section
class RoomType
{
private:
double length; // declare length as a double variable
double width; // declare width as a double variable
public:
RoomType(double = 0.0, double = 0.0); // the constructor's declaration
void showRoomValues();
void setNewRoomValues(double, double);
double calculateRoomArea();
};
// class implementation section
RoomType::RoomType(double l, double w) // this is a constructor
{
length = l;
width = w;
cout << "Created a new room object using the default constructor.\n\n";
}
void RoomType::showRoomValues() // this is an accessor
{
cout << " length = " << length
<< "\n width = " << width << endl;
}
void RoomType::setNewRoomValues(double l, double w) // this is a mutator
{
length = l;
width = w;
}
double RoomType::calculateRoomArea() // this performs a calculation
{
double result = length * width;
cout << result << endl;
return result;
}
int main()
{
RoomType roomOne(12.5, 18.2); // declare a variable of type RoomType
cout << "The values for this room are : \n";
roomOne.showRoomValues(); // use a class method on this object
cout << "\nThe floor area of this room is : ";
roomOne.calculateRoomArea(); // use another class method on this object
roomOne.setNewRoomValues(9, 12); // call the mutator
cout << "\n\nThe values for this room have been changed to : \n";
roomOne.showRoomValues();
cout << "\nThe floor area of this room is : ";
roomOne.calculateRoomArea();
cout << endl;
system("PAUSE");
return 0;
}
3. (Modify) a. Modify the main() function in Program 10.5 to create four rooms: hall, kitchen,
dining room, and living room. The dimensions for these rooms are as follows:
Hall: length = 12.40, width = 3.5
Kitchen: length = 14, width = 14
Living room: length = 12.4, width = 20
Dining room: length = 14, width = 10.5.
Your program should display the area of each room and the total area of all four rooms combined.
int main()
{
RoomType roomOne(12.5, 18.2), hall(12.40,20),kitchen(14,14),livingRoom(12.4,20),diningRoom(14,10.5); // declare a variable of type RoomType
cout << "The values for this room are : \n";
roomOne.showRoomValues(); // use a class method on this object
cout << "\nThe floor area of this room is : ";
roomOne.calculateRoomArea(); // use another class method on this object
roomOne.setNewRoomValues(9, 12); // call the mutator
cout << "\n\nThe values for this room have been changed to : \n";
roomOne.showRoomValues();
cout << "\nThe floor area of this room is : ";
roomOne.calculateRoomArea();
hall.showRoomValues();
kitchen.showRoomValues();
livingRoom.showRoomValues();
diningRoom.showRoomValues();
cout << endl;
system("PAUSE");
return 0;
}
4. (Practice) Enter and run Program 10.6 on your computer.
Done!!
5. (Modify) a. Modify the main() function in Program 10.6 to put a second elevator in service
starting at the 5th floor and have a maximum floor of 20. Have this second elevator move to
the 1st floor and then move to the 12th floor.
int main()
{
Elevator a,b(2,5,20); // declare 1 object of type Elevator
a.request(6);
a.request(3);
b.request(1);
b.request(12);
system("PAUSE");
return 0;
}
b. Verify that the constructor is called by adding a message in it that’s displayed each time a
new object is created. Run your program to verify its operation.
Elevator::Elevator(int idnum, int cfloor, int maxfloor)
{
elNum = idnum;
currentFloor = cfloor;
highestFloor = maxfloor;
cout << "Called Verified" << endl;
}
6. (Modify) Modify the main() function in Program 10.6 to use a while loop that calls the
Elevator’s request() method with a random number between 1 and 15. If the random
number is the same as the elevator’s current floor, generate another request. The while loop
should terminate after five valid requests have been made and be satisfied by movement of
the elevator. (Hint: Review Section 6.8 about the use of random numbers.)
int main()
{
Elevator a, b(2, 5, 20); // declare 1 object of type Elevator
int total = 0;
while (total < 5)
{
double var = rand() % 15 + 1;
b.request(var);
if (var == var){
b.request(var);
}
total++;
}
system("PAUSE");
return 0;
}
7. (Program) Construct a class named Light that simulates a traffic light. The class’s color
attribute should change from Green to Yellow to Red and then back to Green by using the
class’s change() method. When a new Light object is created, its initial color should be Red.
#include <iostream>
using namespace std;
// class declaration section
class Light
{
private:
int lightNum;
int currentLight;
public:
Light(int = 1, int = 1); // constructor
void change(int);
};
// class implementation section
Light::Light(int lnum, int clight)
{
lightNum = lnum;
currentLight = clight;
}
void Light::change(int light)
{
if (light < 1 || light > 3 || light == currentLight)
; // do nothing
else if (light > currentLight) // move Light up
{
std::cout << "\nLight " << lightNum
<< " starting at light "<< currentLight << endl;
while (light > currentLight)
{
currentLight++; // add one to current floor
cout << " Going Up - now at light " << currentLight << endl;
}
cout << "Light " << lightNum
<< " stopping at light " << currentLight << endl;
}
else // move Light down
{
cout << "\nLight " << lightNum
<< " starting at light " << currentLight << endl;
while (light < currentLight)
{
currentLight--; // subtract one from current floor
cout << " Going Down - now at light " << currentLight << endl;
}
cout << "Light " << lightNum
<< " Stopping at light "<< currentLight << endl;
}
if (currentLight == 1){
cout << "Red" << endl;
}
else if (currentLight == 2){
cout << "Yellow" << endl;
}
else{ cout << "Green" << endl; }
return;
}
int main()
{
Light a, b(2, 1); // declare 1 object of type Light
a.change(2);
a.change(1);
a.change(3);
system("PAUSE");
return 0;
}
8. (Program) a. Construct a class definition to represent an employee of a company. Each
employee is defined by an integer ID number, a double-precision pay rate, and the maximum
number of hours the employee should work each week. The class should provide these services:
the capability to enter data for a new employee, the capability to change data for a new
employee, and the capability to display existing data for a new employee.
Done!! :)
b. Include the class definition created for Exercise 8a in a working C++ program that asks the
user to enter data for three employees and then displays the entered data.
#include "stdafx.h"
#include <iostream>
using namespace std;
// class declaration section
class Employee
{
private:
int EmployeeIDNum;
double payRate;
double hoursWorked;
public:
Employee(int = 1, double = 35, double = 40); // constructor
void changeEmployee();
void displayEmployee();
};
// class implementation section
Employee::Employee(int eid, double pr, double hw)
{
EmployeeIDNum = eid;
payRate = pr;
hoursWorked = hw;
}
void Employee::changeEmployee()
{
for (int i = 1; i <= 3; i++){
cout << "Enter " << i << "employees info" << endl;
cin >> EmployeeIDNum;
cin >> payRate;
cin >> hoursWorked;
displayEmployee();
}
return;
}
void Employee::displayEmployee(){
cout << "The ID number is " << EmployeeIDNum << endl;
cout << "The payrate for this employee is " << payRate << endl;
cout << "The hours worked by this employee is " << hoursWorked << endl;
}
int main()
{
Employee a, b(2001, 50, 60); // declare 1 object of type Employee
a.displayEmployee();
a.changeEmployee();
system("PAUSE");
return 0;
}
c. Modify the program written for Exercise 8b to include a menu that offers the user the following
choices:
1. Add an employee
2. Modify employee data
3. Delete an employee
4. Exit this menu
In response to the user’s choice, the program should initiate an action to implement the
choice.
#include <iostream>
#include <cmath>
using namespace std;
// class declaration section
class Employee
{
private:
int EmployeeIDNum;
double payRate;
double hoursWorked;
public:
Employee(int = 1, double = 35, double = 40); // constructor
void addEmployee();
void modifyEmployee(int, double, double);
void displayEmployee();
};
// class implementation section
Employee::Employee(int eid, double pr, double hw)
{
EmployeeIDNum = eid;
payRate = pr;
hoursWorked = hw;
}
void Employee::modifyEmployee(int eid, double pr, double hw){
EmployeeIDNum = eid;
payRate = pr;
hoursWorked = hw;
return;
}
void Employee::addEmployee()
{
cout << "Enter a new employees info" << endl;
cin >> EmployeeIDNum;
cin >> payRate;
cin >> hoursWorked;
displayEmployee();
return;
}
void Employee::displayEmployee(){
cout << "The ID number is " << EmployeeIDNum << endl;
cout << "The payrate for this employee is " << payRate << endl;
cout << "The hours worked by this employee is " << hoursWorked << endl;
}
int main()
{
Employee menu[200]; // declare 1 object of type Employee
int choose = 0, newNum, newNum2, newNum3,i=0;
while (choose != 4){
cout << "Menu" << endl;
cout << "1. Add an employee" << endl;
cout << "2. Modify employee data" << endl;
cout << "3. Delete an employee" << endl;
cout << "4. exit this menu" << endl;
cin >> choose;
if (choose == 1){
cout << "Enter item's location to be added" << endl;
cin >> i;
menu[i].addEmployee();
}
if (choose == 2){
cout << "Enter new Values" << endl;
cin >> newNum;
cin >> newNum2;
cin >> newNum3;
cout << "Enter item's location to be modified" << endl;
cin >> i;
menu[i].modifyEmployee(newNum, newNum2, newNum3);
menu[i].displayEmployee();
}
if (choose == 3){
cout << "Enter item's location to be deleted" << endl;
cin >> i;
menu[i].modifyEmployee(NULL,NULL,NULL);
menu[i].displayEmployee();
}
}
system("PAUSE");
return 0;
}
9. (Program) a. Construct a class definition to represent types of food. A type of food is classified
as basic or prepared. Basic foods are further classified as Dairy, Meat, Fruit, Vegetable,
or Grain. The class should provide these services: the capability to enter data for a new food,
the capability to change data for a new food, and the capability to display existing data for a
new food.
Done!! :)
b. Include the class definition created for Exercise 9a in a working C++ program that asks the
user to enter data for four food items and then displays the entered data.
#include <iostream>
#include <string>
using namespace std;
// class declaration section
class Food
{
private:
string type;
string details;
int weight ;
int cost;
public:
Food(string = "basic", int = 10, int = 20); // constructor
void changeFood();
void displayFood();
};
// class implementation section
Food::Food(string ty, int we, int co)
{
type = ty;
weight = we;
cost = co;
}
void Food::changeFood()
{
for (int i = 1; i <= 4; i++){
cout << "Enter " << i << " Foods info" << endl;
std::getline(std::cin, type);
if (type == "basic"){
cout << "Enter further classifications" << endl;
getline(cin, details);
}
cin >> weight;
cin >> cost;
displayFood();
}
return;
}
void Food::displayFood(){
cout << "The Food Type is " << type << endl;
if (type == "basic"){
cout << details << endl;
}
cout << "The Food weight is " << weight << endl;
cout << "The Food costs " << cost << endl;
}
int main()
{
Food a, b; // declare 2 object of type Food
a.displayFood();
a.changeFood();
system("PAUSE");
return 0;
}
c. Modify the program written for Exercise 9b to include a menu that offers the user the following
choices:
1. Add a food item
2. Modify a food item
3. Delete a food item
4. Exit this menu
In response to the user’s choice, the program should initiate an action to implement the
choice.
#include <iostream>
#include <string>
using namespace std;
// class declaration section
class Food
{
private:
string type;
string details;
int weight ;
int cost;
public:
Food(string = "basic", int = 10, int = 20); // constructor
void addFood();
void modifyFood(string,int,int);
void displayFood();
};
// class implementation section
Food::Food(string ty, int we, int co)
{
type = ty;
weight = we;
cost = co;
}
void Food::addFood()
{
cout << "Enter Foods info" << endl;
std::getline(std::cin, type);
if (type == "basic"){
cout << "Enter further classifications" << endl;
getline(cin, details);
}
cin >> weight;
cin >> cost;
displayFood();
return;
}
void Food::modifyFood(string ty, int we, int co)
{
type = ty;
weight = we;
cost = co;
}
void Food::displayFood(){
cout << "The Food Type is " << type << endl;
if (type == "basic"){
cout << details << endl;
}
cout << "The Food weight is " << weight << endl;
cout << "The Food costs " << cost << endl;
}
int main()
{
Food menu[100]; // declare 2 object of type Food
int choose = 0, newNum2, newNum3, i = 0;
string newNum;
while (choose != 4){
cout << "Menu" << endl;
cout << "1. Add a food item\n"
"2. Modify a food item\n"
"3. Delete a food item\n"
"4. Exit this menu\n" << endl;
cin >> choose;
if (choose == 1){
cout << "Enter item's location to be added" << endl;
cin >> i;
menu[i].addFood();
}
if (choose == 2){
cout << "Enter new Values" << endl;
cin >> newNum;
cin >> newNum2;
cin >> newNum3;
cout << "Enter item's location to be modified" << endl;
cin >> i;
menu[i].modifyFood(newNum, newNum2, newNum3);
menu[i].displayFood();
}
if (choose == 3){
cout << "Enter item's location to be deleted" << endl;
cin >> i;
menu[i].modifyFood(NULL, NULL, NULL);
menu[i].displayFood();
}
}
system("PAUSE");
return 0;
}