Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem to Solve: Create a class called \"CarRacing\" which contains your main d

ID: 3720706 • Letter: P

Question

Problem to Solve:

Create a class called "CarRacing" which contains your main driver method, to simulate car racing as described below.

I have attached a sample input and output.

1. Create an Array object called "cars" to store and manage a number of Car objects.

2. Car class consists of the Strings make, model, and serialNumber, and the integers year, and score.

3. Write the appropriate program segments to carry out the following operations:

          1. Run a race: Given the number of cars and duration of the competition, run a race. After the competition, display the cars in descending (i.e., highest to lowest) order of their scores. Before running the race, you must create a correct number of car objects as described below. (Make sure to use constant identifiers to represent each of the literals such as minimum and maximum car years);

                    a. The make of a car is composed of the text "Make" followed by 2 digits representing the car's respective order of creation, starting from 0. For example, the make of the first car is "Make00" and the make of the third car is "Make02".

                    b. The model of a car is composed of the text "Model" followed by 2 digits representing the car's respective order of creation, starting from 0. For example, the model of the first car is "Model00" and the model of the third car is "Model02".

                    c. The year of a car will be a random number, inclusively, between 1970 and 2018.

                    d. The serialNumber of a car is composed of the text "Sn" followed by 4 digits. The last 4 digits will be a random number, inclusively, between 1000 and 2000.

                    e. The score of a car will be computed during the race. In each minute of the race, the car travels some number of miles, which will be added to the score. The number of miles a minute will be a random number, inclusively, between 1 and 4.

                    f. Use 1000 as the seed for the random number generator.

          2. Update a score: Given a car's serial number, change the score of the respective car.

          3. Displaly all the cars: Display all the cars.

4. The result of your program must match the given sample input and output. Make sure you test your program thoroughly, since I will execute your program on a different side of test data.

Use a package called "CarRacingPackage" to include the CarRacing and Car classes.

5. Naming of Progra Modules:

Program Module Name Java Project CarRacingProject Package CarRacingPackage Car Class Car The Main Driver Class CarRacing Car Racing Description Version: Refer 8 Sample Execution of the Program -CAR RACING By Abi Salimi, CS1301-Section xx, Spring 2018 Available operations Run a race 2. Update a score 3. Display all the cars 9. End the program Enter your desired option:2 Sorry, there are no cars to update!*** Available operations Run a race 2. Update a score 3. Display all the cars 9. End the program Enter your desired option:3 Sorry, there are no cars to display Available operations Run a race 2. Update a score 3. Display all the cars 9. End the program Enter your desired option: 4 Option 4 is invalid Available operations Run a race 2. Update a score 3. Display all the cars 9. End the program Last prieted 4/17/2018 7:06 PM Page 4 of Flename: Car Racing Descripion Ve01.00do

Explanation / Answer

package CarRacingPackage;

import java.util.Random;

public class Car {

  

  

   private String make;

   private String model;

   private String serialNumber;

   private int year;

   private int score;

  

   public Car(int duration,int carNumber) {

       Random random = new Random();

       year = random.nextInt(48) + 1970;

       serialNumber = "Sn" + (random.nextInt(1000) + 1000);

       score = computeScore(duration);

               if(carNumber<10){

                   make = "Make0"+carNumber;

                   model = "Model0"+carNumber;

               }

               else{

                   make = "Make"+carNumber;

                   model = "Model"+carNumber;

               }

   }

  

   // Compute score of car in given duration

   public int computeScore(int duration){

       Random random = new Random();

       int min = duration;

       int score = 0;

       for (int i =0; i<min;i++){

           score += random.nextInt(3) + 1;

       }

       return score;

   }

   /**

   * @return the make

   */

   public String getMake() {

       return make;

   }

   /**

   * @param make the make to set

   */

   public void setMake(String make) {

       this.make = make;

   }

   /**

   * @return the model

   */

   public String getModel() {

       return model;

   }

   /**

   * @param model the model to set

   */

   public void setModel(String model) {

       this.model = model;

   }

   /**

   * @return the serialNumber

   */

   public String getSerialNumber() {

       return serialNumber;

   }

   /**

   * @param serialNumber the serialNumber to set

   */

   public void setSerialNumber(String serialNumber) {

       this.serialNumber = serialNumber;

   }

   /**

   * @return the year

   */

   public int getYear() {

       return year;

   }

   /**

   * @param year the year to set

   */

   public void setYear(int year) {

       this.year = year;

   }

   /**

   * @return the score

   */

   public int getScore() {

       return score;

   }

   /**

   * @param score the score to set

   */

   public void setScore(int score) {

       this.score = score;

   }

  

}

package CarRacingPackage;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Comparator;

import java.util.Scanner;

public class CarRacing {

   private ArrayList<Car>cars;

   /**

   *

   */

   public CarRacing() {

       super();

       cars = new ArrayList<>();

   }

  

   //create cars

   public void runRace(int duration,int carNumber) {

       if (!cars.isEmpty()){

           cars.removeAll(cars);

       }

       for(int i = 0;i<carNumber;i++){

           Car car = new Car(duration,carNumber);

           cars.add(car);

       }

   }

  

   //check car exist in array

   public boolean checkCar(String serialnumber){

      

       for (Car car:cars){

           if (car.getSerialNumber().equals(serialnumber)){

               return true;

           }

       }

       return false;

   }

  

   //update car score

