Question
Write a Car class that has the following fields: year: The year field is an int that holds the car's year. model: The make field
Write a Car class that has the following fields:
year: The year field is an int that holds the car's year.
model: The make field is a String object that holds the model of the car.
speed: The speed field is an int that holds the car's current speed.
instanceNo: The STATIC instanceNo int that is an accumulator to keep track of how many car objects are in the main method.
In addition, the class should have the following methods:
constructor #1: The constructor should accept the car's year and model as arguments. These parameters should be assigned to the object's year and model fields. The constructor should also assign 0 to the speed field. Lastly, it should increment the static field instanceNo by one.
constructor #2: This constructor should make a deep copy of another car and produce a new car that is a carbon copy of the original. Lastly, it should increment the static field instanceNo by one.
accessors: The appropriate accessor methods should be implemented to access all of the instance fields.
accelerate: The accelerate method should add 5 to the speed field when it is called.
brake: The brake method should subtract 5 from the speed field each time it is called.
equals: The equals method should determine if two car objects have equivalent year and model fields.
fastest: A boolean-returning method that returns true if the initial car is going faster (compare two different cars' speeds).
toString: Prints the car's model, year and speed when a car is passed to println().
Create a main method that uses your Car class:
Create three different car objects;
Create a fourth car object that is a carbon copy of your third car -- use the second constructor;
Accelerate cars three and four 3 times;
Print the current speed of all four cars;
Brake your fourth car once, so the third and fourth cars are no longer traveling at the same speed;
Print the current speed of all four cars;
Call the equals method on cars three and four, and print to the end user if they are duplicates (which they should be);
Call the faster method on cars one and four, and print to the end user if car one is going faster than car four.
println() car three.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started