Question

In: Computer Science

(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...

(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(), and toString() methods.
The equals()and toString() methods should each include an @Override annotation.

Suppose each vehicle can keep track of emissions over time. Suppose also that after three months, the number of miles and emissions (in grams) are reported. At this point, if the vehicle’s emissions per mile exceed .2 grams per mile, the vehicle will be recalled. If the vehicle is a truck, emissions per mile may not exceed .4 grams per mile. If recalled, the vehicles are no longer street legal.

Your Person class should keep track of whether a driver has a license or has lost their license. You may assume that all people start with a license.
OUTPUT:
Person 1: Tom Night has a driver's license.
Person 2: Maya Light has a driver's license.
Person 3: Scott Troy has a driver's license.
The Toyota Camry, owned by Tom, is not street legal, with emissions per mile of 0.32.

The Toyota Camry, owned by Maya, is not street legal, with emissions per mile of 0.31.
The Honda Accord, owned by Scott, is street legal, with emissions per mile of 0.25.
Scott Troy has a driver's license

Solutions

Expert Solution

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!!!


Related Solutions

In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
JAVA Design a class named Person and its two derived classes named Student and Employee. Make...
JAVA Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and  numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which will...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an integer à The age of the vehicle       Price, a float à The price of the vehicle       Behaviors: Vehicle() à default constructor sets age=0, and price=0.0 setAge()   à Takes an integer parameter, returns nothing setPrice() à Takes a float parameter, returns nothing getAge()   à Takes no parameters, returns the vehicle’s age getPrice() à Takes no parameters, returns the vehicle’s price End Class Vehicle...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes:...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes: "Bus", "Car", and "Motorcycle". They should contain a variable storing number of seats (bus) / weight (car) / number of tires (motorcycle), constructor, destructor and a print_info function that will print all the information about the vehicle. PK 3. The constructors should take as parameters: company, year and number of seats / weight / number of tires. Inside it assign the company name and...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT