011-003-009

Calling Constructors with this()

Medium

Problem Description

Calling Constructors with this()

In this problem, you will create a program that implements a Rectangle class where the default constructor delegates to the parameterized constructor using this(), and displays rectangle information for both default and input-specified sizes to standard output.

Learning Objective: Understand how to call another constructor using this()

Overview

Implement constructor overloading and chaining with this().

Input

width
height
  • Line 1: width of the custom rectangle (positive integer)
  • Line 2: height of the custom rectangle (positive integer)

Specifications

  • Define two constructors in Rectangle class
  • Rectangle() : initialize width and height to 10
  • Rectangle(int width, int height) : initialize with specified values
  • Default constructor calls argument constructor with this()
  • Read width and height from Scanner to create the custom rectangle

Output Format

Default: 10 x 10
Custom: [width] x [height]

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