← Unit 3 · all exercises
Saved in this browser only

A rectangle that knows its own area

Write a Rectangle class storing a width and a height. The constructor takes both. area returns width times height, and perimeter returns twice the width plus twice the height.

class Rectangle { Rectangle(int w, int h); int area(); int perimeter(); }

Rectangle.java
Loading editor…
Results

Examples

  • new Rectangle(3, 4).area() → 12
  • new Rectangle(3, 4).perimeter() → 14

There are also 3 hidden tests you can't see. Solve the problem, not the examples.