In: Computer Science
Program Code [JAVA]
// Person class
import java.util.Objects;
class Person {
// Required instance variables
private String firstName;
private String lastName;
private boolean hasLicense;
// Constructor
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
// Initially, person has driver's license
this.hasLicense = true;
}
// Setters and Getters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isHasLicense() {
return hasLicense;
}
public void setHasLicense(boolean hasLicense) {
this.hasLicense = hasLicense;
}
// toString and equal methods
@Override
public String toString() {
if(hasLicense)
return firstName + " " + lastName + " has a driver's license.";
else
return firstName + " " + lastName + " has not a driver's license.";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return isHasLicense() == person.isHasLicense() &&
Objects.equals(getFirstName(), person.getFirstName()) &&
Objects.equals(getLastName(), person.getLastName());
}
}
// Vehicle class
class Vehicle {
private String make;
private String model;
private double emissionPerMile;
private Person driver;
// constructor & setters & getters
public Vehicle(String make, String model, double emissionPerMile, Person driver) {
this.make = make;
this.model = model;
this.emissionPerMile = emissionPerMile;
this.driver = driver;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getEmissionPerMile() {
return emissionPerMile;
}
public void setEmissionPerMile(double emissionPerMile) {
this.emissionPerMile = emissionPerMile;
}
public Person getDriver() {
return driver;
}
public void setDriver(Person driver) {
this.driver = driver;
}
// An additional method which determines whether this vehicle is street legal or not
public String getStreetLegal() {
if (emissionPerMile > .2)
return "not street legal";
else
return "street legal";
}
// toString which prints all details as shown in same output
@Override
public String toString() {
return "The " + make + " " + model + ", owned by " + driver.getFirstName()
+ ", is " + getStreetLegal() + ", with emission per mile of " + emissionPerMile + ".";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Vehicle)) return false;
Vehicle vehicle = (Vehicle) o;
return Double.compare(vehicle.getEmissionPerMile(), getEmissionPerMile()) == 0 &&
Objects.equals(getMake(), vehicle.getMake()) &&
Objects.equals(getModel(), vehicle.getModel()) &&
Objects.equals(getDriver(), vehicle.getDriver());
}
}
// Truck class
class Truck extends Vehicle {
private double loadCapacity;
private int towingCapacity;
public Truck(String make, String model, double emissionPerMile, Person driver, double loadCapacity, int towingCapacity) {
super(make, model, emissionPerMile, driver);
this.loadCapacity = loadCapacity;
this.towingCapacity = towingCapacity;
}
// Accessor & Mutators(Getters & Setter)
public double getLoadCapacity() {
return loadCapacity;
}
public void setLoadCapacity(double loadCapacity) {
this.loadCapacity = loadCapacity;
}
public int getTowingCapacity() {
return towingCapacity;
}
public void setTowingCapacity(int towingCapacity) {
this.towingCapacity = towingCapacity;
}
// Re-implementing getStreetLegal method for Truck class
@Override
public String getStreetLegal() {
if (getEmissionPerMile() > .4)
return "not street legal";
else
return "street legal";
}
// toString & equals method
@Override
public String toString() {
return "The truck " + getMake() + " " + getModel() + ", owned by " + getDriver().getFirstName()
+ ", is " + getStreetLegal() + ", with load capacity of " + loadCapacity
+", and towing capacity of " + towingCapacity + " anbd with emission per mile of " + getEmissionPerMile() + ".";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Truck)) return false;
if (!super.equals(o)) return false;
Truck truck = (Truck) o;
return Double.compare(truck.loadCapacity, loadCapacity) == 0 &&
towingCapacity == truck.towingCapacity;
}
}
// A test class to test classes & generate required output as in question
public class Test {
public static void main(String[] args) {
Person person1 = new Person("Tom", "Night");
Person person2 = new Person("Maya", "Light");
Person person3 = new Person("Scott", "Troy");
// Printing them
System.out.println("Person 1: " + person1);
System.out.println("Person 2: " + person2);
System.out.println("Person 3: " + person3);
// Now creating three vehicles
Vehicle vehicle1 = new Vehicle("Toyota", "Camry", 0.32, person1);
Vehicle vehicle2 = new Vehicle("Toyota", "Camery", 0.31, person2);
Vehicle vehicle3 = new Vehicle("Honda", "Accord", 0.2, person3);
System.out.println(vehicle1);
System.out.println(vehicle2);
System.out.println(vehicle3);
System.out.println(person3);
}
}
Sample Output:-
-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!