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
- Define
String typefield and constructor inVehicleclass CarextendsVehicle, addsint doorsfieldCar(String type, int doors): Calls super(type) and sets doorsCar(String type): Delegates to this(type, 4) for default 4 doorsshow()method prints<type> with <doors> doors- 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