14.17. Mixed Up Code Practice

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. Put the necessary blocks of code in the correct order.

  1.       double calculate_area () { return 3.14 * radius * radius; }
    };
  2. circle (double r) { radius = r; }
  3. circle (int r) { radius = r; }  #distractor
  4. class circle {
  5. double radius;
  6. private {  #distractor
  7. private:
  8. public:
  9. struct circle {  #distractor
  10. }  #distractor

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. Put the necessary blocks of code in the correct order.

  1. class circle {
       private:
          double radius;
       public:
          circle (double r) { radius = r; }
          double calculate_area () { return 3.14 * radius * radius; }
  2. double get_radius () {
  3. double set_radius (double r) {  #distractor
  4. else { radius = r; }
  5. if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << std::endl; }
  6. return radius;
  7. void get_radius () {  #distractor
  8. void set_radius (double r) {
  9. }
  10. }
  11. };

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 the new radius. Put the necessary blocks of code in the correct order.

  1. c.radius = 2.4;  #distractor
  2. c.radius = 3.6;  #distractor
  3. circle c(2.4);
  4. circle c;  #distractor
  5. int main() {
  6. s.set_radius (3.6);
  7. std::cout << "New radius: " << c.get_radius () << std::endl;
  8. std::cout << "New radius: " << c.radius << std::endl;  #distractor
  9. std::cout << "Radius: " << c.get_radius () << std::endl;
  10. std::cout << "Radius: " << c.radius << std::endl;  #distractor
  11. }

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. Put the necessary blocks of code in the correct order.

  1. class point {
  2. double get_x () { return x; }
  3. double get_y () { return y; }
  4. double x, y;
  5. point () { x = 0; y = 0; }
  6. point (double x_val, double y_val) { x = x_val; y = y_val; }
  7. private:
  8. public:
  9. void set_x (double x_val) { x = x_val; }
  10. void set_y (double y_val) { y = y_val; }
  11. };

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.

  1. class rectangle {
  2. double length, height;
  3. point get_lower_right () { return lower_right; }
  4. point get_upper_left () { return upper_left; }
  5. point upper_left, lower_right;
  6. private:
  7. public:
  8. rectangle (point up_left, point low_right) { upper_left = up_left; lower_right = low_right; }
  9. void set_lower_right (point p) { lower_right = p; }
  10. void set_upper_left (point p) { upper_left = p; }
  11. };

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.

  1. double rectangle::calculate_area () {
  2. double rectangle::calculate_sides () {
  3. height = get_upper_left().get_y() - get_lower_right().get_y();
  4. height = upper_left.y - lower_right.y;  #distractor
  5. length = get_lower_right().get_x() - get_upper_left().get_x();
  6. length = lower_right.x - upper_left.x;  #distractor
  7. return get_length() * get_height();  #distractor
  8. return height;  #distractor
  9. return length * height;
  10. return length;  #distractor
  11. void rectangle::calculate_sides () {
  12. }
  13. }

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.

  1. int main() {
  2. point p1(2.5, 7.5);
  3. point p2(8, 1.5);
  4. point p3(4.2, 10.7);
  5. r.calculate_sides();
  6. r.calculate_sides();
  7. r.set_upper_left(p3);
  8. r.upper_left = p3;  #distractor
  9. rectangle r(p1, p2);
  10. rectangle r(p2, p1);  #distractor
  11. std::cout << "Area: " << r.calculate_area() << std::endl;
  12. std::cout << "Length: " << r.get_length() << ", Height: " << r.get_height() << std::endl;  #distractor
  13. std::cout << "Length: " << r.length << ", Height: " << r.height << std::endl;
  14. std::cout << "New area: " << r.calculate_area() << std::endl;
  15. }

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.

  1. class date {
  2. date () { day = 0; month = 0; year = 0; }  #distractor
  3. date () { day = 1; month = 1; year = 2000; }
  4. date (int d, int m, int y) { day = d; month = m; year = y; }
  5. int day, month, year;
  6. int get_day () { return day; }
  7. int get_month () { return month; }
  8. int get_year () { return year; }
  9. private:
  10. public:
  11. std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  12. void set_day (int d) { if (d > 0 && d < 32) day = d; }
  13. void set_day (int d) { if (d > 1 && d < 31) day = d; }  #distractor
  14. void set_month (int m) { if (m > 0 && m < 13) month = m; }
  15. void set_month (int m) { if (m >= 0 && m <= 12) month = m; }  #distractor
  16. void set_year (int y) { if (y >= 2000 && y < 3000) year = y; }  #distractor
  17. void set_year (int y) { year = y; }
  18. };

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.

  1. else {
  2. if (get_year() < 0) {
  3. std::cout << get_month() << "/" << get_day() << "/" << -get_year() << " BCE" << std::endl;
  4. std::cout << get_month() << "/" << get_day() << "/" << get_year() << " BCE" << std::endl;  #distractor
  5. std::cout << get_month() << "/" << get_day() << "/" << get_year() << " CE" << std::endl;
  6. std::cout << month << "/" << day << "/" << year << " BCE" << std::endl;  #distractor
  7. void date::print_date () {
  8. }
  9. }
  10. }

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.

  1. bool date::is_leap_year () {
  2. else if (get_year() % 100 != 0) { return true; }
  3. else if (get_year() % 400 != 0) { return false; }
  4. else {
  5. else { return false; }  #distractor
  6. else { return true; }
  7. if (get_year() % 4 != 0) { return false; }
  8. if (get_year() % 4 == 0) { return false; }  #distractor
  9. if (is_leap_year() && get_month() == 2) {
  10. if (is_leap_year()) {  #distractor
  11. int date::last_day_in_month () {
  12. return days_in_month[get_month() - 1] + 1;
  13. return days_in_month[get_month() - 1];
  14. return days_in_month[get_month()];  #distractor
  15. }
  16. }
  17. }