Knowee
Questions
Features
Study Tools

Nandhini is tasked with creating a program to calculate and display the speed of a car based on the distance covered and the time taken.Write a program with two classes: Vehicle as the base class and Car as the derived class, which inherits the properties from the Vehicle class for calculating the speed. The base class fetches the input as a float value, whereas the derived class calculates and prints the output as a float value.Note: Use public inheritanceNote: This kind of question will help in clearing Wipro recruitment.Input format :The input consists of two floating-point numbers separated by a space. The first number represents the distance traveled by the car, and the second number represents the time taken to cover that distance, separated by a space.Output format :The output displays a single line of output, showing the speed of the car in kilometers per hour.Refer to the sample output for the formatting specifications.Code constraints :The input distance and time should be non-negative floating-point numbers.The input time should not be zero (to avoid division by zero).Sample test cases :Input 1 :15.0 5.0Output 1 :Speed of car: 3 km/hrInput 2 :100.23 96.0Output 2 :Speed of car: 1.04406 km/hrNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

Question

Nandhini is tasked with creating a program to calculate and display the speed of a car based on the distance covered and the time taken.Write a program with two classes: Vehicle as the base class and Car as the derived class, which inherits the properties from the Vehicle class for calculating the speed. The base class fetches the input as a float value, whereas the derived class calculates and prints the output as a float value.Note: Use public inheritanceNote: This kind of question will help in clearing Wipro recruitment.Input format :The input consists of two floating-point numbers separated by a space. The first number represents the distance traveled by the car, and the second number represents the time taken to cover that distance, separated by a space.Output format :The output displays a single line of output, showing the speed of the car in kilometers per hour.Refer to the sample output for the formatting specifications.Code constraints :The input distance and time should be non-negative floating-point numbers.The input time should not be zero (to avoid division by zero).Sample test cases :Input 1 :15.0 5.0Output 1 :Speed of car: 3 km/hrInput 2 :100.23 96.0Output 2 :Speed of car: 1.04406 km/hrNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that fulfills the requirements:

class Vehicle:
    def __init__(self, distance, time):
        self.distance = float(distance)
        self.time = float(time)

class Car(Vehicle):
    def __init__(self, distance, time):
        Vehicle.__init__(self, distance, time)

    def calculate_speed(self):
        speed = self.distance / self.time
        return speed

# Input distance and time
distance, time = input().split()
car = Car(distance, time)

# Calculate and print speed
speed = car.calculate_speed()
print("Speed of car: {:.5f} km/hr".format(speed))

This program first defines a base class Vehicle that takes distance and time as inputs and stores them as float values. Then it defines a derived class Car that inherits from Vehicle and adds a method to calculate speed. The speed is calculated by dividing distance by time. The program then takes user input for distance and time, creates a Car object with these values, calculates the speed, and prints it. The speed is printed with 5 decimal places as per the output format in the question.

This problem has been solved

Similar Questions

Problem StatementKaniska is tasked with creating a program that allows users to input information about different types of vehicles and calculate the time it takes for these vehicles to travel a given distance at their maximum speeds. The program will support three types of vehicles: Cars, Bicycles, and Boats.Your task is to implement the Vehicle class as the base class for these three vehicle types. The Vehicle class should have the following properties and functionalities:Properties:numOfWheels (integer): representing the number of wheels of the vehicle.maxSpeed (floating-point): representing the maximum speed of the vehicle.Functionalities:A default constructor to initialize numOfWheels and maxSpeed to 0.A virtual function setNumOfWheels(int wheels) to set the number of wheels of the vehicle.A virtual function setMaxSpeed(float speed) to set the maximum speed of the vehicle.A virtual function getNumOfWheels() to get the number of wheels on the vehicle.A virtual function getMaxSpeed() to get the maximum speed of the vehicle.A virtual function timeToTravel(float distance) that calculates and returns the time it takes for the vehicle to travel the given distance at its maximum speed.Implement three derived classes, Car, Bicycle, and Boat, which inherit from the Vehicle class. Each derived class should override the timeToTravel(float distance) function with the appropriate calculation for the specific vehicle type. The Car and Bicycle classes should calculate the time based on their maximum speed in mph, while the Boat class should consider the maximum speed in knots and convert it to mph using the formula 1 knot = 1.151 mph.In the main function, create instances of each vehicle type (Car, Bicycle, and Boat) and take user input for the number of wheels and maximum speed for each vehicle. Then, the user will input a distance, and the program will display the details of each vehicle and the time it takes for each vehicle to travel the given distance at its maximum speed.Note: This kind of question will help in clearing Capgemini recruitment.Input format :The first line consists of the number of wheels and the maximum speed of the car, separated by a single space.The third line consists of the number of wheels and the maximum speed of the bicycle, separated by a single space.The fifth line consists of the maximum speed of the boat (in knots).The sixth line consists of the distance to travel (in miles).Output format :The program outputs the details of the car, bicycle, and boat, including the number of wheels and the maximum speed for each vehicle, as well as the time it would take to travel the specified distance at maximum speed.The output includes the vehicle type, number of wheels, maximum speed, and time taken to travel the distance.The time taken is displayed in hours.Refer to the sample input and output for format specifications.Code constraints :The number of wheels (numOfWheels) for each vehicle is a positive integer.The maximum speed (maxSpeed) for each vehicle is a positive floating-point number.The distance to travel is a positive floating-point number.The maximum speed of the Boat (in knots) should be converted to mph using the formula: 1 knot = 1.151 mph.Sample test cases :Input 1 :4 602 153050Output 1 :Vehicle Details:Car has 4 wheels and can go up to 60 mph. It would take 0.833333 hours to travel 50 miles at maximum speed.Bicycle has 2 wheels and can go up to 15 mph. It would take 3.33333 hours to travel 50 miles at maximum speed.Boat has 0 wheels and can go up to 30 knots. It would take 1.44802 hours to travel 50 miles at maximum speed.Input 2 :4 902 203575Output 2 :Vehicle Details:Car has 4 wheels and can go up to 90 mph. It would take 0.833333 hours to travel 75 miles at maximum speed.Bicycle has 2 wheels and can go up to 20 mph. It would take 3.75 hours to travel 75 miles at maximum speed.Boat has 0 wheels and can go up to 35 knots. It would take 1.86174 hours to travel 75 miles at maximum speed.Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

