Question

In Java: I got most of the logic down but I'd like to seen an answer that actually prints out the T and H and moves them or those something with an image since I only print out the winner and responses with my own code.

This project involves writing a program to simulate a tortoise and hare race. The contenders will each race along a horizontal course that contains at least 50 squares. You may add more if you wish. The race begins with each contender at square 1. The contender that first reaches or passes the last square of the course is the winner of the race. The following table indicates the types of moves that each contender can make Contender Type of Move Time % age Result of Move Tortoise Fast Plo 50 % 3 squares to right Slow 30 % 1 squares to right Slip 20 % 6 squares to left HareBig Hop 20 % 9 squares to right Small h Big Slip Small Sli Fall a sleep 10% 20% …… 20 % 1 squares to right 12 squares to left 2 squares to left Each contender starts at position 1. When a contender slips, they cant slip any further left than position 1. You will use a random number generator to simulate the percentages of each type of move indicated in the table. Generate a random integer, n, in the range 1 sns 10. For the tortoise, perform a fast plod if the number is 1-5, a slow plod if the number is 6-8, and a slip if the number is 9-10. For the hare, perform a big hop if the number is 1-2, a small hop if the number is 3-5, a big slip if the number is 6, a small slip if the number is 7-8, and fall asleep if the number is 9-10. There are a number of ways to design this program. One way would be to have a looping construct be the overall controller of things. Each iteration would adjust the contender positions, and the loop would terminate when one of the contenders reaches the last square of the race course. You will decide on an approach as part of your design step You must keep track of each contenders position and display it each time positions change. Show the letter T in the position of the tortoise, and the letter H in the position of the Hare. It is possible for the contenders to land on the same square. When this happens, the tortoise bites the hare, and your program should display OUCHIl beginning at that square. All output positions other than the T, the H, and the OUCH should be blank. If the tortoise wins, display TORTOISE WINSII. If the hare wins, display HARE WINSII. If the race is a tie, display ITS A TIEll. At the beginning of the race, display AND THEYRE OFFIl You must use an object oriented approach to designing and implementing this program. Be sure to comment your code and use good programming style

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question along with output. Please rate the answer if it helped. Thank you.

Hare.java

public class Hare {

   private int position;

   private int max_position;

   public Hare(int max)

   {

       position = 1;

       max_position = max;

   }

  

   public int getPosition()

   {

       return position;

   }

  

   private void move(int x)

   {

       position += x;

      

       //make sure position is in range 1-max_position

       if(position < 1)

           position = 1;

       else if(position > max_position)

           position = max_position;

   }

  

   public void bigHop()

   {

       move(9);

   }

  

   public void smallHop()

   {

       move(1);

   }

  

   public void bigSlip()

   {

       move(-12);

   }

  

   public void smallSlip()

   {

       move(-2);

   }

  

   public boolean finishedRace()

   {

       return position == max_position;

   }

  

}

Tortoise.java

public class Tortoise {

   private int position;

   private int max_position;

   public Tortoise(int max)

   {

       position = 1;

       max_position = max;

   }

  

   public int getPosition()

   {

       return position;

   }

  

   private void move(int x)

   {

       position += x;

      

       //make sure position is in range 1-max_position

       if(position < 1)

           position = 1;

       else if(position > max_position)

           position = max_position;

   }

  

   public void fastPlod()

   {

       move(3);

   }

  

   public void slowPlod()

   {

       move(1);

   }

  

   public void slip()

   {

       move(-6);

   }

  

   public boolean finishedRace()

   {

       return position == max_position;

   }

  

}

Race.java

import java.util.Random;

public class Race {

   private static void printTrack(int tick, int length, Hare hare, Tortoise tortoise)

   {

       System.out.printf("\n Tick %3d: ", tick);

      for( int i= 1; i <= length; i++)

      {

      if(i == hare.getPosition() && hare.getPosition() == tortoise.getPosition())

         System.out.printf("[OUCH!!] ");

      else if(i == hare.getPosition())

         System.out.printf("[H]");

      else if(i == tortoise.getPosition())

         System.out.printf("[T]");

      else

         System.out.printf("[]");

  

      }

      System.out.printf(" \n");

   }

  

  

