Knowee
Questions
Features
Study Tools

2. Python: String Representations of ObjectsImplement two vehicle classes: Car:The constructor for Car must take two arguments. The first of them is its maximum speed, and the second one is a string that denotes the units in which the speed is given: either "km/h" or "mph".The class must be implemented to return a string based on the arguments. For example, if car is an object of class Car with a maximum speed of 120, and the unit is "km/h", then printing car prints the following string: "Car with the maximum speed of 120 km/h", without quotes. If the maximum speed is 94 and the unit is "mph", then printing car prints in the following string: "Car with the maximum speed of 94 mph", without quotes. Boat:The constructor for Boat must take a single argument denoting its maximum speed in knots.The class must be implemented to return a string based on the argument. For example, if boat is an object of class Boat with a maximum speed of 82, then printing boat prints the following string: "Boat with the maximum speed of 82 knots", without quotes. The implementations of the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes.  It then prints the string representation of the object to the standard output. Constraints1 ≤ the number of queries in one test file ≤ 100 Input Format Format for Custom TestingIn the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the vehicle type to be constructed, and the remaining parameters denote the values passed for the constructor of the object.Sample Case 0Sample InputSTDIN      Function-----      -------2 → number of queries, q = 2car 151 km/h → query parameters = ["car 151 km/h", "boat 77"]boat 77Sample OutputCar with the maximum speed of 151 km/hBoat with the maximum speed of 77 knotsExplanationThere are 2 queries. In the first of them, an object of class Car with the maximum speed of 151 in km/h is constructed, and then its string representation is printed to the output. In the second query, an object of class Boat is constructed with the maximum speed of 77 knots, and then its string representation is printed to the output.Sample Case 1Sample InputSTDIN      Function-----      --------3 → number of queries, q = 2boat 101 → query parameters = ["boat 101", "car 120 mph", "car 251 km/h"]car 120 mphcar 251 km/hSample OutputBoat with the maximum speed of 101 knotsCar with the maximum speed of 120 mphCar with the maximum speed of 251 km/hExplanationThere are 3 queries. In the first of them, an object of class Boat with the maximum speed of 101 knots is constructed, and then its string representation is printed to the output. In the second query, an object of class Car with the maximum speed of 120 in mph is constructed, and then its string representation is printed to the output. In the third query, an object of class Car with the maximum speed of 251 in km/h is constructed, and then its string representation is printed to the output.

Question

  1. Python: String Representations of ObjectsImplement two vehicle classes: Car:The constructor for Car must take two arguments. The first of them is its maximum speed, and the second one is a string that denotes the units in which the speed is given: either "km/h" or "mph".The class must be implemented to return a string based on the arguments. For example, if car is an object of class Car with a maximum speed of 120, and the unit is "km/h", then printing car prints the following string: "Car with the maximum speed of 120 km/h", without quotes. If the maximum speed is 94 and the unit is "mph", then printing car prints in the following string: "Car with the maximum speed of 94 mph", without quotes. Boat:The constructor for Boat must take a single argument denoting its maximum speed in knots.The class must be implemented to return a string based on the argument. For example, if boat is an object of class Boat with a maximum speed of 82, then printing boat prints the following string: "Boat with the maximum speed of 82 knots", without quotes. The implementations of the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes.  It then prints the string representation of the object to the standard output. Constraints1 ≤ the number of queries in one test file ≤ 100 Input Format Format for Custom TestingIn the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the vehicle type to be constructed, and the remaining parameters denote the values passed for the constructor of the object.Sample Case 0Sample InputSTDIN      Function-----      -------2 → number of queries, q = 2car 151 km/h → query parameters = ["car 151 km/h", "boat 77"]boat 77Sample OutputCar with the maximum speed of 151 km/hBoat with the maximum speed of 77 knotsExplanationThere are 2 queries. In the first of them, an object of class Car with the maximum speed of 151 in km/h is constructed, and then its string representation is printed to the output. In the second query, an object of class Boat is constructed with the maximum speed of 77 knots, and then its string representation is printed to the output.Sample Case 1Sample InputSTDIN      Function-----      --------3 → number of queries, q = 2boat 101 → query parameters = ["boat 101", "car 120 mph", "car 251 km/h"]car 120 mphcar 251 km/hSample OutputBoat with the maximum speed of 101 knotsCar with the maximum speed of 120 mphCar with the maximum speed of 251 km/hExplanationThere are 3 queries. In the first of them, an object of class Boat with the maximum speed of 101 knots is constructed, and then its string representation is printed to the output. In the second query, an object of class Car with the maximum speed of 120 in mph is constructed, and then its string representation is printed to the output. In the third query, an object of class Car with the maximum speed of 251 in km/h is constructed, and then its string representation is printed to the output.
...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution to implement the Car and Boat classes in Python:

Step 1: Define the Car class with a constructor that takes two parameters: maximum speed and unit of speed. The constructor should initialize these values.

Step 2: Define the __str__ method for the Car class. This method should return a string that describes the car in the required format.

Step 3: Define the Boat class with a constructor that takes one parameter: maximum speed. The constructor should initialize this value.

Step 4: Define the __str__ method for the Boat class. This method should return a string that describes the boat in the required format.

Here is the Python code that implements these steps:

class Car:
    def __init__(self, max_speed, speed_unit):
        self.max_speed = max_speed
        self.speed_unit = speed_unit

    def __str__(self):
        return f"Car with the maximum speed of {self.max_speed} {self.speed_unit}"

