Question
Completing the snail class. Snail.java public class Snail { public static char icon = '@'; private int position; private QueueADT movePattern; public Snail (int[] pattern)
Completing the snail class.
Snail.java
public class Snail {
public static char icon = '@';
private int position;
private QueueADT movePattern;
public Snail (int[] pattern) {
}
public void move () {
}
public int getPosition () {
}
public void display () {
}
}
SnailRace.java
public class SnailRace {
private Snail[] participants;
public static int raceLength = 50;
public SnailRace () {
participants = new Snail[3];
participants[0] = new Snail(new int[] {1, 0, 1, 2, 1, 0});
participants[1] = new Snail(new int[] {2, 1, 1, 0});
participants[2] = new Snail(new int[] {0, 0, 3});
try {
startRace();
} catch (Exception e) {
System.out.println("An error occurred.");
}
}
public void startRace () throws InterruptedException {
boolean racing = true;
while (racing) {
for (int i = 0; i < participants.length; i++) {
participants[i].move();
}
for (int i = 0; i < participants.length; i++) {
if (participants[i].getPosition() == raceLength) {
racing = false;
}
}
display();
// Pause briefly to simulate the timing of the race.
Thread.sleep(500);
}
}
public void display () {
for (int i = 0; i < participants.length; i++) {
participants[i].display();
}
System.out.println(" ");
}
public static void main (String[] args) {
new SnailRace();
}
}
Instructions:
Constructor
- initialize the snail's position to 0
- initialize the snail's queue "movePattern"and add each of the step sizes from the int array to the snail's queue in the same order they come in the array
move() method
- Dequeue the first move pattern number and store it in a variable
- Re-enqueue this movement to the back of the queue so it will cycle around
- Increment the snail's position by the move pattern value
- Do not let the snail go past the finish line; after the movement, check its position compared to the race length (this is a variable in the SnailRace class so examine how the variable was made to determine how you can access it from here) and just leave it at the finish line if was going to be past the line
display() method
- create two int variables called "dashesBefore" and "dashesAfter" and assign them the values that represent the distance before and after the snail, i.e. when the snail is at the start, dashesBefore is 0 and dashesAfter is 50. If the snail moves up 1 space, dashesBefore is 1 and dashesAfter is 49, etc.
- use the dashesBefore value to help in displaying that number of dashes on a single line (use System.out.print() rather than .println() to avoid new lines)
- display the snail's shell using the icon variable (@)
- use the dashesAfter value to display the dashes to the right of the snail icon (all on one line still)
- finally print out a newline character so the next snail's path will be on a different line than this one
Questions: Consider the possibility of using an Array Queue or a Circular Array Queue instead of the Linked Queue being used in this simulation. Would the simulation's results be impacted by switching to a different Queue implementation? Which of these classes/methods would you have to modify if you were going to use one of the other queue implementations?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started