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

**MUST BE IN JAVA** Tortoise and the Hare Race! Operation & Specifications This

ID: 3605940 • Letter: #

Question

**MUST BE IN JAVA**

Tortoise and the Hare Race! Operation & Specifications This application simulates a race between two runners. The runners differ in their speed and how often they need to rest. One of the runners, named “Tortoise,” is slow but never rests. The other runner, named “Hare,” is ten times as fast but rests 90% of the time. There is a random element to the runners’ performance, so the outcome of the race is different each time the application is run. The race is run over a course of 50 meters. Each time one of the runners moves, the application displays the runner’s new position on the course. The first runner to reach the 50th position wins the race. When one of the runners finishes the race, the application declares that runner to be the winner and the other runner concedes. The user wll have the option to race again. You will need to create a track for each runner. Each track should be represented by an Array with the Index Variable used to represent the position of the runner.   You will need a Loop. The runners will move at different speeds, so you will not know how many times the loop will repeat. You should implement a method to move the runners. The parameters you provide will determine the return value. This method will determine if the runner will move or rest. If the runner is moving the return value will represent how far they moved. You should implement a method to print out the position of each runner each time through the loop. Sample output: The race is about to start! T------------------------------------------------
H------------------------------------------------

The race is under way!!
----T--------------------------------------------
-----------H-------------------------------------

Tortoise has won the Race!!! ------------------------------------------------T
-------------------------------------H----------- Would you like to race again(y/n)?

Hints To determine whether a runner should run or rest, calculate a random number between 1 and 100. Then have the runner rest if the number is less than or equal to the percentage of time that the runner rests. (Tortoise rests 0%, Hare rests 90%) Otherwise, the runner should run. (Tortoise runs 1 space per round, Hare can run 10 spaces per round if not resting)   Printing to the screen will get messy. Use several System.out.println() to clear the screen before printing the next race update. The program will probably run very fast and be difficult to follow. Use a delay of 1 second between each print. Thread.sleep(1000); // this will stop execution for 1 second

Explanation / Answer

Hello, this program can be done in a lot of ways; even without using the arrays. Anyways, I have done according to your requirements.

//Game.java file

import java.util.Scanner;

public class Game {

      /**

      * t_pos and h_pos are the current positions of the Tortoise and Hare

      */

      static int t_pos,h_pos;

      static Tortoise tortoise;

      static Hare hare;

      public static void main(String[] args) {

           

           

            play(); /*starting the game*/

      }

      public static void play(){

            /**

            * the method will starts the play, loop until the game is over, displays the winner

            * and prompts the user if they want to play again

            */

           

            /**

            * defining Tortoise and Hare objects

            */

            tortoise=new Tortoise();

            hare=new Hare();

            t_pos=1;

            h_pos=1;

           

            System.out.println("The race is about to start");

            tortoise.printTrack();

            hare.printTrack();

            while(t_pos != 50 && h_pos !=50){

                  System.out.println(" "); /*printing blank lines*/

                  t_pos=tortoise.move(); /*moving and getting the current position of tortoise*/

                  h_pos=hare.move();/*moving and getting the current position of hare*/

                  tortoise.printTrack(); /*displaying the tracks*/

                  hare.printTrack();

                  try { /*comment this part to skip the 1s break between each round; for testing*/

                        Thread.sleep(1000);

                  } catch (InterruptedException e) {

                       

                        e.printStackTrace();

                  }

            }

            System.out.println(" Race Over");

            if(t_pos==50 && h_pos==50){

                  System.out.println("Its a tie");

            }

            else if(t_pos==50){

                  System.out.println("Tortoise wins");

            }else if(h_pos==50){

                  System.out.println("Hare wins");

            }

            System.out.println("Do you want to play again? (y/n)");

            Scanner scanner=new Scanner(System.in);

            String ch=scanner.next();

            if(ch.equalsIgnoreCase("y")){

                  play();

            }else if(ch.equalsIgnoreCase("n")){

                  System.out.println("Thanks for playing, Goodbye");

            }else{

                  System.out.println("Invalid choice, quitting..");

            }

      }

}

//Tortoise.java

public class Tortoise {

      /**

      * the current position of the tortoise

      */

      int position;

      /**

      * track array

      */

      char[] track;

      /**

      * speed of tortoise

      */

      int speed=1;

      public Tortoise() {

            position=0;

            track=new char[50];

            for(int i=0;i<track.length;i++){

                  /**

                  * filling the track

                  */

                  track[i]='-';

            }

           

      }

      public int move(){

            if(position<track.length){

                  position=position+speed;

            }

            return position+1;

      }

      public void printTrack(){

            /**

            * the current position of tortoise will be displayed by 'T' everything else will be '-'

            */

            System.out.println();

            for(int i=0;i<track.length;i++){

                  if(i==position){

                        System.out.print('T');

                  }else{

                        System.out.print(track[i]);

                  }

            }

      }

     

     

}

//Hare.java

import java.util.Random;

public class Hare {

      int position;

      int speed=10;

      char[] track;

      /**

      * Random object to generate a random number

      */

      Random random;

      /**

      * resting percent denotes how much time Hare will be resting

      */

      int resting_percent=90;

      public Hare() {

            position=0;

            track=new char[50];

            for(int i=0;i<track.length;i++){

                  track[i]='-';

            }

            random=new Random();

      }

      public int move(){

            int n=random.nextInt(100-1)+1; /*generating a random number between 1 and 100*/

            if(n<=resting_percent){

                  /**

                  * at rest; will not move, returns the current position.

                  */

                  return position;

            }else{

                  /**

                  * not resting..

                  */

                  if(position<track.length){

                        if(position+speed>=track.length){

                              position=track.length-1;

                        }else{

                              position=position+speed;

                        }

                       

                  }

                  return position+1;

            }

           

      }

      public void printTrack(){

            System.out.println();

            for(int i=0;i<track.length;i++){

                  if(i==position){

                        System.out.print('H');

                  }else{

                        System.out.print(track[i]);

                  }

            }

      }

}

/*Output (partial and random)*/

The race is about to start

T-------------------------------------------------

H-------------------------------------------------

-T------------------------------------------------

H-------------------------------------------------

--T-----------------------------------------------

H-------------------------------------------------

---T----------------------------------------------

----------H---------------------------------------

.

.

.

.

-----------------------------------------------T--

-------------------------------------------------H

Race Over

Hare wins

Do you want to play again? (y/n)

y

The race is about to start

T-------------------------------------------------

H-------------------------------------------------

-T------------------------------------------------

H-------------------------------------------------

.

.

.

.

-----------------------------------------------T--

--------------------H-----------------------------

------------------------------------------------T-

--------------------H-----------------------------

-------------------------------------------------T

--------------------H-----------------------------

Race Over

Tortoise wins

Do you want to play again? (y/n)

n

Thanks for playing, Goodbye