class Boat:
    def __init__(self, max_speed):
        self.max_speed = max_speed

    def __str__(self):
        return f"Boat with the maximum speed of {self.max_speed} knots"

You can use these classes like this:

car = Car(120, "km/h")
print(car)  # prints: Car with the maximum speed of 120 km/h

boat = Boat(82)
print(boat)  # prints: Boat with the maximum speed of 82 knots

This code defines two classes, Car and Boat, each with a constructor and a __str__ method. The __str__ method is a special method in Python that returns a string representation of an object. When you print an object, Python calls this method to determine what to print.

This problem has been solved

Similar Questions

Implement two vehicle classes: Car:The constructor for Car must take two arguments. The first of them is its maximum speed, and the second one is a string that denotes the units in which the speed is given: either "km/h" or "mph".The class must be implemented to return a string based on the arguments. For example, if car is an object of class Car with a maximum speed of 120, and the unit is "km/h", then printing car prints the following string: "Car with the maximum speed of 120 km/h", without quotes. If the maximum speed is 94 and the unit is "mph", then printing car prints in the following string: "Car with the maximum speed of 94 mph", without quotes. Boat:The constructor for Boat must take a single argument denoting its maximum speed in knots.The class must be implemented to return a string based on the argument. For example, if boat is an object of class Boat with a maximum speed of 82, then printing boat prints the following string: "Boat with the maximum speed of 82 knots", without quotes. The implementations of the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes.  It then prints the string representation of the object to the standard output. Constraints1 ≤ the number of queries in one test file ≤ 100 Input Format Format for Custom TestingIn the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the vehicle type to be constructed, and the remaining parameters denote the values passed for the constructor of the object.Sample Case 0Sample InputSTDIN      Function-----      -------2 → number of queries, q = 2car 151 km/h → query parameters = ["car 151 km/h", "boat 77"]boat 77Sample OutputCar with the maximum speed of 151 km/hBoat with the maximum speed of 77 knotsExplanationThere are 2 queries. In the first of them, an object of class Car with the maximum speed of 151 in km/h is constructed, and then its string representation is printed to the output. In the second query, an object of class Boat is constructed with the maximum speed of 77 knots, and then its string representation is printed to the output.Sample Case 1Sample InputSTDIN      Function-----      --------3 → number of queries, q = 2boat 101 → query parameters = ["boat 101", "car 120 mph", "car 251 km/h"]car 120 mphcar 251 km/hSample OutputBoat with the maximum speed of 101 knotsCar with the maximum speed of 120 mphCar with the maximum speed of 251 km/hExplanationThere are 3 queries. In the first of them, an object of class Boat with the maximum speed of 101 knots is constructed, and then its string representation is printed to the output. In the second query, an object of class Car with the maximum speed of 120 in mph is constructed, and then its string representation is printed to the output. In the third query, an object of class Car with the maximum speed of 251 in km/h is constructed, and then its string representation is printed to the output.

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.

Problem 2: Classes and Objects Scenario: Write a Python program to implement the concept of classes, methods, and objects. Instructions:  Create a Python Class named “Vehicle.”  This Class will have a total of five (5) attributes and one (1) method, as follows: o Attributes: type, total_seats, color, price, mileage o Method: print_detail()  If the total number of seats in a vehicle is more than seven (7) and the price is more than $50,000, print “It is an unaffordable vehicle.” Otherwise, print “You may afford and easily park it.”  Create the following four (4) vehicle objects: bus, bike, car, and pickup_truck.  For each object, take the attribute value from the user. This means the user will enter all five (5) attribute values for each object.  Assign these values to the related object and print all the details one by one.  In your program logic, you must use the concept of classes, methods, and objects, as well as accessing methods through objects.  Save your Python file as: Problem2_StudentNameInitials

Alice is going for a drive. During her drive, she reached a maximum speed of 𝑆S km/hr.As per the rules of the government, the speed of the vehicle must not exceed 4040 km/hr, otherwise the person will be fined.You need to tell whether Alice will be fined or not.Input FormatThe only line of input will contain a single integer 𝑆S - denoting the maximum speed Alice reached while driving.Output FormatPrint YES if Alice will be fined , otherwise print NO.You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).Constraints1≤𝑆≤501≤S≤50Sample 1:InputOutput40NOExplanation:Since Alice's speed does not exceed 4040, she will not be fined.Sample 2:InputOutput41YESExplanation:Since Alice's speed exceeds 4040, she will be fined.

In a car-rental showroom, every car is identified by a car-ID. There are two categories of cars in the showroom and they are labelled XX and YY. The mileage of car XX is 15 kmpl and YY is 20 kmpl. Write a Java program that obtains the monthly fuel consumption for a specified number of cars  (user-defined) and determines which car has travelled the longest distance. Create a class Car and perform the above task using array of objects.Input form :Enter number of cars: 3Enter car-ID: 121Enter car label: XXEnter car’s monthly fuel consumption in litres: 10Enter car-ID: 122Enter car label: YYEnter car’s monthly fuel consumption in litres: 10Enter car-ID: 123Enter car label: YYEnter car’s monthly fuel consumption in litres: 5Output form :The car-ID of the car that has travelled longest distance is: 122The car label of the car that has travelled longest distance is YYDistance travelled: 200

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.