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

I have a mini project to simulate a tortoise and hare race, I wrote 4 seperate c

ID: 3723460 • Letter: I

Question

I have a mini project to simulate a tortoise and hare race, I wrote 4 seperate calss, but not sure how can make together.

//Contender class will display Tortoise and Hare positions

public abstract class Contender
    {
    protected int position;

    public void setPosition(int s){
        position = s;
    }
    public int getPosition(){
        return position;
    }
}

//Information provided by the table for Hare contender
//Each position is either 9 or 1 square to the right or 12 or 2 to the left
//Extended Contender class for program position display

public class Hare extends Contender {
        private int position;

        public void setPosition(int s){
            position = s;
        }

        public int getPosition(){
            return position;
        }
//Big hop 9 squares to the right
        public void BigHop(){
            position += 9;
        }
//Small hop 1 squares to the right
        public void SmallHop(){
            position += 1;
        }
//Big Slip 12 squares to the left
        public void BigSlip(){
            if (position > 12){
                position -= 12;
            }
            else{
                position = 1;
            }
        }
//Small Slip 2 squares to the left
        public void SmallSlip(){
            if (position > 2){
                position -= 2;
            }
            else{
                position = 1;
            }
        }
//Fall asleep
        public void FallAsleep(){
            position = position;
        }
    }

//Information provided by the table for Tortoise contender
//Each position is either 3 or 1 square to the right or 6 to the left
//Extended Contender class for program position display

public class Tortoise extends Contender{
    private int position;

    public void setPosition(int s){
        position = s;
    }

    public int getPosition(){
        return position;
    }

//Fast plod 3 squares to the right
    public void FastPlod(){
        position += 3;
    }

//Slow plod 1 square to the right
    public void SlowPlod(){
        position += 1;
    }

//Slip 6 squares to the left
    public void Slip(){
        if (position > 6){
            position -= 6;
        }
        else{
            position = 1;
        }
    }
}

//Race executes program

public class Race1 {
   
private static void printTrack(Contender contender, char symbol) {
    int position = contender.getPosition();
   
for (int i = 0; i < position; i++)
        System.out.print(' ');

    System.out.print(symbol);

//Track 50 positions

    for (int i = position + 1; i < 50; i++)
        System.out.print(' ');

    System.out.println();
}


public static void main(String[] args){
        Hare myHare = new Hare();
        Tortoise myTortoise = new Tortoise();

//Set Tortoise and Hare at position 1

        myHare.setPosition(1);
        myTortoise.setPosition(1);

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

//Use math random integer with switch statement to determine the type of move

        while (myHare.getPosition() > 0){
            int n = (int)(Math.random()*10);
            switch (n){
                case 1:
                    myHare.BigHop();
                    myTortoise.FastPlod();
                break;
                case 2:
                    myHare.BigHop();
                    myTortoise.FastPlod();
      break;
                case 3:
                    myHare.SmallHop();
                    myTortoise.FastPlod();
      break;
                case 4:
                    myHare.SmallHop();
                    myTortoise.FastPlod();
      break;
                case 5:
                    myHare.SmallHop();
                    myTortoise.FastPlod();
      break;
                case 6:
                    myHare.BigSlip();
                    myTortoise.SlowPlod();
      break;
                case 7:
                    myHare.SmallSlip();
                    myTortoise.SlowPlod();
      break;
                case 8:
                    myHare.SmallSlip();
                    myTortoise.SlowPlod();
      break;
                case 9:
                    myHare.FallAsleep();
                    myTortoise.Slip();
      break;
                case 10:
                    myHare.FallAsleep();
                    myTortoise.Slip();
      break;
            }
//Display H or T with blank spaces

  printTrack(myHare, 'H');
printTrack(myTortoise, 'T');

            if (myHare.getPosition() == myTortoise.getPosition()){
                System.out.println("OUCH!!");
            }
            if (myHare.getPosition() == 50 || myTortoise.getPosition() == 50){
                if (myHare.getPosition() == 50){
                    System.out.println("HARE WINS!!");
                    break;
                }
                else{
                    if (myHare.getPosition() == myTortoise.getPosition()){
                        System.out.println("IT'S A TIE!!");
                        break;
                    }
                    else{
                        System.out.println("TORTOISE WINS!!");
                         break;
                    }
                }
            }
        }

   }
}

Explanation / Answer


I have made some design changes to your classes so that they fall in place well. Hope it makes sense.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Contender.java
------------
public abstract class Contender
{
protected char symbol;
protected int position;
public Contender(char sym)
{
symbol = sym;
}
public void setPosition(int s){
position = s;
}
public int getPosition(){
return position;
}
  
public char getSymbol(){
return symbol;
}
public abstract void move();
}


Hare.java
---------
import java.util.Random;

//Information provided by the table for Hare contender
//Each position is either 9 or 1 square to the right or 12 or 2 to the left
//Extended Contender class for program position display

public class Hare extends Contender {
private int position;

public Hare()
{
super('H');
}
public void setPosition(int s){
position = s;
}

public int getPosition(){
return position;
}
//Big hop 9 squares to the right
public void BigHop(){
position += 9;
}
//Small hop 1 squares to the right
public void SmallHop(){
position += 1;
}
//Big Slip 12 squares to the left
public void BigSlip(){
if (position > 12){
position -= 12;
}
else{
position = 1;
}
}
//Small Slip 2 squares to the left
public void SmallSlip(){
if (position > 2){
position -= 2;
}
else{
position = 1;
}
}
//Fall asleep
public void FallAsleep(){
position = position;
}

@Override
public void move() {
int r = (int)(Math.random() * 10);
if(r >= 1 && r <=2) //20 %
BigHop();
else if(r >= 3 && r <= 5) // 30 %
SmallHop();
else if(r == 6 ) // 10%
BigSlip();
else if(r == 7 || r == 8) // 20 %
SmallSlip();
else if (r== 9 || r == 0) // 20 %
FallAsleep();
}
}


Tortoise.java
==============
//Information provided by the table for Tortoise contender
//Each position is either 3 or 1 square to the right or 6 to the left
//Extended Contender class for program position display

public class Tortoise extends Contender{
private int position;

public Tortoise()
{
super('T');
}
public void setPosition(int s){
position = s;
}

public int getPosition(){
return position;
}

//Fast plod 3 squares to the right
public void FastPlod(){
position += 3;
}

//Slow plod 1 square to the right
public void SlowPlod(){
position += 1;
}

//Slip 6 squares to the left
public void Slip(){
if (position > 6){
position -= 6;
}
else{
position = 1;
}
}
  
@Override
public void move() {
int r = (int)(Math.random() * 10);
if(r >= 1 && r <=5) //50 %
FastPlod();
else if(r >= 6 && r <= 8) // 30 %
SlowPlod();
else //20%
Slip();
}
}

Race1.java
----------

//Race executes program

public class Race1 {
private int trackLength;
private Contender tortoise;
private Contender hare;

public Race1(int len)
{
trackLength = len;
tortoise = new Tortoise();
hare = new Hare();
tortoise.setPosition(1);
hare.setPosition(1);
}


public void begin()
{
while(tortoise.getPosition() < trackLength && hare.getPosition() < trackLength)
{
tortoise.move();
hare.move();
printTrack();
}


if(tortoise.getPosition() == hare.getPosition())
System.out.println("IT'S A TIE!");
else if(tortoise.getPosition() >= trackLength)
System.out.println("TORTOISE WINS");
else
System.out.println("HARE WINS!");

}

private void printTrack() {
for(int i = 1; i <= trackLength; i++)
{
if(tortoise.getPosition() == hare.getPosition() && hare.getPosition() == i)
System.out.print("OUCH");
else if(tortoise.getPosition() == i)
System.out.print(tortoise.getSymbol());
else if(hare.getPosition() == i)
System.out.print(hare.getSymbol());
else
System.out.print(' ');
}

System.out.println();

for(int i = 1; i <= trackLength; i++)
System.out.print("-");
System.out.println();
}


public static void main(String[] args){
Race1 race = new Race1(50);
race.begin();
}
}


output
-----
H T   
--------------------------------------------------
H T
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H   
--------------------------------------------------
T H
--------------------------------------------------
T H
--------------------------------------------------
T H
--------------------------------------------------
T H
--------------------------------------------------
HT   
--------------------------------------------------
H T
--------------------------------------------------
H T
--------------------------------------------------
H T
--------------------------------------------------
TH
--------------------------------------------------
T H   
--------------------------------------------------
H T   
--------------------------------------------------
H T
--------------------------------------------------
H T   
--------------------------------------------------
H T   
--------------------------------------------------
H T
--------------------------------------------------
H T   
--------------------------------------------------
H T
--------------------------------------------------
H T   
--------------------------------------------------
H T
--------------------------------------------------
H T
--------------------------------------------------
H T   
--------------------------------------------------
H T
--------------------------------------------------
HT   
--------------------------------------------------
H T
--------------------------------------------------
T H
--------------------------------------------------
HARE WINS!