Car Class

Easy
0/40
71 upvotes
Asked in company
TCS

Problem statement

Design a class Car having parameterized constructor that takes two arguments as an input i.e noOfGear and color and a printCarInfo method that prints the CarInfo i.e noOfGear and color.

Design another class RaceCar having parameterized constructor has an additional attribute maxSpeed and printRaceCarInfo method that prints the RaceCarInfo i.e noOfGear, color and maxSpeed.

Note: You have to create an object of class RaceCar and call the printRaceCarInfo method.

Detailed explanation ( Input/output format, Notes, Images )

Input Format:

The first line of input contains a single integer representing noOfGear.

The second line of input contains a string without any preceding and trailing spaces, denoting the color of the car.

The third line of input contains a single Integer representing maxSpeed.

Output Format:

The first line of output prints the number of gears in the car
The second line of output prints the color of the car.
The third line of output prints the maximum speed of the car.
Sample Input 1:
5
red
1000
Sample Output 1:
noOfGear: 5
color: red
maxSpeed: 1000
Explanation of Sample output 1:
When we call the printInfo function, all the info related to the car will be printed the same as the above format.
Approaches (1)
Intuitive Approach
  • Here we create the class Car, and an another class raceCar that is inheriting the property of Car and has an additional attribute maxSpeed.
  • Now we create an object raceCar of class RaceCar and call the printRaceCarInfo method.
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Car Class
Full screen
Console