007-001-012

Class Creation: Calculation Method

Hard

Problem Description

Class Creation: Calculation Method

In this problem, you will create a Rectangle class and implement calculateArea() and calculatePerimeter() methods that compute and return area and perimeter from field values.

Learning Objective: Create methods that perform calculations using field values

Overview

Create a class representing a rectangle and implement methods to calculate area and perimeter.

Specifications

  • Create a Rectangle class
  • Define int fields width and height
  • Initialize via constructor
  • Create calculateArea() method returning area (width x height)
  • Create calculatePerimeter() method returning perimeter (2 x (width + height))

Usage Example

Rectangle rect = new Rectangle(5, 3);
System.out.println(rect.calculateArea());      // 15
System.out.println(rect.calculatePerimeter()); // 16

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up