-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCourse3_week4_codes.cpp
284 lines (213 loc) · 4.84 KB
/
Course3_week4_codes.cpp
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
Exercise-1
#include <iostream>
using namespace std;
class CelestialBody {
private:
double earth_diameter = 12756.3;
public:
CelestialBody(string n, double diam, double dist, int m) {
name = n;
diameter = diam;
distance = dist;
moons = m;
}
string name;
double diameter;
double distance;
int moons;
double ComparedToEarth() {
double relative_size = diameter / earth_diameter;
return relative_size;
}
};
int main() {
CelestialBody planet("Jupiter", 142984, 778360000, 79);
cout << planet.ComparedToEarth() << endl;
return 0;
}
=======================
Exercise-2
#include <iostream>
using namespace std;
//add class definitions below this line
class CelestialBody {
public:
CelestialBody(string n, double diam, double dist, int m) {
name = n;
diameter = diam;
distance = dist;
moons = m;
}
string CloserToSun(CelestialBody cb) {
if (distance < cb.distance) {
return name;
}
else {
return cb.name;
}
}
private:
string name;
double diameter;
double distance;
int moons;
};
//add class definitions above this line
int main() {
//DO NOT EDIT the code below
CelestialBody mercury("Mercury", 4879.4, 57909000, 0);
CelestialBody venus("Venus", 12103.6, 108160000, 0);
cout << mercury.CloserToSun(venus) << endl;
//DO NOT EDIT the code above
return 0;
}
=================
Exercise-5
#include <iostream>
using namespace std;
//add class definitions below this line
class Subway {
public:
Subway() {
passengers = 0;
total_fares = 0;
}
int GetPassengers() {
return passengers;
}
void Board(int p) {
if (p >= 0) {
passengers += p;
CalculateFares(p);
}
}
void Disembark(int p) {
if (p >= 0) {
if (passengers - p < 0) {
passengers = 0;
}
else {
passengers -= p;
}
}
}
double GetFares() {
return total_fares;
}
private:
const double fare = 2.40; //variable cannot be reassigned
int passengers;
double total_fares;
void CalculateFares(int p) { //private helper class function
total_fares += p * fare;
}
};
//add class definitions above this line
int main() {
//DO NOT EDIT code below this line
Subway s;
cout << s.GetPassengers() << endl;
s.Board(23);
s.Disembark(12);
cout << s.GetPassengers() << endl;
cout << s.GetFares() << endl;
//DO NOT EDIT code above this line
return 0;
}
===========================
Exercise-4
#include <iostream>
using namespace std;
//add class definitions below this line
class MarbleBag {
public:
MarbleBag(int r, int b, int y) {
red = r;
blue = b;
yellow = y;
}
int AddRed(int amount) {
return red += amount;
}
int AddBlue(int amount) {
return blue += amount;
}
int AddYellow(int amount) {
return yellow += amount;
}
int RedTotal() {
return red;
}
int BlueTotal() {
return blue;
}
int YellowTotal() {
return yellow;
}
int BagTotal() {
return red + blue + yellow;
}
private:
int red;
int blue;
int yellow;
};
//add class definitions above this line
int main() {
//DO NOT EDIT code below this line
MarbleBag bag(12, 8, 19);
bag.AddRed(4);
bag.AddBlue(12);
bag.AddYellow(-1);
bag.AddBlue(-3);
cout << "There are " << bag.RedTotal() << " red marbles." << endl;
cout << "There are " << bag.BlueTotal() << " blue marbles." << endl;
cout << "There are " << bag.YellowTotal() << " yellow marbles." << endl;
cout << "There are " << bag.BagTotal() << " total marbles." << endl;
//DO NOT EDIT code above this line
return 0;
}
=========================
Exercise-3
#include <iostream>
using namespace std;
//add class definitions below this line
class Mass {
public:
Mass(double mg, double g, double kg) {
milligrams = mg;
grams = g;
kilograms = kg;
}
double TotalWeight() {
return ((milligrams / 1000) + grams + (kilograms * 1000));
}
string CompareWeight(Mass m) {
double mass1 = TotalWeight();
double mass2 = m.TotalWeight();
if (mass1 > mass2) {
return "The first object is heavier.";
}
else if (mass2 > mass1) {
return "The second object is heavier.";
}
else {
return "The two objects weigh the same.";
}
}
private:
double milligrams;
double grams;
double kilograms;
};
//add class definitions above this line
int main() {
//DO NOT EDIT the code below
Mass m1(800, 17, 3);
Mass m2(730, 38, 2);
cout << m1.TotalWeight() << endl;
cout << m2.TotalWeight() << endl;
cout << m1.CompareWeight(m2) << endl;
//DO NOT EDIT the code above
return 0;
}