JAVA!!!!! Part I Specification: This will be a car racing game using object-orie
ID: 3857772 • Letter: J
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 called Car. It will have instance variables name, speed, and location. Create the constructor, accessor, mutator, and toString methods.
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 an invalid 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 called Race with a main method. 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 “It’s 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 by adding 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() < raceLength) { // the race is still on if the leader has crossed the finish line.
Explanation / Answer
Here is the solution, please go through it throughly:-
public class Car {
private int speed;
private String name;
private int location = 0;
public int get_Location() {
return location;
}
public void set_Location(int location) {
this.location = location;
}
private static int maximumSpeedForAll = 120;
public Car(int speed, String name) {
setSpeed(speed);
setName(name);
}
public static int getmaximumSpeedForAll() {
return maximumSpeedForAll;
}
public static void setmaximumSpeedForAll(int maximumSpeedForAll) {
Car.maximumSpeedForAll = maximumSpeedForAll;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed <= maximumSpeedForAll) {
this.speed = speed;
} else {
this.speed = maximumSpeedForAll;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void accelerate (int speed){
this.speed += speed;
if (this.speed > maximumSpeedForAll) {
this.speed = 120;
}
this.location += this.speed;
}
public void decelerate (int speed){
this.speed -= speed;
if (this.speed < 0) {
this.speed = 0;
}
this.location += this.speed;
}
public String toString() {
return "Car (name=" + name + ", speed=" + speed + "]";
}
}
====================================================================================================================================================
import java.util.*;
public class RacingCar {
public static int RacingLength = 1000;
public static boolean racingGameNotFinished = true;
public static Car racingLeadCar(Car[] cars) {
int _racingLeadCarIndex = 0;
int maxLocation = 0;
for(int i=0;i<cars.length;i++) {
if(cars[i].get_Location() > maxLocation) {
maxLocation = cars[i].get_Location();
_racingLeadCarIndex = i;
}
}
return cars[_racingLeadCarIndex];
}
//main
public static void main(String args[]) {
Random rn = new Random();
//creating array to hold ten cars..
Car[] cars = new Car[10];
//initializing cars
for(int i=0;i<10;i++){
cars[i] = new Car(0,"car"+(i+1));
}
// start racing
while(racingGameNotFinished) {
for(int i=0;i<10;i++) {
//accelerate or deaccelerate
int _speed = rn.nextInt(20);
if(_speed <= 10) {
cars[i].accelerate(_speed);
}
else{
cars[i].decelerate(_speed-10);
}
}
//finding racingLeadCar
Car _tmpCar = racingLeadCar(cars);
//getting lead car location
int location = _tmpCar.get_Location();
if(location < 900) {
System.out.println(_tmpCar.getName() +" was in lead with location: "+_tmpCar.get_Location()+" and flying at "+_tmpCar.getSpeed());
}
else if(location >= 900 && location < RacingLength) {
System.out.println(_tmpCar.getName() +" was in lead with location: "+_tmpCar.get_Location()+" and flying at "+_tmpCar.getSpeed());
System.out.println("Racing is about to complete..");
}
else if(location >= RacingLength) {
System.out.println("Racing is completed...");
System.out.println(_tmpCar.getName() +" was the champion of this race");
racingGameNotFinished = false;
break;
}
}
====================================================================================================================================================
Please run these codes into your java window and check out the output.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.