← Back

Code Challenge 5.1 - Rectangle Class


Objective

Your task is to design and implement a Rectangle class and write a program that tests the class. This challenge will focus on encapsulating object-oriented programming concepts such as constructors, methods, and fields.

Skills to Practice

Challenge Tasks

  1. Create a console project named Challenge 5-1.
  2. Draw the UML Diagram: -Draw the UML diagram for the Rectangle class, including fields, constructors, and methods.
  3. Class Implementation:
    • Define a class named Rectangle with the following specifications:
      • Two double data fields named width and height with default values of 1. A no-arg constructor that creates a default rectangle.
      • A constructor that creates a rectangle with specified width and height.
      • A method named GetArea()* that returns the area of the rectangle.
      • A method named GetPerimeter()* that returns the perimeter of the rectangle.
  4. Test Program:
    • Write a test program that creates two Rectangle objects:
      • One with a width of 10 and height of 50.
      • Another with a width of 12.5 and height of 25.8.
      • Display the width, height, area, and perimeter of each rectangle in this order.

* Use the naming convention matches your programming language/preference.