Knowee
Questions
Features
Study Tools

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

Question

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·500000Numberofdoors:5Teslahammer1002500000Number·of·doors:·5Tesla·hammer1002·1200000Maximum·load·capacity:·7000·tons

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

Solution

Sure, here is the Python code that models vehicles, specifically two types: Car and Truck.

class Vehicle:
    def __init__(self, make, model, price):
        self.make = make
        self.model = model
        self.price = price

    def display_info(self):
        return f"{self.make} {self.model} ${self.price}"

class Car(Vehicle):
    def __init__(self, make, model, price, num_doors):
        super().__init__(make, model, price)
        self.num_doors = num_doors

    def display_info(self):
        return super().display_info() + f"\nNumber of doors: {self.num_doors}"

class Truck(Vehicle):
    def __init__(self, make, model, price, max_load):
        super().__init__(make, model, price)
        self.max_load = max_load

    def display_info(self):
        return super().display_info() + f"\nMaximum load capacity: {self.max_load} tons"

# Test Case
car_info = input("Enter make, model, price, and number of doors of the car (separated by spaces): ").split()
truck_info = input("Enter make, model, price, and maximum load capacity of the truck (separated by spaces): ").split()

car = Car(car_info[0], car_info[1], int(car_info[2]), int(car_info[3]))
truck = Truck(truck_info[0], truck_info[1], int(truck_info[2]), int(truck_info[3]))

print(car.display_info())
print(truck.display_info())

This code first defines a base class Vehicle with attributes make, model, and price. It then defines two derived classes Car and Truck that inherit from Vehicle and introduce their unique attributes num_doors and max_load respectively. The display_info method is implemented in Vehicle and overridden in Car and Truck to display the appropriate information. The program then takes user input for a car and a truck, creates instances of Car and Truck with the input data, and prints their information.

This problem has been solved

Similar Questions

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

Design a C++ program to manage vehicles for a transport company. The program should handle three types of vehicles: cars, trucks, and buses. Implement a class with a base class and derived classes for each vehicle type. The base class, Vehicle, should have the following attributes: colour, mileage, top speed, and the number of gears. Each derived class should inherit from the Vehicle class and include additional attributes specific to the vehicle type.Car Class (Derived from Vehicle):Additional Attributes: number of doors and fuel type.Functionality: Implement a function to display car details, including all attributes inherited from the base class and the additional attributes specific to cars.Truck Class (Derived from Vehicle):Additional Attributes: cargo capacity and max load capacity.Functionality: Implement a function to display truck details, including all attributes inherited from the base class and the additional attributes specific to trucks.Bus Class (Derived from Vehicle):Additional Attributes: seating capacity and route number.Functionality: Implement a function to display bus details, including all attributes inherited from the base class and the additional attributes specific to buses.

Car sales program: 1. Create a program in which the user is asked to enter the brand, model and selling price of a car. Print the information in one print() command subsequently using formatting printing.

Create two (2) classes named Car and TestCar.In the Car class, initialize two (2) public static variables: MAKE (String, final) and numCars (int). Set their values to "Toyota" and 0, respectively.Declare two (2) empty String instance variables named chassisNo and model.Create a public constructor with a single parameter named model (String).  This constructor should:Increment the value of numCars by one.Set the value of chassisNo based on the result of concatenating the String "ch" and the value of numCars. Syntax: chassisNo = ch + numCars;Use the this keyword to set the instance variable model equal to the value of the parameter model.Display a message saying "Car manufactured".Create two (2) pairs of getter and setter methods that will allow you to get and set the values of the two (2) instance variables: getModel() and setModel(), getChassisNo() and setChassisNo()Add a public toString() method that will return and display the car's make, model, and chassis number in separate lines.In the main method of the TestCar class, display the car's make and the initial number of manufactured cars by accessing them from the Cars class. Example: System.out.println("Manufacturer: " + Car.MAKE);Instantiate two (2) Car objects assigning the values "Camry" and "Veloz", respectively. Example: Car car1 = new Car("Camry");Add two (2) statements to display the data of the two (2) Car objects. Example: System.out.println(car1);Display the total number of cars manufactured by accessing the numCars method.

Complete the Car class byDeclaring the String factory instance variableDeclaring the String model instance variableDeclaring int year instance variableDeclaring string colour instance variableWriting the constructor method with input parameters for factory, model, year and colourWriting the method carAge to display the age of the car (2024 - year)Complete the main method byUsing new to create an instance of Car with the constructor: Car("BMW" , "X5" , 2021 , "Black") Printing the car detailsPrinting the return value for the carAge() method on the Car instance

1/2

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.