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.
Question
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.
Solution
Here is a Python implementation of the classes MotorVehicle and Car as described:
class MotorVehicle:
def __init__(self, modelName, modelNumber, modelPrice):
self.modelName = modelName
self.modelNumber = modelNumber
self.modelPrice = modelPrice
def display(self):
print("Name: ", self.modelName)
print("Model Number: ", self.modelNumber)
print("Price: ", self.modelPrice)
class Car(MotorVehicle):
def __init__(self, modelName, modelNumber, modelPrice, discountRate):
super().__init__(modelName, modelNumber, modelPrice)
self.discountRate = discountRate
def display(self):
super().display()
print("Discount Rate: ", self.discountRate)
def discount(self):
return self.modelPrice - (self.modelPrice * self.discountRate / 100)
# Testing the classes
car = Car("Tesla", "Model S", 75000, 10)
car.display()
print("Price after discount: ", car.discount())
In this code, we first define the class MotorVehicle with its data members and methods. Then, we define the class Car that inherits from MotorVehicle and adds its own data member and methods. The super().__init__() call in the Car constructor is used to call the constructor of the parent class. The display() method in the Car class first calls the display() method of the parent class to display the common attributes, and then displays its own attribute. The discount() method calculates the price after applying the discount. Finally, we create an instance of the Car class and test its methods.
Similar Questions
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:
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
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
Predict the outputabstract class Vehicle{ abstract void calcPremium(Number N);}interface Insurance{ abstract void calcPremium (Object O);}class Car extends Vehicle implements Insurance{ public void calcPremium (Object O) { System.out.println("Object"); } void calcPremium (Number N) { System.out.println("Number"); } }public class Test{ public static void main(String[] args) { Vehicle a = new Car(); a. calcPremium (new Integer(121)); Insurance b = new Car(); b. calcPremium (new Integer(121)); Car c = new Car(); c. calcPremium (new Integer(121)); }}Select one:a.NumberObjectNumberb.NumberNumberObjectc.Run time errord.Compile time error
Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, as member. Inherit the classes bicycle, car, bike, and lorry from vehicle class. Consider car and bike only as passenger vehicle. Get inputs engine number, chassis number, top speed, number of passengers, number of gears for Passenger vehicle, and if is not a passenger vehicle, get inputs total amount of load to carry for the respective inherited classes. Generate a report for the vehicles. Implement the concept using inheritance and method overriding. Input 1: Car (VEHICLE TYPE) HRDQ2456 (VEHICLE NUMBER) 52WVC1234 (ENGINE NUMBER) 2H2X1234 (CHASSIS NUMBER) 535kph (SPEED) 4 (No. of PASSENGER) 5 (GEARS) Output1: Car HRDQ2456 52WVC1234 2H2X1234 535kph 4 5
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.