[Practice] Java Inheritance


Submit solution

Points: 10
Time limit: 0.5s
Memory limit: 29M

Author:
Problem type
Allowed languages
Java

Problem Statement

  • Write a Java Class named Vehicle with functions start and stop. This Class has a variable run_status. This is modified by the start and stop functions.

  • Write two other classes named Car and Bike which inherit the Vehicle Class.

  • Define the constructor for the Car and Bike Classes. Car Constructor takes the Model Name, Year, and Num of Wheels. Bike Constructor takes the Brand Name, Year, and Num of Gears.

  • The Car class has a function called drive, which takes a parameter gear position.

  • The Bike class has a function called pedal, which takes a parameter pedal speed.

  • In the main function, Instantiate Car and Bike classes. Then start, drive and stop the Car. Similarly start, pedal and stop the Bike.

  • If the Vehicle is not running (run_status is false), then drive and pedal should return error.

Test Data

Sample Input 1

No Input

Sample Output 1
Car Instantiated with Parameter Jaguar XF, 2022, 4 
[Vehicle] started.
Driving the car in gear position: 3
[Vehicle] stopped.
Bike Instantiated with Parameter Giant, 2021, 18
[Vehicle] started.
Pedaling the bike at speed: 10
[Vehicle] stopped.

Comments

There are no comments at the moment.