14.16. Activecode ExercisesΒΆ
Answer the following Activecode questions to assess what you have learned in this chapter.
Question
Let's write the class definition for
circle.circleshould have its radius stored in a private member variable. Also write the constructor forcircle, which takes a radius as a parameter, in addition to the public member functioncalculate_area, which returns the area of thecircle. Make sure to include theprivateandpublickeywords! Use 3.14 for the value of pi.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to wrte the class definition and constructor for
circle.
1#include <iostream>
2
3class circle {
4 private:
5 double radius;
6 public:
7 circle (double r) { radius = r; }
8 double calculate_area () { return 3.14 * radius * radius; }
9};
Question
Now that we have our
circleclass, let's write some accessor functions! Write thecirclemember functionsget_radiusandset_radius. It doesn't make sense for acircle's radius to be negative, so in yourset_radiusfunction, output an error message if the given radius is negative.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the accessor functions for
get_radiusandset_radius.
1#include <iostream>
2
3class circle {
4 private:
5 double radius;
6 public:
7 circle (double r) { radius = r; }
8 double calculate_area () { return 3.14 * radius * radius; }
9 double get_radius () {
10 return radius;
11 }
12 void set_radius (double r) {
13 if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << std::endl; }
14 else { radius = r; }
15 }
16};
Question
Write a
main. inmain, create acirclewith radius 2.4 and output the radius. Then change the radius to 3.6 and output
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the code.
1#include <iostream>
2
3class circle {
4 private:
5 double radius;
6 public:
7 circle (double r) { radius = r; }
8 double calculate_area () { return 3.14 * radius * radius; }
9 double get_radius () {
10 return radius;
11 }
12 void set_radius (double r) {
13 if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << std::endl; }
14 else { radius = r; }
15 }
16};
17
18int main() {
19 circle c(2.4);
20 std::cout << "Radius: " << c.get_radius () << std::endl;
21 c.set_radius (3.6);
22 std::cout << "New radius: " << c.get_radius () << std::endl;
23}
Question
A
rectanglecan be constructed given only two points. First, write the class definition forpoint, which stores an x and a y value in private member variables. Also write the default constructor, which sets x and y to 0, and a constructor that takes in an x_val and y_val. in addition, write its accessor functions,get_x,get_y,set_x, andset_y.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the code.
1#include <iostream>
2
3class point {
4 private:
5 double x, y;
6 public:
7 point () { x = 0; y = 0; }
8 point (double x_val, double y_val) { x = x_val; y = y_val; }
9 double get_x () { return x; }
10 double get_y () { return y; }
11 void set_x (double x_val) { x = x_val; }
12 void set_y (double y_val) { y = y_val; }
13};
Question
Now that we've defined the
pointclass, we can go back to writing therectangleclass.rectangleshould store it's upper-left and lower-right points as private member variables. Write accessor functions for these variables after the constructor. It should also have length and height stored as public member variables. Also write a constructor that takes an upper-left point and a lower-right point as parameters.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the
rectangleclass.
1#include <iostream>
2
3class point {
4 private:
5 double x, y;
6 public:
7 point () { x = 0; y = 0; }
8 point (double x_val, double y_val) { x = x_val; y = y_val; }
9 double get_x () { return x; }
10 double get_y () { return y; }
11 void set_x (double x_val) { x = x_val; }
12 void set_y (double y_val) { y = y_val; }
13};
14
15class rectangle {
16 private:
17 point upper_left, lower_right;
18 public:
19 double length, height;
20 rectangle (point up_left, point low_right) { upper_left = up_left; lower_right = low_right; }
21 point get_upper_left () { return upper_left; }
22 point get_lower_right () { return lower_right; }
23 void set_upper_left (point p) { upper_left = p; }
24 void set_lower_right (point p) { lower_right = p; }
25};
Question
Write the
rectanglemember functioncalculate_sides, which finds the length and height of the rectangle using the storedpoint``s. Afterwards, write the ``rectanglemember functioncalculate_area, which returns the area of the rectangle.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the
calculate_sidesandcalculate_areamember functions.
1#include <iostream>
2
3class point {
4 private:
5 double x, y;
6 public:
7 point () { x = 0; y = 0; }
8 point (double x_val, double y_val) { x = x_val; y = y_val; }
9 double get_x () { return x; }
10 double get_y () { return y; }
11 void set_x (double x_val) { x = x_val; }
12 void set_y (double y_val) { y = y_val; }
13};
14
15class rectangle {
16 private:
17 point upper_left, lower_right;
18 public:
19 double length, height;
20 rectangle (point up_left, point low_right) { upper_left = up_left; lower_right = low_right; }
21 point get_upper_left () { return upper_left; }
22 point get_lower_right () { return lower_right; }
23 void set_upper_left (point p) { upper_left = p; }
24 void set_lower_right (point p) { lower_right = p; }
25public:
26 void calculate_sides();
27 double calculate_area();
28};
29
30void rectangle::calculate_sides () {
31 length = get_lower_right().get_x() - get_upper_left().get_x();
32 height = get_upper_left().get_y() - get_lower_right().get_y();
33}
34
35double rectangle::calculate_area () {
36 return length * height;
37}
Question
Write a
maininmain, create arectanglewith corners at (2.5, 7.5) and (8, 1.5). Print out the length and height, calculate the area, and print out the area. Then change the upper_left corner to be at (4.2, 10.7) and print out the new area.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to create this
rectangle.
1#include <iostream>
2
3class point {
4 private:
5 double x, y;
6 public:
7 point () { x = 0; y = 0; }
8 point (double x_val, double y_val) { x = x_val; y = y_val; }
9 double get_x () { return x; }
10 double get_y () { return y; }
11 void set_x (double x_val) { x = x_val; }
12 void set_y (double y_val) { y = y_val; }
13};
14
15class rectangle {
16 private:
17 point upper_left, lower_right;
18 public:
19 double length, height;
20 rectangle (point up_left, point low_right) { upper_left = up_left; lower_right = low_right; }
21 point get_upper_left () { return upper_left; }
22 point get_lower_right () { return lower_right; }
23 void set_upper_left (point p) { upper_left = p; }
24 void set_lower_right (point p) { lower_right = p; }
25public:
26 void calculate_sides();
27 double calculate_area();
28};
29
30void rectangle::calculate_sides () {
31 length = get_lower_right().get_x() - get_upper_left().get_x();
32 height = get_upper_left().get_y() - get_lower_right().get_y();
33}
34
35double rectangle::calculate_area () {
36 return length * height;
37}
38
39int main() {
40 point p1(2.5, 7.5);
41 point p2(8, 1.5);
42 rectangle r(p1, p2);
43 r.calculate_sides();
44 std::cout << "Length: " << r.length << ", Height: " << r.height << std::endl;
45 std::cout << "Area: " << r.calculate_area() << std::endl;
46 point p3(4.2, 10.7);
47 r.set_upper_left(p3);
48 r.calculate_sides();
49 std::cout << "New area: " << r.calculate_area() << std::endl;
50}
Question
Let's write the
dateclass.datestores information about the day, month, and year in private variables, in addition to avectorof the number of days in each month. Write accessor functions for each variable, keeping in mind the valid values each variable can take. in addition, write the default constructor, which initializes the date to January 1, 2000. Write another constructor which takes in a day, month, and year in that order.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
dateclass and addtional constructors.
1#include <iostream>
2#include <vector>
3
4class date {
5 private:
6 int day, month, year;
7 std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
8 public:
9 date () { day = 1; month = 1; year = 2000; }
10 date (int d, int m, int y) { day = d; month = m; year = y; }
11 int get_day () { return day; }
12 int get_month () { return month; }
13 int get_year () { return year; }
14 void set_day (int d) { if (d > 0 && d < 32) day = d; }
15 void set_month (int m) { if (m > 0 && m < 13) month = m; }
16 void set_year (int y) { year = y; }
17};
Question
Let's write the
datemember function,print_date, which prints the date out in the following format: month/day/year CE/BCE depending on whether the year is negative or not.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
print_datemember function.
1#include <iostream>
2#include <vector>
3
4class date {
5 private:
6 int day, month, year;
7 std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
8 public:
9 date () { day = 1; month = 1; year = 2000; }
10 date (int d, int m, int y) { day = d; month = m; year = y; }
11 int get_day () { return day; }
12 int get_month () { return month; }
13 int get_year () { return year; }
14 void set_day (int d) { if (d > 0 && d < 32) day = d; }
15 void set_month (int m) { if (m > 0 && m < 13) month = m; }
16 void set_year (int y) { year = y; }
17public:
18 void print_date();
19};
20
21void date::print_date () {
22 if (get_year() < 0) {
23 std::cout << get_month() << "/" << get_day() << "/" << -get_year() << " BCE" << std::endl;
24 }
25 else {
26 std::cout << get_month() << "/" << get_day() << "/" << get_year() << " CE" << std::endl;
27 }
28}
Question
Write the
datemember functionis_leap_year, which returns true if the year is a leap year. Then write thedatemember functionlast_day_in_month, which returns the last day in thedate's month.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is onne way to write the
is_leap_yearandlast_day_in_monthmember functions.
1#include <iostream>
2#include <vector>
3
4class date {
5 private:
6 int day, month, year;
7 std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
8 public:
9 date () { day = 1; month = 1; year = 2000; }
10 date (int d, int m, int y) { day = d; month = m; year = y; }
11 int get_day () { return day; }
12 int get_month () { return month; }
13 int get_year () { return year; }
14 void set_day (int d) { if (d > 0 && d < 32) day = d; }
15 void set_month (int m) { if (m > 0 && m < 13) month = m; }
16 void set_year (int y) { year = y; }
17public:
18 bool is_leap_year();
19 int last_day_in_month();
20};
21
22bool date::is_leap_year () {
23 if (get_year() % 4 != 0) { return false; }
24 else if (get_year() % 100 != 0) { return true; }
25 else if (get_year() % 400 != 0) { return false; }
26 else { return true; }
27}
28
29int date::last_day_in_month () {
30 if (is_leap_year() && get_month() == 2) {
31 return days_in_month[get_month() - 1] + 1;
32 } else {
33 return days_in_month[get_month() - 1];
34 }
35}