Activecode Exercises

Answer the following Activecode questions to assess what you have learned in this chapter.

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 calculateArea, which returns the area of the Circle. Make sure to include the private and public keywords! Use 3.14 for the value of pi.

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

Now that we have our Circle class, let’s write some accessor functions! Write the Circle member functions getRadius and setRadius. It doesn’t make sense for a Circle’s radius to be negative, so in your setRadius function, output an error message if the given radius is negative.

Below is one way to write the accessor functions for getRadius and setRadius.

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

Below is one way to write the code.

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 xVal and yVal. In addition, write its accessor functions, getX, getY, setX, and setY.

Below is one way to write the code.

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.

Below is one way to write the Rectangle class.

Write the Rectangle member function calculateSides, which finds the length and height of the rectangle using the stored Point``s. Afterwards, write the ``Rectangle member function calculateArea, which returns the area of the rectangle.

Below is one way to write the calculateSides and calculateArea member functions.

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 upperLeft corner to be at (4.2, 10.7) and print out the new area.

Below is one way to create this Rectangle.

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.

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

Let’s write the Date member function, printDate, which prints the date out in the following format: month/day/year CE/BCE depending on whether the year is negative or not.

Below is one way to write the printDate member function.

Write the Date member function isLeapYear, which returns true if the year is a leap year. Then write the Date member function lastDayInMonth, which returns the last day in the Date’s month.

Below is onne way to write the isLeapYear and lastDayInMonth member functions.

You have attempted of activities on this page