THINK FIRST·CODE LATER

← All labs

Rectangle: a first class with accessors

Problem

Write a class Rectangle with two private instance variables width and height (double), a constructor taking both, accessors getWidth() and getHeight(), a mutator setWidth(double) that ignores values <= 0, and two methods area() and perimeter().

Write a separate class RectangleApp whose main reads width and height from the keyboard, creates one Rectangle and prints:

Area: 12.00
Perimeter: 14.00

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.