Q2) Suppose that our program uses many kinds of vehicles, such as car, truck and
ID: 3755291 • Letter: Q
Question
Q2) Suppose that our program uses many kinds of vehicles, such as car, truck and
motorcycle. You should design a superclass called Vehicle, which defines the behaviors of all
the vehicles. For example, you would like all the vehicles to have a method called display(),
which returns the information of that particular vehicle. The Vehicle class can be written as
follows:
class Vehicle {
protected String registrationNumber;
protected int model;
Vehicle(String registrationNumber, int model) {
this.registrationNumber = registrationNumber;
this.model= model;
}
public String display() {
return " Registration no: " + registrationNumber + "
and Model no: " + model;
}
}
a. Derive Car, Truck and Motorcycle subclasses from the superclass Vehicle.
b. Within Car, Truck and Motorcycle constructor, you must set registrationNumber and the
model through superclass constructor (use super)
c. Each subclass has attribute noOfWheels which reflects the number of wheels it has
d. Override method display() in Car, Truck and Motorcycle so its reflect their information.
e. Create VehicleTest class to demonstrate all class capabilities
(in java programing language )
please help me with these two questions
thank you
Explanation / Answer
As per chegg polocy solving first question: -
// CommunityMember.java
abstract class CommunityMember {
// instance variables
private String firstName;
private String lastName;
// Constructor
public CommunityMember(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Returns first name
public String getFName() {
return firstName;
}
// Returns last name
public String getLName() {
return lastName;
}
@Override
public String toString() {
return "First Name : " + firstName + "Last Name : " + lastName;
}
}
// Student.java
class Student extends CommunityMember {
// instance variables
private int ID;
private double gradeAvg;
// Constructor
public Student(String firstName, String lastName, int ID, double gradeAvg) throws Exception {
super(firstName, lastName);
// Calling setter methods
setID(ID);
setAverage(gradeAvg);
}
// Set id number
public void setID(int ID) throws Exception {
if (ID < 0 || ID > 9999999)
throw new Exception("Invalid ID number");
else
this.ID = ID;
}
// Set average grade points
public void setAverage(double gradeAvg) throws Exception {
if (gradeAvg < 0.0 || gradeAvg > 4.0)
throw new Exception("Invalid grade average number");
else
this.gradeAvg = gradeAvg;
}
// Returns id
public int getID() {
return ID;
}
// Returns grade average
public double gradeAvg() {
return gradeAvg;
}
// Override toString metod
@Override
public String toString() {
return super.toString() + "Student ID : " + ID + " Grade Average : " + gradeAvg;
}
}
// Employee.java
class Employee extends CommunityMember {
// instance variables
private int ID;
private double salary;
// Employee
public Employee(String firstName, String lastName, int id, double salary) throws Exception {
super(firstName, lastName);
// Setter methods
setID(ID);
setSalary(salary);
}
// Set method to set ID
public void setID(int ID) throws Exception {
if (ID < 0 || ID > 9999999)
throw new Exception("Invalid Employee ID number");
else
this.ID = ID;
}
// Set method to set salary
public void setSalary(double salary) throws Exception {
if (salary < 0 || salary > 100000)
throw new Exception("Invalid salary number");
else
this.salary = salary;
}
// Returns ID
public int getID() {
return ID;
}
// Returns salary
public double gradeSalary() {
return salary;
}
// Override toString metod
@Override
public String toString() {
return super.toString() + "Employee ID : " + ID + " Salary : " + salary;
}
}
// Alumnus.java
class Alumnus extends CommunityMember {
// Set gender type
private String gender;
// Constructor to set first,last and gender type
public Alumnus(String firstName, String lastName, String gender) {
super(firstName, lastName);
this.gender = gender;
}
// Override toString metod
@Override
public String toString() {
return super.toString() + " Gender : " + gender;
}
}
// Staff.java
class Staff extends Employee {
// instance variable
private String department;
// Constructor
public Staff(String firstName, String lastName, int id, double salary, String department) throws Exception {
super(firstName, lastName, id, salary);
this.department = department;
}
// Override toString
@Override
public String toString() {
return super.toString() + " Department : " + department;
}
}
// Faculty.java
class Faculty extends Employee {
// instance variables
private String subject;
private Date dateOfJoin;
// Constructor
public Faculty(String firstName, String lastName, int id, double salary, String subject, Date dateOfJoin)
throws Exception {
super(firstName, lastName, id, salary);
this.subject = subject;
this.dateOfJoin = dateOfJoin;
}
// Override toString metod
@Override
public String toString() {
return super.toString() + " Subject : " + subject + " Date of Join : " + dateOfJoin;
}
}
class Administrator extends Faculty {
// instance variables
private String department;
// Constructor
public Administrator(String firstName, String lastName, int id, double salary, String subject, Date dateOfJoin,String department)
throws Exception {
super(firstName, lastName, id, salary, subject, dateOfJoin);
this.department = department;
}
// Override toString metod
@Override
public String toString() {
return super.toString() + " Departments : " + department;
}
}
class Teacher extends Faculty {
// instance variables
private String degree;
// Constructor
public Teacher(String firstName, String lastName, int id, double salary, String subject, Date dateOfJoin,String degree)
throws Exception {
super(firstName, lastName, id, salary, subject, dateOfJoin);
this.degree = degree;
}
// Override toString metod
@Override
public String toString() {
return super.toString() + " Degree : " + degree;
}
}
public class Hierarchy {
public static void main(String[] args) throws Exception {
// Declare an array of CommunityMember
CommunityMember[] communitMember = new CommunityMember[7];
communitMember[0] = new Student("Michell", "Johnson", 1212, 4.0);
communitMember[1] = new Employee("Hellen", "Keller", 1323, 5500);
communitMember[2] = new Alumnus("Kunal", "Malhotra", "Male");
communitMember[3] = new Faculty("BN", "Achari", 4515, 45000, "Computer Scient", new Date(1988, 5, 25));
communitMember[4] = new Staff("Mahn", "Singh", 1000, 5200, "Non-Teaching");
communitMember[5] = new Administrator("BN", "Achari", 4515, 45000, "Computer Scient", new Date(1988, 5, 25),"IT");
communitMember[6] = new Teacher("BN", "Achari", 4515, 45000, "Computer Scient", new Date(1988, 5, 25),"BCA");
// Print the objects of student
System.out.println("Student Details");
System.out.println(communitMember[0].toString());
System.out.println("Employee Details");
// Print the objects of Alumini
System.out.println(communitMember[1].toString());
System.out.println("Alumnus Details");
// Print the objects of Faculty
System.out.println(communitMember[2].toString());
System.out.println("Faculty Details");
// Print the objects of Staff
System.out.println(communitMember[3].toString());
System.out.println("Staff Details");
// Print the objects of Staff
System.out.println(communitMember[4].toString());
System.out.println("Administrator Details");
// Print the objects of Administrator
System.out.println(communitMember[5].toString());
System.out.println("Teacher Details");
// Print the objects of Teacher
System.out.println(communitMember[6].toString());
}
}
Output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.