forked from PatrickHentz/A-First-Book-of-C-Exercises-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise 10.2 (Completed)
930 lines (797 loc) · 20.9 KB
/
Exercise 10.2 (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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
1. (Review) Define the following terms:
a. Class - the combination of data and associated operations
b. Object - are created from classes
c. Declaration section - declares both the data types and finctions for the class
d. Implementation section - defines the functions whose prototypes have been declared in the declartion section
e. Instance variable - variables lised in the class declaration
f. Member method - functions listed in the class declartion
g. Data member - variables listed in the class declaration
h. Constructor - is any method with the same name as it class
i. Class instance - is an object
j. Services - another name for the operations defined in a class implementation section
k. Methods - variable listed in the class declartion
l. Interface - consists of a class's public member method declaarations and any supporting commands
2. (Practice) Write a class declaration section for each of the following specifications. In each
case, include a prototype for a constructor and a member method named showData() that can
be used to display data member values.
a. A class named Time that has integer data members named secs, mins, and hours
class Time
{
private:
int hour;
int minute;
int seconds;
public:
Time(int =7, int = 40, int = 25);
void setTime(int,int,int);
void showTime();
};
b. A class named Complex that has double-precision data members named real and imaginary
class Complex
{
private:
double real;
double imaginary;
public:
Complex(double = 5, double = 15);
void setComplex(double,double);
void showComplex();
};
c. A class named Circle that has integer data members named xcenter and ycenter and a double-precision data member named radius
class Circle
{
private:
int xcenter;
int ycenter;
double radius;
public:
Circle(int = 5, int = 5, double = 3);
void setCircle(int,int,double);
void showCircle();
};
d. A class named System that has character data members named computer, printer, and screen, each capable of holding 30 characters (including the end-of-string NULL),
and double-precision data members named compPrice, printPrice, and scrnPrice
class System
{
private:
char computer;
char printer;
char screen;
double compPrice;
double printPrice;
double scrnPrice;
public:
System(char = Dell,double = 500, char = HP, double = 300, char = Dell, double = 900);
void setSystem(char,double,char,double,char,double);
void showSystem();
};
3. (Practice) a. Construct a class implementation section for the constructor and showData()
member methods corresponding to the class declaration created for Exercise 2a.
// class implementation section
Time::Time(int h, int m, int s){
hour = h;
minute = m;
seconds = s;
}
void Time::setTime(int h, int m, int s){
hour = h;
minute = m;
seconds = s;
}
void Time::showTime()
{
cout << "The time is ";
cout << setfill('0')
<< setw(2) << hour << ':'
<< setw(2) << minute << ':'
<< setw(2) << seconds << endl;
return;
}
b. Construct a class implementation section for the constructor and showData() methods corresponding
to the class declaration created for Exercise 2b.
// class implementation section
Complex::Complex(double r, double i){
real = r;
imaginary = i;
}
void Complex::setComplex(double r , double i){
real = r;
imaginary = i;
}
void Complex::showComplex()
{
cout << "The Complex number is ";
cout << setfill('0')
<< setw(2) << real << '+'
<< setw(2) << imaginary << 'i' << endl;
return;
}
c. Construct a class implementation section for the constructor and showData() methods
corresponding to the class declaration created for Exercise 2c.
// class implementation section
Circle::Circle(int x, int y, double r){
xcenter = x;
ycenter = y;
radius = r;
}
void Circle::setCircle(int x, int y, double r){
xcenter = x;
ycenter = y;
radius = r;
}
void Circle::showCircle()
{
cout << "The Circle x,y and radius are ";
cout << setfill('0')
<< setw(2) << xcenter << ','
<< setw(2) << ycenter << " and the radius "
<< setw(2) << radius << endl;
return;
}
d. Construct a class implementation section for the constructor and showData() methods corresponding
to the class declaration created for Exercise 2d.
// class implementation section
System::System(char c, double cp, char p, double pp, char s, double sp){
computer = c;
compPrice = cp;
printer = p;
printPrice = pp;
screen = s;
scrnPrice = sp;
}
void System::setSystem(char c, double cp, char p, double pp, char s, double sp){
computer = c;
compPrice = cp;
printer = p;
printPrice = pp;
screen = s;
scrnPrice = sp;
}
void System::showSystem()
{
cout << "The Electroic's prices are";
cout << setw(2) << computer << " $"
<< setw(3) << compPrice << " Show Printer "
<< setw(2) << printer << " $"
<< setw(3) << printPrice << "Show Screen"
<< setw(2) << screen << " $"
<< setw(3) << scrnPrice << endl;
return;
}
4. (Program) a. Include the class declaration and implementation sections prepared for
Exercises 2a and 3a in a complete working program.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
//class declaration section
class Time
{
private:
int hour;
int minute;
int seconds;
public:
Time(int = 7, int = 40, int = 25); // constructor
void setTime(int, int, int); // member method to copy time
void showTime(); // member method to display time
};
// class implementation section
Time::Time(int h, int m, int s){
hour = h;
minute = m;
seconds = s;
}
void Time::setTime(int h, int m, int s){
hour = h;
minute = m;
seconds = s;
}
void Time::showTime()
{
cout << "The time is ";
cout << setfill('0')
<< setw(2) << hour << ':'
<< setw(2) << minute << ':'
<< setw(2) << seconds << endl;
return;
}
int main(){
Time a, b, c(10, 3, 34);
b.setTime(10, 9, 45);
a.showTime();
b.showTime();
c.showTime();
system("PAUSE");
return 0;
}
b. Include the class declaration and implementation sections prepared for Exercises 2b and 3b
in a complete working program.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
//class declaration section
class Complex
{
private:
double real;
double imaginary;
public:
Complex(double = 5, double = 15);
void setComplex(double, double);
void showComplex();
};
// class implementation section
Complex::Complex(double r, double i){
real = r;
imaginary = i;
}
void Complex::setComplex(double r , double i){
real = r;
imaginary = i;
}
void Complex::showComplex()
{
cout << "The Complex number is ";
cout << setfill('0')
<< setw(2) << real << '+'
<< setw(2) << imaginary << 'i' << endl;
return;
}
int main(){
Complex a, b, c( 3, 34);
b.setComplex( 9, 45);
a.showComplex();
b.showComplex();
c.showComplex();
system("PAUSE");
return 0;
}
c. Include the class declaration and implementation sections prepared for Exercises 2c and 3c
in a complete working program.
#include <iostream>
#include <iomanip>
using namespace std;
//class declaration section
class Circle
{
private:
int xcenter;
int ycenter;
double radius;
public:
Circle(int = 5, int = 5, double = 5);
void setCircle(int, int, double);
void showCircle();
};
// class implementation section
Circle::Circle(int x, int y, double r){
xcenter = x;
ycenter = y;
radius = r;
}
void Circle::setCircle(int x, int y, double r){
xcenter = x;
ycenter = y;
radius = r;
}
void Circle::showCircle()
{
cout << "The Circle x,y and radius are ";
cout << setfill('0')
<< setw(2) << xcenter << ','
<< setw(2) << ycenter << " and the radius "
<< setw(2) << radius << endl;
return;
}
int main(){
Circle a, b, c( 3, 4, 5.23);
b.setCircle( 9, 4, 8.34);
a.showCircle();
b.showCircle();
c.showCircle();
system("PAUSE");
return 0;
}
d. Include the class declaration and implementation sections prepared for Exercises 2d and 3d
in a complete working program.
#include <iostream>
#include <iomanip>
using namespace std;
//class declaration section
class System
{
private:
char computer;
char printer;
char screen;
double compPrice;
double printPrice;
double scrnPrice;
public:
System(char = 'D', double = 500, char = 'H', double = 300, char = 'D', double = 900);
void setSystem(char, double, char, double, char, double);
void showSystem();
};
// class implementation section
System::System(char c, double cp, char p, double pp, char s, double sp){
computer = c;
compPrice = cp;
printer = p;
printPrice = pp;
screen = s;
scrnPrice = sp;
}
void System::setSystem(char c, double cp, char p, double pp, char s, double sp){
computer = c;
compPrice = cp;
printer = p;
printPrice = pp;
screen = s;
scrnPrice = sp;
}
void System::showSystem()
{
cout << "The Electroic's prices are";
cout << setw(2) << computer << " $"
<< setw(3) << compPrice << " Show Printer "
<< setw(2) << printer << " $"
<< setw(3) << printPrice << "Show Screen"
<< setw(2) << screen << " $"
<< setw(3) << scrnPrice << endl;
return;
}
int main(){
System a, b, c( 'A', 400,'B', 500, 'C', 100);
b.setSystem( 'D',900,'E', 400, 'F', 600);
a.showSystem();
b.showSystem();
c.showSystem();
system("PAUSE");
return 0;
}
5. (Desk check) Determine the errors in the following class declaration section:
class Employee
{
public:
int empnum;
char code;
private:
class(int = 0); // This line of code does not create a proper contructor
void showemp(int, char);
};
---------Corrected Code------------
class Employee
{
public:
int empnum;
char code;
private:
Employee(int = 0, char = 'E');
void showemp(int, char);
};
6. (Modify) a. Add another member method named convert() to Program 10.1 that does the
following: The method should access the month, year, and day data members and display and
then return an integer calculated as year × 10000 + month × 100 + day. For example, if the date
is 4/1/2014, the returned value is 20140401. (Dates in this form are useful when performing
sorts because placing the numbers in numerical order automatically places the corresponding
dates in chronological order.)
// class declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2012); // constructor
void setDate(int, int, int); // member method to copy a date
void showDate(); // member method to display a date
int convert(); //converts the dates to a sorting order
};
// class implementation section
int Date::convert()
{
int con = year * 10000 + month * 100 + day;
return con;
}
b. Include the modified Date class constructed for Exercise 6a in a complete C++ program.
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2012); // constructor
void setDate(int, int, int); // member method to copy a date
void showDate(); // member method to display a date
int convert(); //converts the dates to a sorting order
};
// class implementation section
Date::Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
void Date::setDate(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
return;
}
void Date::showDate()
{
cout << "The date is ";
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100; // extract the last 2 year digits
cout << endl;
return;
}
int Date::convert()
{
int con = year * 10000 + month * 100 + day;
return con;
}
int main()
{
Date a, b, c(4, 1, 2000),d; // declare 3 objects
b.setDate(12, 25, 2009); // assign values to b's data members
a.showDate(); // display object a's values
b.showDate(); // display object b's values
c.showDate(); // display object c's values
cout << d.convert()<< endl;
system("PAUSE");
return 0;
}
7. (Modify) a. Add to Program 10.1’s class definition an additional member method named
leapyr() that returns a true if the year is a leap year and a false if it’s not a leap year. A leap
year is any year that’s evenly divisible by 4 but not by 100, with the exception that all years
evenly divisible by 400 are leap years. For example, the year 1996 is a leap year because it’s
evenly divisible by 4 and not evenly divisible by 100. The year 2000 is a leap year because
it’s evenly divisible by 400.
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2012); // constructor
void setDate(int, int, int); // member method to copy a date
void showDate(); // member method to display a date
bool leapyr(); //converts the dates to a sorting order
};
bool Date::leapyr(){
if ((year % 4) == 0 && (year % 100) != 0 || (year % 400) == 0){
cout << "This year is a leap year" << endl;
return true;
}
else {
cout << "This is not a leap year" << endl;
return false;
}
}
b. Include the class definition constructed for Exercise 7a in a complete C++ program. The
main() function should display the message The year is a leap year or the message
The year is not a leap year (depending on the Date object’s year value).
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2012); // constructor
void setDate(int, int, int); // member method to copy a date
void showDate(); // member method to display a date
bool leapyr(); //converts the dates to a sorting order
};
// class implementation section
Date::Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
void Date::setDate(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
return;
}
void Date::showDate()
{
cout << "The date is ";
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100; // extract the last 2 year digits
cout << endl;
return;
}
bool Date::leapyr(){
if ((year % 4) == 0 && (year % 100) != 0 || (year % 400) == 0){
cout << "This year is a leap year" << endl;
return true;
}
else {
cout << "This is not a leap year" << endl;
return false;
}
}
int main()
{
Date a, b, c(4, 1, 2000); // declare 3 objects
b.setDate(12, 25, 2009); // assign values to b's data members
a.showDate(); // display object a's values
b.showDate(); // display object b's values
c.showDate(); // display object c's values
c.leapyr();
a.leapyr();
b.leapyr();
system("PAUSE");
return 0;
}
8. (Modify) a. Add a member method named dayOfWeek() to Program 10.1’s class definition
that determines the day of the week for any Date object. An algorithm for determining the day
of the week, known as Zeller’s algorithm, is the following:
If mm is less than 3
mm = mm + 12 and yyyy = yyyy - 1
Endif
Set century = int(yyyy/100)
Set year = yyyy % 100
Set T = dd + int(26 * (mm + 1)/10) + year + int(year / 4)
int(century / 4) - 2 * century
Set DayOfWeek = T % 7
If DayOfWeek is less than 0
DayOfWeek = DayOfWeek + 7
Endif
Using this algorithm, the variable DayOfWeek has a value of 0 if the date is a Saturday, 1 if a
Sunday, and so forth.
int Date::dayOfWeek()
{
if (month < 3){
month = month + 12;
year = year - 1;
}
int century = int(year / 100);
int year1 = year % 100;
int T = day + int(26 * (month + 1) / 10) + year + int(year / 4);
int(century / 4) - 2 * century;
int DayOfWeek = T % 7;
if (DayOfWeek < 0){
DayOfWeek = DayOfWeek + 7;
}
return DayOfWeek;
}
b. Include the class definition constructed for Exercise 8a in a complete C++ program. The
main() function should display the name of the day (Sun, Mon, Tue, and so on) for the
Date object being tested.
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2012); // constructor
void setDate(int, int, int); // member method to copy a date
void showDate(); // member method to display a date
void dayOfWeek();
};
// class implementation section
Date::Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
void Date::setDate(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
return;
}
void Date::showDate()
{
cout << "The date is ";
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100; // extract the last 2 year digits
cout << endl;
;
return;
}
void Date::dayOfWeek()
{
if (month < 3){
month = month + 12;
year = year - 1;
}
int century = int(year / 100);
int year1 = year % 100;
int T = day + int(26 * (month + 1) / 10) + year + int(year / 4);
int(century / 4) - 2 * century;
int DayOfWeek = T % 7;
if (DayOfWeek < 0){
DayOfWeek = DayOfWeek + 7;
}
if (DayOfWeek == 0){
cout << "Sat" << endl;
}
else if (DayOfWeek == 1){
cout << "Sun" << endl;
}
else if (DayOfWeek == 2){
cout << "Mon" << endl;
}
else if (DayOfWeek == 3){
cout << "Tues" << endl;
}
else if (DayOfWeek == 4){
cout << "Wed" << endl;
}
else if (DayOfWeek == 5){
cout << "Thur" << endl;
}
else if (DayOfWeek == 6){
cout << "Fri" << endl;
}
return;
}
int main()
{
Date a, b, c(4, 1, 2000); // declare 3 objects
b.setDate(12, 25, 2009); // assign values to b's data members
a.showDate(); // display object a's values
b.showDate(); // display object b's values
c.showDate(); // display object c's values
a.dayOfWeek();
b.dayOfWeek();
c.dayOfWeek();
system("PAUSE");
return 0;
}
9. (Program) a. Construct a class named Rectangle that has double-precision data members
named length and width. The class should have member methods named perimeter() and
area() to calculate a rectangle’s perimeter and area, a member method named setData() to
set a rectangle’s length and width, and a member method named showData() that displays a
rectangle’s length, width, perimeter, and area.
Done!!
b. Include the Rectangle class constructed in Exercise 9a in a working C++ program.
#include <iostream>
#include <iomanip>
using namespace std;
// class declaration section
class Rectangle
{
private:
double length;
double width;
public:
Rectangle(double = 5, double = 9); // constructor
void setData(double,double); // member method to copy a Rectangle
void showData();
void perimeter(); // member method to display a Rectangle
void area();
};
// class implementation section
Rectangle::Rectangle(double l, double w)
{
length = l;
width = w;
}
void Rectangle::setData(double l, double w)
{
length = l;
width = w;
return;
}
void Rectangle::showData()
{
cout << "The Rectangle has a length, width, perimeter, and area " << endl;
cout << setw(2) <<"Lenght: "<< length << endl;
cout << setw(2) <<"Width: " << width << endl;
cout << "Perimeter "; perimeter();
cout << "Area "; area();
return;
}
void Rectangle::perimeter()
{
double per = (length * 2) + (width * 2);
cout << per << endl;
return;
}
void Rectangle::area(){
double ar = length * width;
cout << ar << endl;
return;
}
int main()
{
Rectangle a, b, c(4, 1); // declare 3 objects
b.setData(12, 25); // assign values to b's data members
a.showData(); // display object a's values
b.showData(); // display object b's values
c.showData(); // display object c's values
system("PAUSE");
return 0;
}
10. (Modify) a. Modify the Date class defined in Program 10.1 to include a nextDay() method
that increments a date by one day. Test your method to ensure that it increments days into a
new month and into a new year correctly.
void Date::nextDay(){
if ( month != 12 && day == 31) {
month++;
day = 1;
}
else if ( month == 12 && day == 31){
year++;
day = 1;
month = 1;
}
else if ( day <= 30 && month !=12){
day++;
}
cout << month <<'/' << day << '/'<< year << endl;
}
b. Modify the Date class defined in Program 10.1 to include a priorDay() method that decrements
a date by one day. Test your method to ensure that it decrements days into a prior
month and into a prior year correctly.
void Date::priorDay(){
if ( month != 1 && day == 1) {
month--;
day = 31;
}
else if ( month == 1 && day == 1){
--year;
day = 31;
month = 12;
}
else if ( day <= 30 && month !=12){
day--;
}
cout << month <<'/' << day << '/'<< year << endl;
}
11. (Modify) Modify the Date class in Program 10.1 to contain a method that compares two Date
objects and returns the larger of the two. The method should be written according to the following
algorithm:
Accept two Date values as parameters
Determine the later date by using the following procedure:
Convert each date into an integer value having the form yyyymmdd
(This can be accomplished with the formula year * 10000 + month * 100 + day)
Compare the corresponding integers for each date
The larger integer corresponds to the later date
Return the later date
void Date::compareDate(Date a, Date b){
int year1 = a.year * 10000 + a.month * 100 + a.day;
int year2 = b.year * 10000 + b.month * 100 + b.day;
if (year1 > year2){
cout << year1 << endl;
}
else{
cout << year2 << endl;
}
return;
}