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...
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...
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
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods: 1) Module1 data fields: a. an object of type Module1Data named m1d b. an object of type Scanner named scanner c. a double named input 2) Module1Data methods: a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT