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) Definition of class Vehicle Instance variables: year                 that holds the year of vehicle (int) make               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) Definition of class Car, which is a subclass of Vehicle. Instance variables: speed              that holds the car's current speed (int)   Methods: a default constructor with no arguments. 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) 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.

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter11: More Object-oriented Programming Concepts
Section: Chapter Questions
Problem 2RQ
icon
Related questions
Question
100%

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) Definition of class Vehicle

Instance variables:

  • year                 that holds the year of vehicle (int)
  • make               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) Definition of class Car, which is a subclass of Vehicle.

Instance variables:

  • speed              that holds the car's current speed (int)

 

Methods:

  • a default constructor with no arguments.

  • 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) 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.

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

 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 car1 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 car1 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 car1 as argument.

 

  • Using equals() method, check if car1 is same car3 and print "car1 is same as car3" if both cars are the same, otherwise print "car1 is not same as car3"

 

  • Using equals() method, check if car1 is same car2 and print "car1 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.

 

  • Declare a Truck object (with truck2 to be the name of reference variable) by calling the default constructor. By calling the set methods, set the instance variables to Volvo 2023. The initial speed of this car should be 0.

 

  • 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.
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT