Making sure that encapsulation is not violated (i.e., instance variables must be private); design an inheritance...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Answer rating: 100% (QA)
Vehiclejava public class Vehicle private int year private String make private int currentSpeed publi... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
The Crazy Eddie fraud may appear smaller and gentler than the massive billion-dollar frauds exposed in recent times, such as Bernie Madoffs Ponzi scheme, frauds in the subprime mortgage market, the...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
The organizing committee for the HCC Celebrates Columbia's 50th Birthday, Inc., has planned a special event commemorating notable people originally from Columbia, MD. The committee has invited the...
-
Which of the following statements about Erlang are TRUE? Check all that apply All functions in an .erl file are public by default, i.e. callable from the shell or from any other file All parameters...
-
Define each of the following terms: a. Capital budgeting; regular payback period; discounted payback period b. Independent projects; mutually exclusive projects c. DCF techniques; net present value...
-
1. Describe the outcome predicted by the median voter model. 2. State the factor on which voter choice depends when people vote for nonexcludable public goods. 3. Explain whether voting will lead to...
-
Rank in order, from smallest to largest, the resistances R 1 to R 4 of the four resistors. 2 V 2 V IV IV IA 2 A IA 2A R R R3 R
-
Gansac Publishing Company signed a contract with an author to publish her book. The signing took place on January 1, 2016, and a payment of $20,000 was made to obtain a copyright. Gansac expects to...
-
You agree that a tenant can buy a directory sign at your cost plus a fifteen percent (15%) mark-up. The tenant is charged $ 373.75 for the sign. What was your cost? if you pay $ 225.00 for the...
-
In cell K3, enter a formula using the COUNTIF function that counts the number of Expansion projects, checking that the column of project types in the Projects table (Projects[Project Type]) is equal...
-
The following parametric curves have been used to define the shape of a turbine blade for a new jet engine. The two curves represent the bottom and top edges of the blade. P1 (u) = (3u, u2 + 1, 1-u)...
-
Video Poker: Do PROGRAMMING PROJECT P7.10 in the 7th Edition of Big Java (this problem is P7.9 in the 6th Edition). You must follow the instructions below to receive credit for this assignment....
-
Assume a company projects it will sell 50 computers in the next year. Purchase of each computer is $1,000 and relative cost per machine is $25. What will be the financial drain on the organization if...
-
What is the title? How does the title contribute to your understanding of the meaning?
-
Make a university portal in java just for students studying computer science and computer engineering. It maintains data on system users as well as the various classes offered in these two majors....
-
11. Suppose your boss makes a statement that American children study more hours per week than Brazilian children. You think American children study less. You do some research and find that a sample...
-
The trial balance of Rollins Inc. included the following accounts as of December 31, 2021: Debits Credits Sales revenue 6,700,000 Interest revenue 44,000 Loss on sale of investments 20,000 Loss on...
-
In the circuit shown in Figure 4, a battery supplies a constant voltage of 40 V, the inductance is 2 H, the resistance is 10, and l(0) = 0. (a) Find l(t). (b) Find the current after 0.1s.
-
Greg died on July 1, 2012, and left Lea, his wife, a $50,000 life insurance policy which she elects to receive at $5,000 per year plus interest for 10 years. In the current year, Lea receives $6,200....
-
Rebecca and Walter Bunge have been married for 5 years. They live at 883 Scrub Brush Street, Apt. 52B, Las Vegas, NV 89125. Rebecca is a homemaker and Walt is a high school teacher. Rebecca's Social...
-
Skyler is covered by his company's health insurance plan. The health insurance costs his company $3,500 a year. During the year, Skyler is diagnosed with a serious illness and the health insurance...
-
What are the main phases of a design review?
-
Classify these Bluetooth layers using the OSI model: a. baseband; b. L2CAP; c. RFCOMM.
-
Use Bluetooth to connect a simple sensor, such as an electric eye, to a database.
Study smarter with the SolutionInn App