Mohit wants a financial calculator program for Fixed Deposits and Simple Interest. He needs a program that uses multi-level inheritance. The program should have three classes:class Investment - Holds the principal, interest rate, and time period as attributes.class FixedDeposit - Derived from the Investment class, calculates the maturity amount using a method called calculateMaturityAmount(). class SimpleInterest - Derived from FixedDeposit class, prints the maturity amount using a method called printFD(). It then computes the simple interest and prints it using a method called calculateSimpleInterest().Formulas used:Maturity Amount = P * (1 + (R/100)) ^ T where the power value is calculated using pow() function from the math library.Simple Interest = P * N * R/100where P - principal, R - interest rate, and N - time period in years.Input format :The first line consists of three double-point numbers separated by a space representing the principal amount, interest rate, and time period in years to calculate Fixed Deposit and Simple Interest.Output format :The first line displays "Maturity Amount: " followed by a double value which is the calculated Maturity amount rounded to two decimal places.The second line displays "Simple Interest: " followed by a double value which is the calculated Simple interest rounded to two decimal places.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ Principal amount ≤ 1060.1 ≤ Interest rate ≤ 100.1 ≤ Time period ≤ 10Sample test cases :Input 1 :1000.0 5.0 2Output 1 :Maturity Amount: 1102.50Simple Interest: 100.00Input 2 :1500.0 4.5 4Output 2 :Maturity Amount: 1788.78Simple Interest: 270.00

Janu is developing a time conversion program that consists of a base class called Time and two derived classes named TimeToSeconds and TimeToMinutes.Time class: - Stores hours and minutes and has a virtual function display that prints the input time.TimeToSeconds class: Overrides the display method to convert the time to seconds and displays it.TimeToMinutes class: Overrides the display method to convert the time to minutes and displays it.To assist Janu, we need to create a program that accepts input for hours and minutes, creates instances of the derived classes, and displays the original time, the time in seconds, and the time in minutes.

Write a program to demonstrate multiple inheritance. Consider two base classes worker(int code, char name, float salary), HR who calculates the different allowances like (float DA, HRA). You have another class manger with calculates (float TA(is 10% of salary), gross salary) where the properties of worker and HR is used in this class. Write a test application class to create an array of 10 workers and display their details with gross salary.

Dinesh is working in a supermarket and he is developing a program to calculate the cost of different types of items. Help him write the program that does the following:a) Create a base class, ItemType, with one virtual function double calculateAmount()b) Create a class called wooden that extends ItemType class with a number of items and cost as its private attributes. Obtain the data members and override the virtual function and calculate the total amount.c) Create a class called electronics that extends ItemType class with cost as its private attribute. Obtain the data member and override the virtual function and calculate the amount with 20% discount.Note: This question helps in clearing Infosys tests.Input format :The first line consists of an integer choice (1 or 2) representing the choice of item type.If the choice is 1 (wooden items), the next line consists of two space-separated integers: noOfItems and cost, representing the number of wooden items and their individual cost, respectively.If the choice is 2 (electronics), the next line consists of a single floating-point number cost, representing the cost of the electronic item.Output format :The output prints a floating-point number representing the calculated total cost of the chosen item type rounded off to two decimal places.Code constraints :10 < cost < 1060 < noOfItems < 20Sample test cases :Input 1 :15 840.5Output 1 :4202.50Input 2 :21800.56Output 2 :1440.45Note :

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.