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:
Question
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:
Solution 1
Here is the Java code for the problem:
// Abstract class MotorVehicle
abstract class MotorVehicle {
String modelName;
int modelNumber;
double modelPrice;
MotorVehicle(String modelName, int modelNumber, double modelPrice) {
this.modelName = modelName;
this.modelNumber = modelNumber;
this.modelPrice = modelPrice;
}
abstract void display();
}
// Subclass Car
class Car extends MotorVehicle {
double discountRate;
Car(String modelName, int modelNumber, double modelPrice, double discountRate) {
super(modelName, modelNumber, modelPrice);
this.discountRate = discountRate;
}
void display() {
System.out.println("Name: " + modelName);
System.out.println("Model No: " + modelNumber);
System.out.println("Price: " + modelPrice);
System.out.println("Discount rate: " + discountRate);
System.out.println("Discount value: " + discount());
System.out.println("Price after discount: " + (modelPrice - discount()));
}
double discount() {
return modelPrice * discountRate / 100;
}
}
In this code, MotorVehicle is an abstract class with data members modelName, modelNumber, and modelPrice. It has a constructor to initialize these data members and an abstract method display().
Car is a subclass of MotorVehicle that adds a new data member discountRate. It has a constructor to initialize the data members of both the superclass and itself. It also provides an implementation for the display() method and adds a new method discount() to compute the discount.
Solution 2
Here is the Java code for the problem:
// Abstract class MotorVehicle
abstract class MotorVehicle {
String modelName;
int modelNumber;
double modelPrice;
MotorVehicle(String modelName, int modelNumber, double modelPrice) {
this.modelName = modelName;
this.modelNumber = modelNumber;
this.modelPrice = modelPrice;
}
abstract void display();
}
// Subclass Car
class Car extends MotorVehicle {
double discountRate;
Car(String modelName, int modelNumber, double modelPrice, double discountRate) {
super(modelName, modelNumber, modelPrice);
this.discountRate = discountRate;
}
void display() {
System.out.println("Name: " + modelName);
System.out.println("Model No: " + modelNumber);
System.out.println("Price: " + modelPrice);
System.out.println("Discount rate: " + discountRate);
}
void discount() {
double discountValue = modelPrice * discountRate / 100;
double priceAfterDiscount = modelPrice - discountValue;
System.out.println("Discount value: " + discountValue);
System.out.println("Price after discount: " + priceAfterDiscount);
}
}
In this code, MotorVehicle is an abstract class with data members modelName, modelNumber, and modelPrice. It has a constructor to initialize these data members and an abstract method display().
Car is a subclass of MotorVehicle that adds a new data member discountRate. It has a constructor to initialize all data members including those inherited from MotorVehicle. It overrides the display() method to display all data members. It also has a discount() method to compute the discount value and the price after discount.
Similar Questions
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.
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 form:Car (VEHICLE TYPE)HRDQ2456 (VEHICLE NUMBER)52WVC1234 (ENGINE NUMBER)2H2X1234 (CHASSIS NUMBER)535kph (SPEED)4 (No. of PASSENGER)5 (GEARS)Output form:CarHRDQ245652WVC12342H2X1234535kph45
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
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
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
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.