014-002-009

Initialization with Constructor Chaining

Medium

Problem Description

Initialization with constructor Chaining

In this problem, you will create a program that initializes a Car class extending Vehicle using constructor chaining with super() and this(), reading vehicle data from standard input and displaying the result.

Learning Objective: Understand constructor chaining with super() and this()

Overview

Create a Car class extending Vehicle, using this and super for constructor chaining. Read type and door count from Scanner to verify both constructors work correctly.

Specifications

  1. Define String type field and constructor in Vehicle class
  2. Car extends Vehicle, adds int doors field
  3. Car(String type, int doors): Calls super(type) and sets doors
  4. Car(String type): Delegates to this(type, 4) for default 4 doors
  5. show() method prints <type> with <doors> doors
  6. In main, read input using Scanner, create Cars with both constructors and call show()

Input Format

Line 1: Type of the first car (String)
Line 2: Type of the second car (String)
Line 3: Number of doors for the second car (int)

Output Format

<type1> with 4 doors
<type2> with <doors2> doors

Example

Input:

Sedan
SUV
5

Output:

Sedan with 4 doors
SUV with 5 doors

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