Question: Making sure that encapsulation is not violated (i.e., instance variables must be private); design an inheritance hierarchy of classes Car, Truck, and Vehicle based

Making sure that encapsulation is not violated (i.e., instance variables must be private); design an inheritance hierarchy of classes Car, Truck, and Vehicle based on the following description. Car is a Vehicle. Truck is a Vehicle. (a) (10 Points) Definition of class Vehicle Instance variables: year make that holds the year of vehicle (int) that holds the make of vehicle (string) Methods: a default constructor with no arguments. a second constructor that accepts the Vehicle's year and the make as arguments. These values should be assigned to the object's instance variables: year and make. A copy constructor that accepts an existing vehicle object reference as argument and copies the values of instance variables to the newly created object. Accessor methods (getYear() and getMake()) that get the values of instance variables. Mutator methods (setYear(int) and setMake(String)) that set the values of instance variables. . toString() should return a string with the values of instance variables of vehicle object as follows. For example, the method should return the following string for a vehicle Ford 2022. year: 2022, make: Ford equals() should accept a vehicle object reference as argument and return true if all instance variables of calling object are same values as those of passed vehicle object, respectively, or false otherwise. brake() should do nothing (i.e., empty body). This method is for polymorphism to call appropriate brake () method of subclass. (b) (10 Points) Definition of class Car, which is a subclass of Vehicle. Instance variables: speed Methods: a default constructor with no arguments. that holds the car's current speed (int) another constructor that accepts the car's year and make, as arguments and should assign them to the object's instance variables. A copy constructor that accepts an existing Car object reference as argument and copies the values of instance variables to the newly created object. . . Accessor methods (getSpeed()) that get the values of instance variables. Mutator methods (setSpeed()) that set the values of instance variables with those of argument values. toString() should return a formatted string with the values of instance variables of a car. For example, the method should return the following string for a car Toyota 2023 at speed 95. Car year: 2023, make: Toyota, current speed: 95 This toString() should call the parent class's toString() method to return the string given above. equals() should accept a Car object reference as argument and return true if all instance variables of calling object are same values as those of passed Car object, respectively, or false otherwise. accelerate() should receive an argument for maximum speed limit. It should add 20 to instance variable speed, each time accelerate() is called. It should limit the speed, i.e., this method can't change speed to more than the maximum speed limit. brake () method should subtract 10 from the instance variable speed, each time brake() is called. The value of instance variable speed can't be less than 0, i.e., this method can't change speed to less than 0. sameSpeed() method should accept a Car object reference as argument, and return true if the speed of the calling object is the same as that of passed object, and false otherwise. (c) (10 Points) Definition of class Truck, which is a subclass of Vehicle. Instance variables: speed holds the truck's current speed (int) Methods: a default constructor with no arguments. another constructor that accepts the truck's year, make and initial speed as arguments. These values should be assigned to object's instance variables: year, make, and speed. Accessor method (getSpeed() method) that gets the value of instance variable. Mutator method (setSpeed() method) that sets the value of instance variable with that of argument. toString() should returns a formatted string with the values of instance variables of a truck. For example, the method should return the following string for a truck Toyota 2023 at speed 95. Truck year: 2023, make: Toyota, current speed: 95 . . This toString() should call the parent class's toString() method to return the string given above. . . accelerate() should receive an argument for maximum speed limit. It should add 10 to instance variable speed, each time accelerate() is called. It should limit the speed, i.e., this method can't change speed to more than the maximum speed limit. brake () method should subtract 5 from the instance variable speed each time brake() is called. The value of instance variable speed can't be less than 0, i.e., this method can't change speed to less than 0. (d) (10 Points) Write a driver program (i.e., class Driver) to accomplish the following: Driver program should accept two integer values as input arguments (i.e., String[] args of main method). The first argument value should be the maximum speed limit of a car, and the second argument should be the maximum speed limit of a truck. Declare a Car object (with carl to be the name of reference variable) by calling the default constructor, and then set the instance variables by calling the appropriate set methods for Toyota 2021. The initial speed of this car should be 15. Declare a Car object (with car2 to be the name of reference variable) by calling the two-argument constructor for Ford 2022. The initial speed of this car should be 0. Print the carl object. . Print the car2 object. . Accelerate car2 by calling accelerate() method ten times using a for loop. The accelerate() method should pass the maximum speed of the car (i.e., args[0] of main() method) as argument. Print the car2 object. Using the copy constructor, create a Car object (with car3 to be the name of reference variable) by passing the reference variable carl as argument. Using equals() method, check if carl is same car3 and print "cart is same as car3" if both cars are the same, otherwise print "car1 is not same as car3" Using equals() method, check if carl is same car2 and print "cart is same as car2" if both cars are the same, otherwise print "car1 is not same as car2" Set car3 speed to 88. Declare a Truck object (with truck1 to be the name of reference variable) by calling three arguments constructor for GMC 2022 and an initial speed of this truck to be 25. Print the truck1 object. . Print the truck2 object. . Accelerate both trucks (i.e., truck1 and truck2) by calling accelerate() method four times using a for loop. Create an array of five Vehicle objects with elements to be the three car references (i.e., car1, car2, and car3 objects) and two truck references (i.e., truck1 and truck2 objects). Call brake () method, three times for all Vehicle objects in the Vehicle array. Hint: declare brake () method with an empty body in Vehicle class for appropriate brake() method (i.e., either the car's brake (), or the truck's brake() method to be called automatically via the polymorphism feature. Using a for loop, print all Vehicle objects in the array. For example: The output of this Driver should be as follows, for String[] args values of main() to be 100 and 60: Car year: 2021, make: Toyota, current speed: 15 Car year: 2022, make: Ford, current speed: 0 Car year: 2022, make: Ford, current speed: 100 carl is same as car3 carl is not same as car2 Truck year: 2022, make: GMC, current speed: 25 Truck year: 2023, make: Volvo, current speed: 0 Car year: 2021, make: Toyota, current speed: 0 Car year: 2022, make: Ford, current speed: 70 Car year: 2021, make: Toyota, current speed: 58 Truck year: 2022, make: GMC, current speed: 45 Truck year: 2023, make: Volvo, current speed: 25 Submission instructions: Please submit only the source code of java classes (Vehicle.java, Car.java, Truck.java, Driver.java) all of them in the same answer textbox by cut/paste one after another, or by directly coding in the textbox.
Step by Step Solution
3.44 Rating (157 Votes )
There are 3 Steps involved in it
Vehiclejava public class Vehicle private int year private String make private int currentSpeed publi... View full answer
Get step-by-step solutions from verified subject matter experts
