This lab is designed to help you practice creating custom classes, defining operators, and writing functions in R. Follow the instructions and complete the tasks below.
- Define a custom S3 class called "Circle" that represents a circle. The class should store the radius of the circle.
- Write a constructor function Circle() that takes the radius as input and assigns the class "Circle" to the object.
- Write a print.Circle() method to display the radius and area of the circle when the object is printed. Use the formula for the area of a circle: area = π * radius^2.
Example:
my_circle <- Circle(5)
print(my_circle)
Output:
Type: "Circle object"
Radius: 5
Area: 78.53982
-
Define a custom operator %>% that calculates the distance between the centers of two circles and checks if they intersect. The operator should return TRUE if the circles intersect and FALSE otherwise. The formula for the distance between two circles is: distance = sqrt((x2 - x1)^2 + (y2 - y1)^2).
- Assume that the circles are centered at (x1, y1) and (x2, y2), respectively.
- Two circles intersect if the distance between their centers is less than or equal to the sum of their radii.
-
Modify the Circle class to include x and y coordinates for the center of the circle.
Example:
circle1 <- Circle(radius = 3, x = 0, y = 0) circle2 <- Circle(radius = 4, x = 5, y = 0) circle1 %> circle2
Output: TRUE
-
Write a function circumference() that calculates the circumference of a Circle object. The formula for the circumference is: circumference = 2 * π * radius.
-
Add this function as a method for the Circle class so that it can be called directly on objects of the class.
Example:
my_circle <- Circle(radius = 5) circumference(my_circle)
Output: 31.41593
-
Define a custom S4 class called "Rectangle" that represents a rectangle. The class should store the length and width of the rectangle.
-
Write a constructor function Rectangle() that takes the length and width as input and creates an object of the "Rectangle" class.
-
Write a show() method for the Rectangle class to display the length, width, and area of the rectangle when the object is printed.
Example:
my_rectangle <- Rectangle(length = 4, width = 6) show(my_rectangle)
Output:
Type: "Rectangle object"
Length: 4
Width: 6
Area: 24
-
Define a custom operator %==% that compares two Rectangle objects and checks if they have the same area.
-
The operator should return TRUE if the areas are equal and FALSE otherwise.
Example:
rectangle1 <- Rectangle(length = 4, width = 6) rectangle2 <- Rectangle(length = 3, width = 8) rectangle1 %==% rectangle2
Output: TRUE
- Submitted notebook (or file) with your responses to each of the exercises.
- Upon completion, add your deliverables to git.
- Then commit git and push your branch to the remote.
- Make a pull request and paste the PR link in the submission field in the Student Portal.
Good luck!