014-003-008

Constructor Chaining Implementation

Medium

Problem Description

constructor Chaining Implementation

In this problem, you will create a program that defines Shape and Rectangle classes, chains their constructors using this() and super(), and displays results using values read from standard input.

Learning Objective: Understand constructor chaining using this() and super() together

Overview

Implement constructor chaining using this() and super() in a class with multiple constructors.

Specifications

  • Define constructor in Shape class that takes String color
  • Rectangle class has int width, int height
  • Rectangle has 2 constructors: (width, height, color) and (width, height) (default color "White")
  • Use this() to chain between constructors
  • Read values from standard input to create Rectangle instances

Input Format

width1 height1 color1
width2 height2
  • Line 1: width, height, and color of the first Rectangle (space-separated)
  • Line 2: width and height of the second Rectangle (default color "White" is used)

Output Format

Rectangle: width1xheight1, Color: color1
Rectangle: width2xheight2, Color: White

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