   public void updateCar(String serialnumber,int updateScore){

       for (Car car:cars){

           if (car.getSerialNumber().equals(serialnumber)){

               car.setScore(updateScore);

               return;

           }

       }

   }

  

   //display cars

   public void displayCars(){

       System.out.println("display cars");

       Car[] carArray = new Car[cars.size()];

       carArray = cars.toArray(carArray);

       Arrays.sort(carArray,new SortByScore() );

       System.out.println("Rank "+"Score "+"Serial Number "+"Year "+"Make "+ "Model ");

       int i = 1;

       for(Car car : carArray){

           System.out.println(i+" "+car.getScore()+" "+car.getSerialNumber()+" "+car.getYear()+" "+car.getMake()+" "+car.getModel());

           i++;

       }

   }

   public static void main(String[] args) {

       CarRacing racing = new CarRacing();

       boolean end= false;

       Scanner sc = new Scanner(System.in);

       int option;

       while(!end){

           try{

           System.out.println("Available options: 1.Run a race 2.Update a score 3.Display all the cars 4.End the program");

           option = sc.nextInt();

           switch (option) {

           case 1:

               boolean rightEntry= false;

               while(!rightEntry){

                   System.out.println("Enter number of cars between 10 and 50");

                   int carNumber = sc.nextInt();

                   if (carNumber>9 &&carNumber<51){

                       System.out.println("Enter race duration between 60 and 1440");

                       int duration = sc.nextInt();

                       if (duration>59 &&duration<1441){

                           racing.runRace(duration, carNumber);

                           racing.displayCars();

                           rightEntry = true;

                       }

                       else{

                           System.out.println("Please enter a correct duration");

                       }

                   }

                   else{

                       System.out.println("Please enter a correct numbers of cars");

                   }

               }

               break;

           case 2:

               if(racing.cars.isEmpty()){

                   System.out.println("********Sorry, There are no car to update*********");

               }

               else{

                   System.out.print("Enter a car serial number:");

                   String serialNumber = sc.next();

                   if (racing.checkCar(serialNumber)){

                       System.out.println("Enter the new score for car "+serialNumber);

                       int score = sc.nextInt();

                       racing.updateCar(serialNumber, score);

                   }

                   else{

                       System.out.println("*********The car with serial number"+serialNumber+"not found.***********");

                   }

               }

               break;

           case 3:

               if(racing.cars.isEmpty()){

                   System.out.println("********Sorry, There are no car to display*********");

               }

               else{

                   racing.displayCars();

               }

               break;

           case 4:

               if(racing.cars.isEmpty()){

                   System.out.println("********option 4 is invalid*********");

               }

               else{

                   end=true;

                   System.out.println("Have a nice day!");

               }

               break;

           default:

               System.out.println("Please enter a correct option default");

               break;

           }

           }

           catch(Exception e){

               e.printStackTrace();

               System.out.println("Please enter a correct option catch");

           }

       }

   }

}

// class use to sort array

class SortByScore implements Comparator<Car> {

public int compare(Car a, Car b) {

if ( a.getScore() > b.getScore() ) return -1;

else if ( a.getScore() == b.getScore() ) return 0;

else return 1;

}

}

Sample output:

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

2

********Sorry, There are no car to update*********

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

3

********Sorry, There are no car to display*********

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

4

********option 4 is invalid*********

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

1

Enter number of cars between 10 and 50

10

Enter race duration between 60 and 1440

1200

display cars

Rank Score Serial Number Year Make Model

1 2436 Sn1939 1983 Make10 Model10

2 2424 Sn1502 2014 Make10 Model10

3 2414 Sn1804 1978 Make10 Model10

4 2414 Sn1640 2010 Make10 Model10

5 2409 Sn1478 1993 Make10 Model10

6 2402 Sn1476 1979 Make10 Model10

7 2392 Sn1557 1990 Make10 Model10

8 2384 Sn1815 1981 Make10 Model10

9 2376 Sn1119 1999 Make10 Model10

10 2359 Sn1249 1999 Make10 Model10

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

3

display cars

Rank Score Serial Number Year Make Model

1 2436 Sn1939 1983 Make10 Model10

2 2424 Sn1502 2014 Make10 Model10

3 2414 Sn1804 1978 Make10 Model10

4 2414 Sn1640 2010 Make10 Model10

5 2409 Sn1478 1993 Make10 Model10

6 2402 Sn1476 1979 Make10 Model10

7 2392 Sn1557 1990 Make10 Model10

8 2384 Sn1815 1981 Make10 Model10

9 2376 Sn1119 1999 Make10 Model10

10 2359 Sn1249 1999 Make10 Model10

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

2

Enter a car serial number:12212

*********The car with serial number12212not found.***********

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

2

Enter a car serial number:Sn1249

Enter the new score for car Sn1249

3000

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

3

display cars

Rank Score Serial Number Year Make Model

1 3000 Sn1249 1999 Make10 Model10

2 2436 Sn1939 1983 Make10 Model10

3 2424 Sn1502 2014 Make10 Model10

4 2414 Sn1804 1978 Make10 Model10

5 2414 Sn1640 2010 Make10 Model10

6 2409 Sn1478 1993 Make10 Model10

7 2402 Sn1476 1979 Make10 Model10

8 2392 Sn1557 1990 Make10 Model10

9 2384 Sn1815 1981 Make10 Model10

10 2376 Sn1119 1999 Make10 Model10

Available options:

1.Run a race

2.Update a score

3.Display all the cars

4.End the program

4

Have a nice day!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote