1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
skelet666 [1.2K]
3 years ago
7

Tortise and hare race java g Modify the main class so it runs the race 100 times and reports how many times each runner wins. (T

o make the application run faster, you may want to reduce the sleep time in the runner threads.)
Computers and Technology
1 answer:
SOVA2 [1]3 years ago
3 0

Answer:

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("\n\n\n"); /*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("\nRace 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

Explanation:

You might be interested in
You manage a small LAN for a branch office. The branch office has three file servers and few client workstations. You want to us
lana66690 [7]

You manage small LAN for a branch office. The branch office has three file servers and few client workstations. You want to use Ethernet device and offer guaranteed bandwidth to each server. You design the network by <u>connecting all network devices to a switch. Connect each server to its own switch port.</u>

<u></u>

Explanation:

  • A local-area network (LAN) is a computer network that spans a relatively small area.
  • Most often, a LAN is confined to a single room, building or group of buildings, however, one LAN can be connected to other LANs over any distance via telephone lines and radio waves.
  • The LAN is the networking infrastructure that provides access to network communication services and resources for end users and devices spread over a single floor or building.
  • Designing a LAN for the campus use case is not a one-design-fits-all proposition.
  • If there is a single 48-port switch, 47 devices can be supported, with only one port used to connect the switch to the rest of the network, and only one power outlet needed to accommodate the single switch

8 0
3 years ago
The excessive use of the internet and the web for personal use at work is sometimes called ____ .
Elodia [21]
This is called Cyberslacking
7 0
3 years ago
Read 2 more answers
A reaction between an acid and a base is both a neutralization reaction and what other kind of reaction?
astraxan [27]
Hi!

The answer is C. Double replacement.

Have a nice day!
7 0
3 years ago
as the reader, you can figure out the writer's ____ based on his or her word choice and sentence structure a. purpose b. audienc
Misha Larkins [42]

Answer:

my choice purpose

Explanation:

the writer simply writes with a purpose to make his audience understand it's with the purpose that he writes

8 0
3 years ago
How often should you change your car's oil?
allsm [11]

Most cars can go at least 5,000 miles before an oil change. “Change your car's engine oil every three months, or 3,000 miles.

6 0
3 years ago
Read 2 more answers
Other questions:
  • Suppose a worker needs to process 100 items. the time to process each item is exponentially distributed with a mean of 2 minutes
    5·1 answer
  • 2. What type of expansion card allows your computer to
    12·1 answer
  • Mobile phones that function as credit cards are called _____.
    11·1 answer
  • Don is visiting digital stores and searching the internet to learn about high-definition DVD players before he buys one. This is
    8·1 answer
  • Given an integer n and an array a of length n, your task is to apply the following mutation to an: Array a mutates into a new ar
    5·1 answer
  • Which communication technology should you use if you need to connect three offices which are all within 3 miles of each other at
    6·1 answer
  • When parallel parking, you should select a space that is __________ as long as your car.
    11·1 answer
  • Why must you be careful when handling a hard drive?
    10·1 answer
  • A reputable, world-renowned auction house uses blockchain to verify the authenticity of paintings prior to placing them up for s
    7·1 answer
  • What Microsoft feature enables you to represent text as colorful visuals
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!