Concept explainers
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.
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps
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.
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.
- True or False Constructors can accept arguments in the same way as other methods.arrow_forwardDesign a class named Pet, which have the following fields: • Name - name of the pet. • Animal - type of animal that a pet is (Dog, Cat, Bird). • Age - age of the pet. The Pet class should also have the following methods: • setName • setAnimal • setAge • getName •.getAnimal .getAge a) Draw a UML diagram of the class. Be sure to include notation showing each field and method's access specification and data type. Also include notation showing any method parameters and their data types. b) Write the Java code for the Pet class. Create a main method in the same class and instantiate at least two objects of the class.arrow_forwardIt is possible to express the relationship that exists between classes and objects.arrow_forward
- Please include java doc commentarrow_forwardCode the StudentAccount class according to the class diagramMethods explanation:pay(double): doubleThis method is called when a student pays an amount towards outstanding fees. The balance isreduced by the amount received. The updated balance is returned by the method.addFees(double): doubleThis method is called to increase the balance by the amount received as a parameter. The updatedbalance is returned by the method.refund(): voidThis method is called when any monies owned to the student is paid out. This method can onlyrefund a student if the balance is negative. It displays the amount to be refunded and set thebalance to zero. (Use Math.abs(double) in your output message). If there is no refund, display anappropriate message.Test your StudentAccount class by creating objects and calling the methods addFees(), pay() andrefund(). Use toString() to display the object’s data after each method calledarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education