JAVA!!!!! Part I Specification: This will be a car racing game using object-oriented programming. You have some
Question:
JAVA!!!!!
Part I Specification:This will be a car racing game using object-oriented programming. You have some of this done from the previous assignment. Start with the code that is in Video 14.
Create a class calledCar. It will have instance variablesname, speed, and location. Create the constructor, accessor, mutator, andtoStringmethods.
These cars (objects of this class) will be in a race later the location is their place on the race track. All the cars will start at location 0.
All cars have a maximum speed of 120, and a minimum speed of 0. These should be stored as shared variables (static) in the Car class. The instance variables should not be allowed to go outside of these boundaries, i.e., a car object with a speed outside of the valid range would be an object in aninvalid state.
Add two methods to the Car class called accelerate and decelerate that have one parameter. These methods will add or subtract the parameter to/from the speed of that car.
Add a method called randomSpeedChange() that will increase or decrease the speed of the car by a random value in the range of -10 to 10 mph. (No parameters)
Demonstrate that your class is working by creating 2 car objects, and change their state by calling the methods, and using the property methods.
Car andretti = new Car(Andretti, 30); // creates a car going 30
andretti.randomSpeedChange();
Show the object before and after calling some methods that change the state of the object.
Create another class calledRacewith amainmethod. Make some Car objects, and use them in a race.
The first race will have 2 cars. The main method only needs 2 local variables, i.e. the 2 Car objects.
The length of the race is 1000.
Both cars will start out with a speed of 30.
The race will be implemented as a loop.Each time through the loop:
Your cars will move to a next location by adding their current speed to their location. (If the speed is 50, and they are at location 200, then change the location to 250.)
Call out the race (SOPLs) like a NASCAR announcer: Andretti is in the lead at position 365. He is flying at 30 mph!
Or Its neck and neck with both cars at position 45 or something more creative that you can come up with.
Each car will increase or decrease their speed randomly each time through the main loop.
The car that is behind will increase its speed by 2 mph, and the car that is in the lead will decrease its speed by 2 mph. (Tighten the race to make it more exciting.)
The loop will continue until one of the cars is declared to be the winner.
Part II Using the same Car class, but start a new main for this part.
Second Race. Create another RacingGame class that has a main method. This race will work similar to the first one, but there will be 10 cars in this race. The car objects must be stored in an array of Car objects.
Car[] cars = new Car[10];
cars[0] = new Car( .. );
cars[1] = new Car( etc.
Most of this race is the same as the 2 car race above.
The length of the race is 1000.
All cars will start out with a speed of 30.
The race will be implemented as a loop. Each time through the loop:
Your cars will move to a new position byadding their current speed to their position.
Each car will increase or decrease their speed randomly each time through the main loop.
Call out the race (SOPLs) like a NASCAR announcer: Andretti is in the lead at location 36. He is flying at 30 mph! (Create a static method called from the main that will determine the leader.)
public static Car leadCar(Car[] cars) { // returns a reference to the car object that is in the lead
now you can refer to the leader as a method call. Think about this:
while( leadCar(cars).getLocation()