Finished Wednesday, 19 February 2025, 2:01 PM Monday, 24 February 2025, 11:43 PM 5 Days 9 Hours 5.00/5.00 Out of 10.00 (%)
Finished Wednesday, 19 February 2025, 2:01 PM Monday, 24 February 2025, 11:43 PM 5 Days 9 Hours 5.00/5.00 Out of 10.00 (%)
Question 1 In the coordinate plane, we have class Point to store a point with it's x-y coordinate.
Correct
Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.
Mark 1.00 out of
1.00 Note: For exercises in Week 1, we have #include <bits/stdc++.h> and using namespace std;
For example:
Test Result
Reset answer
1 class Point
2 ▼ {
3 private:
4 double x, y;
5
6 public:
7 Point()
8 ▼ {
9 ▼ /*
10 * STUDENT ANSWER
11 * TODO: set zero x-y coordinate
12 */
13 this -> x = 0.0;
14 this -> y = 0.0;
15 }
16
17 Point(double x, double y)
18 ▼ {
19 ▼ /*
20 * STUDENT ANSWER
21 */
22 this -> x = x;
23 this -> y = y;
24
25 }
26
27 void setX(double x)
28 ▼ {
29 ▼ /*
30 * STUDENT ANSWER
31 */
32 this -> x = x;
33 }
34
35 void setY(double y)
36 ▼ {
37 ▼ /*
38 * STUDENT ANSWER
39 */
40 this -> y = y;
41
41
42 }
43
44 double getX() const
45 ▼ {
46 ▼ /*
47 * STUDENT ANSWER
48 */
49 return x;
50 }
51
52 double getY() const
53 ▼ {
54 ▼ /*
55 * STUDENT ANSWER
56 */
57 return y;
58 }
59
60 double distanceToPoint(const Point& pointA)
61 ▼ {
62 ▼ /*
63 * STUDENT ANSWER
64 * TODO: calculate the distance from this point to point A in the coordinate plane
65 */
66 return sqrt(pow(this -> x - pointA.x,2)+pow(this -> y - pointA.y,2));
67 }
68 };
Correct
Marks for this submission: 1.00/1.00.
Question 2
In the coordinate plane, a circle is defined by center and radius.
Correct
Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.
Mark 1.00 out of
1.00 Note: you can use implemented class Point in previous question
For example:
Test Result
Reset answer
1 class Point
2 ▼ {
3 private:
4 double x, y;
5
6 public:
7 Point()
8 ▼ {
9 ▼ /*
9 ▼ /*
10 * STUDENT ANSWER
11 * TODO: set zero x-y coordinate
12 */
13 this -> x = 0.0;
14 this -> y = 0.0;
15 }
16
17 Point(double x, double y)
18 ▼ {
19 ▼ /*
20 * STUDENT ANSWER
21 */
22 this -> x = x;
23 this -> y = y;
24
25 }
26
27 void setX(double x)
28 ▼ {
29 ▼ /*
30 * STUDENT ANSWER
31 */
32 this -> x = x;
33 }
34
35 void setY(double y)
36 ▼ {
37 ▼ /*
38 * STUDENT ANSWER
39 */
40 this -> y = y;
41
42 }
43
44 double getX() const
45 ▼ {
46 ▼ /*
47 * STUDENT ANSWER
48 */
49 return x;
50 }
51
52 double getY() const
53 ▼ {
54 ▼ /*
55 * STUDENT ANSWER
56 */
57 return y;
58 }
59
60 double distanceToPoint(const Point& pointA)
61 ▼ {
62 ▼ /*
63 * STUDENT ANSWER
64 * TODO: calculate the distance from this point to point A in the coordinate plane
65 */
66 return sqrt(pow(this -> x - pointA.x,2)+pow(this -> y - pointA.y,2));
67 }
68 };
69
70 class Circle
71 ▼ {
72 private:
73 Point center;
74 double radius;
75
76 public:
77 Circle()
78 ▼ {
79 ▼ /*
80 * STUDENT ANSWER
81 * TODO: set zero center's x-y and radius
82 */
83 center = Point(0, 0);
84 radius = 0;
85
86 }
86 }
87
88 Circle(Point center, double radius)
89 ▼ {
90 ▼ /*
91 * STUDENT ANSWER
92 */
93 this -> center = center;
94 this -> radius = radius;
95 }
96
97 Circle(const Circle &circle)
98 ▼ {
99 ▼ /*
100 * STUDENT ANSWER
101 */
102 this -> center = circle.center;
103 this -> radius = circle.radius;
104
105 }
106
107 void setCenter(Point point)
108 ▼ {
109 ▼ /*
110 * STUDENT ANSWER
111 */
112 this -> center = point;
113 }
114
115 void setRadius(double radius)
116 ▼ {
117 ▼ /*
118 * STUDENT ANSWER
119 */
120 this ->radius = radius;
121 }
122
123 Point getCenter() const
124 ▼ {
125 ▼ /*
126 * STUDENT ANSWER
127 */
128 return center;
129 }
130
131 double getRadius() const
132 ▼ {
133 ▼ /*
134 * STUDENT ANSWER
135 */
136 return radius;
137 }
138
139 void printCircle()
140 ▼ {
141 printf("Center: {%.2f, %.2f} and Radius %.2f\n", this->center.getX(), this->center.get
142 }
143 };
✓ Circle A; Center: {0.00, 0.00} and Radius Center: {0.00, 0.00} and Radius ✓
A.printCircle(); 0.00 0.00
Correct
Marks for this submission: 1.00/1.00.
Your task is to define the constructors and the methods of the class.
Note:
In this task, iostream library has been included, and namespace std is being used. No other libraries are allowed.
For example:
Test Result
Reset answer
1 ▼ Character::Character() {
2 // STUDENT ANSWER
3 hp = 0;
4 x = 0;
5 y = 0;
6
7 }
8
9 ▼ Character::Character(int hp, int x, int y) {
10 // STUDENT ANSWER
11 this->hp = hp;
12 this->x = x;
13 this->y = y;
14 }
15
16 ▼ int Character::getHp() {
17 // STUDENT ANSWER
18 return hp;
19 }
20
21 ▼ void Character::setHp(int hp) {
22 // STUDENT ANSWER
23 this -> hp = hp;
24 }
25
26 ▼ int Character::getX() {
27 // STUDENT ANSWER
28 return x;
28 return x;
29 }
30
31 ▼ void Character::setX(int x) {
32 // STUDENT ANSWER
33 this -> x = x;
34 }
35
36 ▼ int Character::getY() {
37 // STUDENT ANSWER
38 return y;
39 }
40
41 ▼ void Character::setY(int y) {
42 // STUDENT ANSWER
43 this -> y = y;
44 }
45
46 ▼ int Character::getManhattanDistTo(Character* other) {
47 // STUDENT ANSWER
48 return abs(x - other->getX()) + abs(y - other->getY());
49
50 }
✓ Character ch2; 0 0 0 0 0 0 ✓
cout << ch2.getHp() << " " << ch2.getX() << " " << ch2.getY();
✓ Character ch4; 4 4 ✓
ch4.setX(4);
cout << ch4.getX();
✓ Character ch5; 5 5 ✓
ch5.setY(5);
cout << ch5.getY();
✓ Character ch6; 6 6 ✓
ch6.setHp(6);
cout << ch6.getHp();
Correct
Marks for this submission: 1.00/1.00.
Question 4
Hoang is a K19 student studying at Bach Khoa University. He plans to write a book management software for the
Correct
library. In the class design, Hoang has designed the class Book as follows:
Mark 1.00 out of
1.00 class Book
{
private:
char* title;
char* authors;
int publishingYear;
public:
// some method
}
Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.
Note: For exercises in Week 2, we have #include <bits/stdc++.h> and using namespace std;
For example:
Test Result
Reset answer
1 class Book
2 ▼ {
3 private:
4 char* title;
5 char* authors;
6 int publishingYear;
7
8 public:
9 Book()
10 ▼ {
11 ▼ /*
12 * STUDENT ANSWER
13 * TODO: set zero publishingYear and null pointer
14 */
15 title = nullptr;
16 authors = nullptr;
17 publishingYear = 0;
18 }
19
20 Book(const char* title, const char* authors, int publishingYear)
21 ▼ {
22 ▼ /*
23 * STUDENT ANSWER
24 */
25 this->title = new char[strlen(title) + 1];
26 strcpy(this->title, title);
27 this->authors = new char[strlen(authors) + 1];
28 strcpy(this->authors, authors);
29 this->publishingYear = publishingYear;
30 }
31
32 Book(const Book &book)
33 ▼ {
34 ▼ /*
35 * STUDENT ANSWER
36 * TODO: deep copy constructor
37 */
38 this->title = new char[strlen(book.title) + 1];
39 strcpy(this->title, book.title);
40
41 this->authors = new char[strlen(book.authors) + 1];
42 strcpy(this->authors, book.authors);
43
44 this->publishingYear = book.publishingYear;
45
46 }
47
48 void setTitle(const char* title)
49 ▼ {
50 ▼ /*
51 * STUDENT ANSWER
52 */
53 delete[] this->title;
54 this->title = new char[strlen(title) + 1];
55 strcpy(this->title, title);
56 }
57
57
58 void setAuthors(const char* authors)
59 ▼ {
60 ▼ /*
61 * STUDENT ANSWER
62 */
63 delete[] this->authors;
64 this->authors = new char[strlen(authors) + 1];
65 strcpy(this->authors, authors);
66 }
67
68 void setPublishingYear(int publishingYear)
69 ▼ {
70 ▼ /*
71 * STUDENT ANSWER
72 */
73 this->publishingYear = publishingYear;
74 }
75
76 char* getTitle() const
77 ▼ {
78 ▼ /*
79 * STUDENT ANSWER
80 */
81 return title;
82
83 }
84
85 char* getAuthors() const
86 ▼ {
87 ▼ /*
88 * STUDENT ANSWER
89 */
90 return authors;
91 }
92
93 int getPublishingYear() const
94 ▼ {
95 ▼ /*
96 * STUDENT ANSWER
97 */
98 return publishingYear;
99 }
100
101 ~Book()
102 ▼ {
103 ▼ /*
104 * STUDENT ANSWER
105 */
106 delete[] title;
107 delete[] authors;
108 }
109
110 ▼ void printBook(){
111 printf("%s\n%s\n%d", this->title, this->authors, this->publishingYear);
112 }
113 };
✓ Book book1("Giai tich 1","Nguyen Dinh Huy",2000); Giai tich 1 Giai tich 1 ✓
book1.printBook(); Nguyen Dinh Huy Nguyen Dinh Huy
2000 2000
✓ Book book1("Giai tich 1","Nguyen Dinh Huy",2000); Giai tich 1 Giai tich 1 ✓
Book book2 = book1; Nguyen Dinh Huy Nguyen Dinh Huy
book2.printBook(); 2000 2000
Correct
Marks for this submission: 1.00/1.00.
Question 5
1. In the toy store, all toy has a price. Car toy has a price and color, Puzzle toy has a price and size. We have to
Correct
implement class CarToy and class PuzzleToy which inherit from class Toy.
Mark 1.00 out of 2. class ToyBox has a pointer array to store a list of toys (up to 5 items including car and puzzle) and number of
1.00
items in the box.
Your task is to implement two function addItem(…) in class ToyBox. If successfully added, the function returns the
current number of toys in the box. If the box is full, return -1.
For example:
Test Result
ToyBox box;
box.addItem(car);
box.addItem(puzzle);
box.printBox();
1 enum Color
2 ▼ {
3 red,
4 green,
5 blue
6 };
7 enum Size
8 ▼ {
9 small,
10 medium,
11 big
12 };
13
14 class Toy
15 ▼ {
16 protected:
17 double price;
18
19 public:
20 Toy(double price)
21 ▼ {
22 this->price = price;
23 }
24
25 virtual void printType() = 0;
26 friend class ToyBox;
27 };
28
29 class CarToy : public Toy
30 ▼ {
31 private:
32 Color color;
33
34 public:
35 CarToy(double price, Color color) : Toy(price)
36 ▼ {
37 ▼ /*
38 * STUDENT ANSWER
38 * STUDENT ANSWER
39 */
40 this -> color = color;
41 }
42
43 void printType()
44 ▼ {
45 cout << "This is a car toy\n";
46 }
47
48 friend class ToyBox;
49 };
50
51 class PuzzleToy : public Toy
52 ▼ {
53 private:
54 Size size;
55
56 public:
57 PuzzleToy(double price, Size size) : Toy(price)
58 ▼ {
59 ▼ /*
60 * STUDENT ANSWER
61 */
62 this -> size = size;
63 }
64
65 void printType()
66 ▼ {
67 cout << "This is a puzzle toy\n";
68 }
69
70 friend class ToyBox;
71 };
72
73 class ToyBox
74 ▼ {
75 private:
76 Toy* toyBox[5];
77 int numberOfItems;
78
79 public:
80 ToyBox()
81 ▼ {
82 ▼ /*
83 * STUDENT ANSWER
84 * TODO: set zero numberOfItems and nullptr toyBox
85 */
86 numberOfItems = 0;
87 ▼ for (int i = 0; i < 5; i++) {
88 toyBox[i] = nullptr;
89 }
90 }
91
92 int addItem(const CarToy& carToy)
93 ▼ {
94 ▼ /*
95 * STUDENT ANSWER
96 * TODO: function add a new Car toy to the box.
97 If successfully added, the function returns the current number of toys in the
98 If the box is full, return -1.
99 */
100 if (numberOfItems >= 5) return -1;
101 toyBox[numberOfItems] = new CarToy(carToy);
102 numberOfItems++;
103 return numberOfItems;
104 }
105
106 int addItem(const PuzzleToy& puzzleToy)
107 ▼ {
108 ▼ /*
109 * STUDENT ANSWER
110 * TODO: function add a new Puzzle toy to the box.
111 If successfully added, the function returns the current number of toys in the
112 If the box is full, return -1.
113 */
114 if (numberOfItems >= 5) return -1;
115 toyBox[numberOfItems] = new PuzzleToy(puzzleToy);
115 toyBox[numberOfItems] = new PuzzleToy(puzzleToy);
116 numberOfItems++;
117 return numberOfItems;
118 }
119
120 void printBox()
121 ▼ {
122 for (int i = 0; i < numberOfItems; i++)
123 toyBox[i]->printType();
124 }
125 };
ToyBox box;
box.addItem(car);
box.addItem(puzzle);
box.printBox();
✓ Toy* toy = new CarToy(30000,red); This is a car toy This is a car toy ✓
toy->printType();
Correct
Marks for this submission: 1.00/1.00.