(CO 1) Write a Rectangle class that has the following member variables: • length
ID: 3713194 • Letter: #
Question
(CO 1) Write a Rectangle class that has the following member variables:
• length
• width
The class should have the following member functions:
• Default constructor that sets length and width to 0.0
• Constructor that accepts the length and width as arguments
• setLength, a mutator function for the length variable
• getLength, an accessor function for the length variable
• setWidth, a mutator function for the width variable
• getWidth, an accessor function for the width variable
• calculateArea, returns area, which is calculated as area = length × width
• calculatePerimeter, returns perimeter, which is calculated as perimeter = 2 × length + 2 × width
Explanation / Answer
public class Rectangle { private double length; private double width; public Rectangle() { this.length = 0; this.width = 0; } public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double calculateArea() { return length * width; } public double calculatePerimeter() { return 2*(length+width); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.