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. circle should have its radius stored in a private member variable. Also write the constructor for circle, which takes a radius as a parameter, in addition to the public member function calculate_area, which returns the area of the circle. Make sure to include the private and public keywords! Use 3.14 for the value of pi.

Example c192_mucp_14_1_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to wrte the class definition and constructor for circle.

Example c192_mucp_14_11_ac_a
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 circle class, let's write some accessor functions! Write the circle member functions get_radius and set_radius. It doesn't make sense for a circle's radius to be negative, so in your set_radius function, output an error message if the given radius is negative.

Example c192_mucp_14_2_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the accessor functions for get_radius and set_radius.

Example c192_mucp_14_2_ac_a
 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. in main, create a circle with radius 2.4 and output the radius. Then change the radius to 3.6 and output

Example c192_mucp_14_3_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the code.

Example c192_mucp_14_3_ac_a
 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 rectangle can be constructed given only two points. First, write the class definition for point, 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, and set_y.

Example c192_mucp_14_4_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the code.

Example c192_mucp_14_4_ac_a
 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 point class, we can go back to writing the rectangle class. rectangle should 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.

Example c192_mucp_14_5_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the rectangle class.

Example c192_mucp_14_5_ac_a
 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 rectangle member function calculate_sides, which finds the length and height of the rectangle using the stored point``s. Afterwards, write the ``rectangle member function calculate_area, which returns the area of the rectangle.

Example c192_mucp_14_6_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the calculate_sides and calculate_area member functions.

Example c192_mucp_14_6_ac_a
 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 main in main, create a rectangle with 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.

Example c192_mucp_14_7_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to create this rectangle.

Example c192_mucp_14_7_ac_a
 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 date class. date stores information about the day, month, and year in private variables, in addition to a vector of 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.

Example c192_mucp_14_8_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the date class and addtional constructors.

Example c192_mucp_14_8_ac_a
 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 date member 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.

Example c192_mucp_14_9_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the print_date member function.

Example c192_mucp_14_9_ac_a
 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 date member function is_leap_year, which returns true if the year is a leap year. Then write the date member function last_day_in_month, which returns the last day in the date's month.

Example c192_mucp_14_10_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is onne way to write the is_leap_year and last_day_in_month member functions.

Example c192_mucp_14_10_ac_a
 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}