Refer to the example code of the Circle class in Chapter 8.2, design a class nam
ID: 3546674 • Letter: R
Question
Refer to the example code of the Circle class in Chapter 8.2, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and length that specify the width and length of the rectangle. The default values are 0.0 for both width and length. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and length. A method named getArea() that returns the area of this rectangle. A method named getPerimeter() that returns the perimeter. Accessor and Mutator methods for length and width; Make sure that your constructors and mutator methods can check the new length and width to make sure only positive values can be used in your code, otherwise print out error message to the user. Create your own project in Eclips, add a test class with main method as usual first. Then add another new class called Rectangle without the main method in the same project. Now you should have two class files in your project. Implement the Rectangle class first. In your test class main method, creates two Rectangle objects
Explanation / Answer
Dear,
Code:
/**
Rectangle class, phase 5
*/
public class Rectangle
{
private double length;
private double width;
/**
Constructor
@param len The length of the rectangle.
@param w The width of the rectangle.
*/
public Rectangle(double len, double w)
{
length = len;
width = w;
}
/**
The setLength method stores a value in the
length field.
@param len The value to store in length.
*/
public void setLength(double len)
{
length = len;
}
/**
The setWidth method stores a value in the
width field.
@param w The value to store in width.
*/
public void setWidth(double w)
{
width = w;
}
/**
The getLength method returns a Rectangle
object's length.
@return The value in the length field.
*/
public double getLength()
{
return length;
}
/**
The getWidth method returns a Rectangle
object's width.
@return The value in the width field.
*/
public double getWidth()
{
return width;
}
/**
The getArea method returns a Rectangle
object's area.
@return The product of length times width.
*/
public double getArea()
{
return length * width;
}
/**
The getPerimeter method returns a Rectangle
object's perimeter.
@return The perimeter of rectangle.
*/
public double getPerimeter()
{
return 2*(length + width);
}
}
/**
This program demonstrates the Rectangle class's
constructor.
*/
public class ConstructorDemo
{
public static void main(String[] args)
{
// Create a Rectangle object, passing 5.0 and
// 15.0 as arguments to the constructor.
Rectangle box = new Rectangle(5.0, 15.0);
// Display the length.
System.out.println("The box's length is " +
box.getLength());
// Display the width.
System.out.println("The box's width is " +
box.getWidth());
// Display the area.
System.out.println("The box's area is " +
box.getArea());
// Display the perimeter.
System.out.println("The box's perimeter is " +
box.getPerimeter());
}
}