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
For safety, the lights on your vehicle must be in good working condition. Which statement about
bija089 [108]
I think the answer is a but I am not for sure
7 0
3 years ago
Read 2 more answers
LAB: Parsing dates Complete main() to read dates from input, one date per line. Each date's format must be as follows: March 1,
Vladimir [108]

The python code which can be used to read dates from input, one date per line and each date's format must be as follows: March 1, 1990, is:

<h3>Python code</h3>

import datetime

inputs = []

result = []

#read the inputs

date = input()

inputs.append(date)

while not date == "-1":

 date = input()

 inputs.append(date)

#check if the input is in the correct format and convert it.

for date_text in inputs:

 try:

   date_text = datetime.datetime.strptime(date_text,"%d %B, %Y")

   result.append(date_text.strftime("%d/%m/%Y"))

 except ValueError:

   pass

   

print(*result, sep = "\n")

The above code would parse the string and extract the date. The split() method was used to break the input into tokens.

Read more about python programming here:

brainly.com/question/27666303

#SPJ1

6 0
2 years ago
How to delete the last element in array
svlad2 [7]

Answer:

Hey mate.....

Explanation:

This is ur answer.....

<em>To remove the last n elements from an array, use arr. splice(-n) (note the "p" in "splice"). The return value will be a new array containing the removed elements.</em>

<em />

Hope it helps!

Mark me brainliest pls.....

FOLLOW ME! :)

4 0
2 years ago
owever, sitting in a corner of the store, a hacker had just set up an open "rogue" wireless hotspot posing as the coffee shop’s
Ivenika [448]

However, sitting in a corner of the store, a hacker had just placed up an open “rogue” wireless hotspot posing as the coffee shop’s wireless network. When Sarah logged onto her bank’s website, the hacker hijacked her session and gained access to her bank accounts. Another term for rogue wireless hotspots exists as “evil twin” hotspots.

<h3>What is meant by wireless network?</h3>

A computer network is referred to as a wireless network if radio frequency (RF) links are used to connect network nodes. A common solution for households, companies, and telecommunications networks is wireless networking. Wi-Fi is a wireless networking technology that enables printers and video cameras to connect to the Internet as well as computers (laptops and desktops), mobile devices (smart phones, wearables, etc.), and other devices.

Increased Mobility: Wireless networks give mobile users access to real-time data, enabling them to move freely across the premises of your firm without losing network connectivity. This improves collaboration and production across the entire organisation, which is not achievable with traditional networks.

Hence, However, sitting in a corner of the store, a hacker had just placed up an open “rogue” wireless hotspot posing as the coffee shop’s wireless network. When Sarah logged onto her bank’s website, the hacker hijacked her session and gained access to her bank accounts. Another term for rogue wireless hotspots exists as “evil twin” hotspots.

To learn more about wireless network refer to:

brainly.com/question/26956118

#SPJ4

6 0
1 year ago
Which icon is greater in file size? (Show your working)
nadya68 [22]

Answer:

the 256 color icon would be greater in file size.

Explanation:

regardless of how many pixels are in the image, file A has 256 colors meaning the computer has to individually load each one of those colors. it'll probably use a lot of ink if you decide to print it, too.

5 0
3 years ago
Other questions:
  • Sahil is making a graphic novel in which the villain plans to reduce the rate at which energy is released from the Sun. The vill
    7·2 answers
  • Which windows tools would you use to browse the files system on a hard drive?
    6·2 answers
  • The Magic Square is a grid with 3 rows and 3 columns with the following properties: • The grid contains every number from 1 to 9
    9·1 answer
  • Which of the following can be considered beta testing? A programmer at Linus Systems checks the integration of multiple modules
    14·1 answer
  • What type of undocumented yet benign hidden feature launches after a special set of commands, key combinations, or mouse clicks?
    15·1 answer
  • It is a SQL keyword that creates an autoincrementing field.
    5·1 answer
  • Which of the following is a key difference between a Windows 7 restore point and one you set yourself?
    11·1 answer
  • In which situation would saving a Word document as a PDF be most useful?
    9·2 answers
  • Which tool can be used to increase the space between a bullet point or a number and text?
    11·2 answers
  • What are the benefits and drawbacks of a desktop utilising virtualisation and a server?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!