   public static void main(String[] args) {

       int track_length = 50;

       Hare hare = new Hare(track_length);

       Tortoise tortoise = new Tortoise(track_length);

       int tick = 1;

       Random random = new Random(System.currentTimeMillis());

       System.out.println("AND THEY'RE OFF!!");

       printTrack(tick, track_length, hare, tortoise);

      

       while(!hare.finishedRace() && !tortoise.finishedRace())

       {

           tick++;

           int n = 1 + random.nextInt(10);

          

           //generate move for hare

      if(1 <= n && n <=2) //20 % big hop

         hare.bigHop();

      else if(3<=n && n <=5) //30% small hop

      hare.smallHop();

      else if(n == 6) //10% , big slip,

      hare.bigSlip();

      else if(7<=n && n<=8) //20% small slip

      hare.smallSlip();

      //no move if 9 and 10 , harre is asleep

  

      //generate move for tortoise

  

      if(1 <= n && n <=5) //50 % fast plod

      tortoise.fastPlod();

      else if(6<=n && n <=8) //30% slow plod

      tortoise.slowPlod();

      else //20% , slip

      tortoise.slip();

      printTrack(tick, track_length, hare, tortoise);

  

       }

      

       if(tortoise.finishedRace())

           if(hare.finishedRace())

               System.out.println("IT'S A TIE");

           else

               System.out.println("TORTOISE WINS!!");

       else

           System.out.println("HARE WINS!!");

   }

}

output

AND THEY'RE OFF!!

Tick 1: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 2: [][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 3: [T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 4: [H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 5: [][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 6: [][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 7: [H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 8: [H][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 9: [][H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 10: [H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 11: [][][][][][][][][][OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 12: [][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 13: [T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 14: [][][][T][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 15: [T][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 16: [][][][T][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 17: [H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 18: [][][][][][][][T][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 19: [H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 20: [H][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 21: [][H][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 22: [][][][][][][][][][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 23: [][][][][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 24: [H][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 25: [H][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 26: [H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 27: [H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 28: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 29: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 30: [H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 31: [][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 32: [H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 33: [][H][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 34: [H][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 35: [][][][][][][][][][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 36: [][][][][][][][][][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 37: [][][][][][][][][][][][][][][][][][][T][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 38: [][][][][][][][][][][][][][][][][][H][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 39: [][][][][][][][][][][][][][][][][][][H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 40: [][][][][][][H][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 41: [][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][]

Tick 42: [][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]

Tick 43: [][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]

Tick 44: [][][][][][][][][][][][][][][H][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][]

Tick 45: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]

Tick 46: [][][][][][][][][][][][][][H][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][]

Tick 47: [][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]

Tick 48: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]

Tick 49: [][][][][][][][][][][][][][][][][][][][][H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][]

Tick 50: [][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]

Tick 51: [][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]

Tick 52: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]

Tick 53: [][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][]

Tick 54: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][]

Tick 55: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]

Tick 56: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]

Tick 57: [][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][]

Tick 58: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][]

Tick 59: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]

Tick 60: [][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]

Tick 61: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][]

Tick 62: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]

Tick 63: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]

Tick 64: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]

Tick 65: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]

Tick 66: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]

Tick 67: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]

Tick 68: [H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][]

Tick 69: [][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][]

Tick 70: [][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]

Tick 71: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][]

Tick 72: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]

Tick 73: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]

Tick 74: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]

Tick 75: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]

Tick 76: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][]

Tick 77: [][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]

Tick 78: [][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][T][][][][][][][][][][][][][][][]

Tick 79: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][T][][][][][][][][][][][][]

Tick 80: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][T][][][][][][][][][]

Tick 81: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][T][][][][][][]

Tick 82: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][T][][][]

Tick 83: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][T]

TORTOISE WINS!!

Add a comment
Know the answer?
Add Answer to:
In Java: I got most of the logic down but I'd like to seen an answer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program to recreate the classic race between the hare and the tortoise. Each animal...

    Create a program to recreate the classic race between the hare and the tortoise. Each animal will move across the screen left to right over the course of 70 squares from 1 - 70 (each will have their own variable valued 1 - 70 and will print as an H or T) . Random numbers will determine the amount each animal will move forward or backward. You will write a program calls functions that uses pointers and pass by reference...

  • I need just homework#6, I post homework# 5 to understand how to do it ? Please...

    I need just homework#6, I post homework# 5 to understand how to do it ? Please do it and please don't copy the answer CSCE 1030 Homework 5 Due: 11:59 PM on Monday, April 17,2017 PROGRAM DESCRIPTION: In this C++ program, you will start building the software infrastructure that will allow you to play a simplified version of the class game of Minesweeper that will display to the screen. In Homework 6, you will continue the work begun here to...

  • I need help with my programming assignment. The language used should be java and the algorithm...

    I need help with my programming assignment. The language used should be java and the algorithm should use search trees so that you play against the computer and he chooses the best move. The tree should have all possibilities on the leaves and you could use recursion to so that it populates itself. The game can be a 3*3 board (no need the make it n*n). Please put comments so that I can understand it. Thanks The game of ‘Walls’...

  • INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first....

    INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • Java question for two classes: Given the upper limit n as a parameter, the result of...

    Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...

  • (Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of...

    (Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of its (x, y) location. All walkers start at the coordinates (0, 0). When a walker is asked to move, it randomly moves either left, right, up or down. Each of these four moves should occur with equal probability. The resulting behavior is known as a "random walk." (A 2-dimensional random walk example is pictured at right.) Each RandomWalker object should have the following public...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int n;       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT