/*******This is Vehicle class *********/ public class Vehicle { private String r
ID: 3713107 • Letter: #
Question
/*******This is Vehicle class *********/
public class Vehicle {
private String registrationNumber;
private String ownerName;
private double price;
private int yearManufactured;
public Vehicle() {
registrationNumber="";
ownerName="";
price=0.0;
yearManufactured=0;
}
public Vehicle(String registrationNumber, String ownerName, double price, int yearManufactured) {
this.registrationNumber=registrationNumber;
this.ownerName=ownerName;
this.price=price;
this.yearManufactured=yearManufactured;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public String getOwnerName() {
return ownerName;
}
public double getPrice() {
return price;
}
public int getYearManufactured() {
return yearManufactured;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber=registrationNumber;
}
public void setOwnerName(String ownerName) {
this.ownerName=ownerName;
}
public void setPrice(double price) {
this.price=price;
}
public void setYearManufactured(int yearManufactured) {
this.yearManufactured=yearManufactured;
}
public String toString() {
return " Registration number:"+ registrationNumber +" Owner Name:"+ownerName+" Price:"+
price+" year manufactured"+yearManufactured;
}
}
3. (5 pts Inheritance) Hand-write two Java classes, Car and Truck. They are both subclasses of Vehicle. a. A car has an additional instance variable, numberOfDoors. b. A truck has an additional instance variable, numberOfAxles c. Write a constructor that requires input for the instance variables of each class (including receiving registration number, owner name, price, and year manufactured, and calling super with them) d. Write getters and setters for all instance variables for both classes (e.g., Car and Truck).Explanation / Answer
class Vehicle { private String registrationNumber; private String ownerName; private double price; private int yearManufactured; public Vehicle() { registrationNumber=""; ownerName=""; price=0.0; yearManufactured=0; } public Vehicle(String registrationNumber, String ownerName, double price, int yearManufactured) { this.registrationNumber=registrationNumber; this.ownerName=ownerName; this.price=price; this.yearManufactured=yearManufactured; } public String getRegistrationNumber() { return registrationNumber; } public String getOwnerName() { return ownerName; } public double getPrice() { return price; } public int getYearManufactured() { return yearManufactured; } public void setRegistrationNumber(String registrationNumber) { this.registrationNumber=registrationNumber; } public void setOwnerName(String ownerName) { this.ownerName=ownerName; } public void setPrice(double price) { this.price=price; } public void setYearManufactured(int yearManufactured) { this.yearManufactured=yearManufactured; } public String toString() { return " Registration number:"+ registrationNumber +" Owner Name:"+ownerName+" Price:"+ price+" year manufactured"+yearManufactured; } } class Car extends Vehicle{ private int numberOfDoors; public Car(String registrationNumber, String ownerName, double price, int yearManufactured, int numberOfDoors) { super(registrationNumber, ownerName, price, yearManufactured); this.numberOfDoors = numberOfDoors; } public int getNumberOfDoors() { return numberOfDoors; } @Override public String toString() { return "Car{" + "numberOfDoors=" + numberOfDoors + '}'; } public void setNumberOfDoors(int numberOfDoors) { this.numberOfDoors = numberOfDoors; } } class Truck extends Vehicle{ private int numberOfAxles; public Truck(String registrationNumber, String ownerName, double price, int yearManufactured, int numberOfAxles) { super(registrationNumber, ownerName, price, yearManufactured); this.numberOfAxles = numberOfAxles; } public int getNumberOfAxles() { return numberOfAxles; } public void setNumberOfAxles(int numberOfAxles) { this.numberOfAxles = numberOfAxles; } @Override public String toString() { return "Truck{" + "numberOfAxles=" + numberOfAxles + '}'; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.