Implement a class named Rectangle that contains following private members:
Two double data fields that specify the width and height of the rectangle. A string data field, named color, that specifies the color of a rectangle.
You should provide a no-argument constructor that creates a default rectangle having width and
height of 2 units and color set to black. You should also provide an overloaded constructor that
creates a rectangle with the specified width and height. In addition you should provide methods
to only set the color of the rectangle. Further, you should provide a member function named
getArea() that returns the area of this rectangle and a function named getPerimeter() that returns
the perimeter.
Write a test program that creates two Rectangle objects. Assign width 10 and height 20 to the
first object and width 34.5 and height 15.9 to the second object. Assign color red to all
Rectangle objects and find their areas and perimeters.
public class Rectangle {
private double width;
private double height;
// Default Constructor
public Rectangle() {
}
// Constructor
public Rectangle(double width, double height) {
}
// Calculate area
public double getArea() {
}
// Calculate perimeter
public double getPerimeter() {
}
}
Comments
Leave a comment