Question

In: Computer Science

Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {...

Explain the java class below, how it make:

import java.util.*;
import java.util.concurrent.TimeUnit;
public class RacingGame {
  
   ArrayList<Driver> player = new ArrayList<Driver>();
   CarType car = new CarType("Ferrari","2018",280,90,15);
   Formula formula = new Formula(5);// number of laps
   long startTime = System.currentTimeMillis();
   long currentTime = System.currentTimeMillis();
   int totalDis = 0;
  
   public static void main(String[] args)
   {
       RacingGame formulaRace = new RacingGame();
       formulaRace.player.add(new Driver("Saleh",10,3));
       formulaRace.formula.setDistance(20);//lap distance
       formulaRace.totalDis = formulaRace.formula.getTotalDistance();
       formulaRace.formula.setDistance(formulaRace.totalDis);
       formulaRace.raceStatus();
   }
  
   public void raceStatus()
   {
       int newSpeed = 0;
       long diff = 0;
       long sec = 0;
       while (formula.distance > 0)
       {
          
           currentTime = System.currentTimeMillis();
           diff = currentTime - startTime;
           sec = diff/1000;
           newSpeed = (int) sec * car.getAcceleration();
           car.setSpeed((int)(newSpeed + (newSpeed* ((double)player.get(0).getExperince()/10) * ((double)player.get(0).getLevel()/100))));
           int newDistance = (int) (formula.distance - ((car.getSpeed()*sec))/3600);
           formula.setDistance(newDistance);
           int traveledDistance = totalDis - newDistance;
           System.out.println("Driver name: " + player.get(0).getPlayerName() + ", current Speed: " + car.getSpeed() + " km/h, Traveled distance: " + traveledDistance + " km");
           try {
               TimeUnit.SECONDS.sleep(1);
           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
       System.out.println("Driver name: " + player.get(0).getPlayerName() + " finished the race in " + sec + " seconds");
       }
      
}

//Driver(String playerName, int year, int level, String experienceType)
//CarType(String model, String year, int maxSpeed, int balance, double acceleration)

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Code execution flow is as below

package c11;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class RacingGame {
  
//declare array list for the driver
ArrayList<Driver> player = new ArrayList<Driver>();

//define class object car
CarType car = new CarType("Ferrari","2018",280,90,15);

//define class object of formula
Formula formula = new Formula(5);// number of laps

//get start time
long startTime = System.currentTimeMillis();

//get current time
long currentTime = System.currentTimeMillis();

//store total distance
int totalDis = 0;
  

//main function
public static void main(String[] args)
{
   //start racing game
RacingGame formulaRace = new RacingGame();

//add player to formula race
formulaRace.player.add(new Driver("Saleh",10,3));
//set distance of 20
formulaRace.formula.setDistance(20);//lap distance
//get total distance
formulaRace.totalDis = formulaRace.formula.getTotalDistance();
//set distance = total distance
formulaRace.formula.setDistance(formulaRace.totalDis);
//get race status
formulaRace.raceStatus();
}
  
//function race status
public void raceStatus()
{
   //start speed to 0
int newSpeed = 0;
//set diff to 0
long diff = 0;
//set sec to 0
long sec = 0;
//while formula distnace is greater than zero
while (formula.distance > 0)
{
     
   //get current time
currentTime = System.currentTimeMillis();
//get total time from start
diff = currentTime - startTime;
//get diff in seconds
sec = diff/1000;
//set new speed = sec * car acceleration
newSpeed = (int) sec * car.getAcceleration();
//set car speed according to experince and level
car.setSpeed((int)(newSpeed + (newSpeed* ((double)player.get(0).getExperince()/10) * ((double)player.get(0).getLevel()/100))));
//get new distance
int newDistance = (int) (formula.distance - ((car.getSpeed()*sec))/3600);
//set distance to formula
formula.setDistance(newDistance);
//get travel distance
int traveledDistance = totalDis - newDistance;
//print driver info
System.out.println("Driver name: " + player.get(0).getPlayerName() + ", current Speed: " + car.getSpeed() + " km/h, Traveled distance: " + traveledDistance + " km");
try {
   //wait for 1 second
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//print driver info
System.out.println("Driver name: " + player.get(0).getPlayerName() + " finished the race in " + sec + " seconds");
}
  
}


Related Solutions

In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
import java.util.*; import java.security.*; import javax.crypto.*; import java.nio.file.*; public class CryptoApp {    public static void...
import java.util.*; import java.security.*; import javax.crypto.*; import java.nio.file.*; public class CryptoApp {    public static void main(String[] args) throws Exception { Crypto crypto = new BasicCrypto();        String welcome = "Hello 2043-er's! Let's try this again :-)"; System.out.println(welcome); // First, where are we? //Let's print out our current working directory        Path cwd = FileSystems.getDefault().getPath("").toAbsolutePath(); System.out.println("Current Working Directory: " + cwd); // Read in our file to encrypt    byte[] originalData = Files.readAllBytes(Paths.get(System.getProperty("user.home"), "C-2044-Sample/Crypto/src/encrypt.txt")); // Encrypt it and...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module public static void score_search(int s,int score[]) { // Initialise flag as 0 int flag=0; // Looping till the end of the array for(int j=0;j<10;j++) { // If the element is found in the array if(s==score[j]) { // Update flag to 1 flag=1; } } // In case flag is 1 element is found if(flag==1) { System.out.println("golf score found"); } // // In case flag...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class HashTable implements IHash { // Method of hashing private HashFunction hasher; // Hash table : an ArrayList of "buckets", which store the actual strings private ArrayList<List<String>> hashTable; /** * Number of Elements */ private int numberOfElements; private int numberOfBuckets; /** * Initializes a new instance of HashTable. * <p> * Initialize the instance variables. <br> * Note: when initializing the hashTable, make sure to...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT