rectangle class(driver included)

hey everyone,
i cannot for the life of me figure this out, so if somone would do it for me so i can better grasp the concept, i would be very thankful.

Write a Rectangle class that represents a rectangle object which takes in its constructor two parameters representing the length and width of the rectangle. Write setter and getter methods for both the length and width, ensuring that the values of each is not negative and handles negative values appropriately. Write two getter methods named perimeter() and area() that return the perimeter and the area of the object; the perimeter is the sum of all the sides (e.g., l + w + l + w) and the area is the product of the length and the width (e.g., l * w).

driver code:

public class Rectangletest {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(-1, 4);
System.out.printf("The rectangle has sides of %.02f and %.02f
", rectangle.getLength(), rectangle.getWidth());
System.out.printf("Setting the length to 5
");
rectangle.setLength(5);
System.out.printf("The rectangle has sides of %.02f and %.02f
", rectangle.getLength(), rectangle.getWidth());
System.out.printf("The rectangle's perimeter is %.02f, and its area is %.02f
", rectangle.getPerimeter(), rectangle.getArea());
}
}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories