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
Question
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
Solution
Sure, here is a step-by-step solution to the problem:
Step 1: Define the class Vehicle. This class will have five attributes: type, total_seats, color, price, mileage and one method: print_detail().
class Vehicle:
def __init__(self, type, total_seats, color, price, mileage):
self.type = type
self.total_seats = total_seats
self.color = color
self.price = price
self.mileage = mileage
Step 2: Define the method print_detail() inside the class Vehicle. This method will check if the total number of seats in a vehicle is more than seven and the price is more than $50,000. If both conditions are met, it will print “It is an unaffordable vehicle.” Otherwise, it will print “You may afford and easily park it.”
def print_detail(self):
if self.total_seats > 7 and self.price > 50000:
print("It is an unaffordable vehicle.")
else:
print("You may afford and easily park it.")
Step 3: Create four objects of the class Vehicle: bus, bike, car, and pickup_truck. For each object, take the attribute value from the user.
bus = Vehicle(input("Enter bus type: "), int(input("Enter total seats in bus: ")), input("Enter bus color: "), int(input("Enter bus price: ")), int(input("Enter bus mileage: ")))
bike = Vehicle(input("Enter bike type: "), int(input("Enter total seats in bike: ")), input("Enter bike color: "), int(input("Enter bike price: ")), int(input("Enter bike mileage: ")))
car = Vehicle(input("Enter car type: "), int(input("Enter total seats in car: ")), input("Enter car color: "), int(input("Enter car price: ")), int(input("Enter car mileage: ")))
pickup_truck = Vehicle(input("Enter pickup truck type: "), int(input("Enter total seats in pickup truck: ")), input("Enter pickup truck color: "), int(input("Enter pickup truck price: ")), int(input("Enter pickup truck mileage: ")))
Step 4: Call the method print_detail() for each object to print the details.
bus.print_detail()
bike.print_detail()
car.print_detail()
pickup_truck.print_detail()
This is the complete Python program that implements the concept of classes, methods, and objects.
Similar Questions
Write the codeCreate a Python program to model vehicles, specifically two types: Car and Truck.Begin by defining a base class Vehicle with attributes make, model, and price. Subsequently, create two derived classes, Car and Truck, both of which inherit from the Vehicle class. Each derived class should introduce its unique attributes, and:Implement a method display_info in the base class Vehicle to print the common attributes (make, model, price).Override the display_info method in each derived class (Car and Truck) to print the make, model, price along with the unique attributes (num_doors for Car and max_load for Truck) respectively.Input Format:For Car:The first line contains the make, model, price, and number of doors of the car, separated by spaces.For Truck:The first line contains the make, model, price, and maximum load capacity of the truck, separated by spaces.Output Format:The first line should display information about the car, including the make, model, price, and number of doors.The second line should display information about the truck, including the make, model, price, and maximum load capacity.Sample Test CasesTest Case 1:Expected Output:Enter·make,·model,·price,·and·number·of·doors·of·the·car·(separated·by·spaces):·Honda civic 500000 5Enter·make,·model,·price,·and·maximum·load·capacity·of·the·truck·(separated·by·spaces):·Tesla hammer1002 1200000 7000Honda·civic·$500000Number·of·doors:·5Tesla·hammer1002·$1200000Maximum·load·capacity:·7000·tons
Defi ne a class MotorVehicle as describedbelow: Data members: (a) modelName (b) modelNumber (c) modelPrice Methods: (a) display() method to display the name,price, and model number. Defi ne another class named Car that inherits theclass MotorVehicle and has the following: Data members: (a) discountRateMethods: (a) display() method to display the Carname, Car model number, Car price, andthe discount rate. (b) discount() method to compute thediscount Create the classes MotorVehicleand Car with suitable constructors andtest it.
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.
Create an abstract class Motor Vehicle with the following details:Data Members: (a) model Name (b)model Number (c) model PriceMethods: display() to show all the details.Create a subclass of this class Car that inherits the class Motor Vehicle and add the following details:Data Members: discount RateMethods:(a) display() method to display the Car name, model number, price and the discount rate.(b) discount() method to compute the discount.Input FormatName:Model No:Price:Discount rate:Discount value:Price after discount:Output FormatVehicleTN SA5221100000.00202000.008000.00
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.
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.