Question: Write a public instance method called performDance(), which takes no arguments and returns no value. The method should first check if any of the frogs

Write a public instance method called performDance(), which takes no arguments and returns no value.

The method should first check if any of the frogs are dancing. If there are no dancing frogs, the method should simply terminate; however, if there are one or more dancing frogs the method should proceed as follows:

First, the method should get the dancing frogs to do danceMove1.

Next, the method should check to see if numOfDanceMoves is greater than 1, and if it is the method should get the dancing frogs to do danceMove2.

Finally, the method should check to see if numOfDanceMoves is greater than 2, and if it is the method should get the dancing frogs to do danceMove3.

This is my code so far

public class FrogDanceCoordinator { /** * instance variables */ private int numOfFrogsDancing; private int numOfDanceMoves;

private Frog frog1; private Frog frog2; private Frog frog3;

private String danceMove1; private String danceMove2; private String danceMove3;

/** * Constructor for objects of class FrogDanceCoordinator. */ public FrogDanceCoordinator (Frog aFrog1, Frog aFrog2, Frog aFrog3) { super(); this.frog1 = aFrog1; this.frog2 = aFrog2; this.frog3 = aFrog3; this.numOfFrogsDancing = 0; this.numOfDanceMoves = 0; }

/* instance methods */

/** * Returns true if the argument is in the range 1 to 3 (inclusive), * otherwise false. */ public boolean isValidNumOfMoves(int aNumber) { return ((aNumber >= 1) && (aNumber <= 3)); }

/** * -- TO BE MODIFIED -- * Prompts the user for a number of dance moves * in the range 1 to 3 inclusive, and returns this number. */ public int promptForNumOfMoves() { String movesString; int moves = 0; boolean again = true; while (again) { movesString = OUDialog.request ("Please enter the number of dance moves to be performed - between 1 and 3 (inclusive)"); try { moves = Integer.parseInt(movesString); return moves; } catch (NumberFormatException anException) { OUDialog.alert("The number entered is not between 1 and 3(inclusive)"); } } return moves; } /** * Repeatedly prompts the user for a number of moves, until they enter * a valid input representing a number in the range 1 to 3 inclusive, and * then returns this number. */ public int getNumOfMoves() { int moves = this.promptForNumOfMoves(); while (!this.isValidNumOfMoves(moves)) { OUDialog.alert("That is not a valid number of dance moves"); moves = this.promptForNumOfMoves(); } return moves; } /** * Returns true if the argument is either "flip" or "slide", * otherwise false. */ public boolean isValidMove(String aMoveType) { return ((aMoveType.equals("flip")) || (aMoveType.equals("slide"))); }

/** * Prompts the user to enter the the name of a dance move which must be either "flip" or "slide". */ public String promptForDanceMove() { String danceMove = OUDialog.request ("Please enter type of dance move - flip or slide"); while (!this.isValidMove(danceMove)) { OUDialog.alert("This is not a valid type of move"); danceMove = OUDialog.request ("Please enter type of dance move - \"flip\" or \"slide\""); } return danceMove; } /** * 'Flip' a frog depending on its position */ private void flip(Frog aFrog) { aFrog.jump(); if (aFrog.getPosition() == 1) { aFrog.setPosition(11); } else { aFrog.setPosition(1); } aFrog.jump(); }

/** * 'Slide' a frog depending on its position */ private void slide(Frog aFrog) { if (aFrog.getPosition() == 1) { while (aFrog.getPosition() < 11) { aFrog.setPosition(aFrog.getPosition() + 1); } aFrog.jump(); } else { while (aFrog.getPosition() > 1) { aFrog.setPosition(aFrog.getPosition() - 1); } aFrog.jump(); } }

/** * 'Flip' all frogs that have been set for dancing */ private void flipDancingFrogs() { if (this.frog1.getColour().equals(OUColour.RED)) { this.flip(this.frog1); } if (this.frog2.getColour().equals(OUColour.RED)) { this.flip(this.frog2); } if (this.frog3.getColour().equals(OUColour.RED)) { this.flip(this.frog3); } }

/** * 'Slide' all frogs that have been set for dancing */ private void slideDancingFrogs() { if (this.frog1.getColour().equals(OUColour.RED)) { this.slide(this.frog1); } if (this.frog2.getColour().equals(OUColour.RED)) { this.slide(this.frog2); } if (this.frog3.getColour().equals(OUColour.RED)) { this.slide(this.frog3); } } /** * 1b */ public void selectDancingFrogs() { Frog[] frog = {frog1,frog2,frog3}; numOfFrogsDancing = 0; for (int i = 0; i { boolean result; result = OUDialog.confirm("Please confirm if you want Frog"+ i + " to dance"); if (result) { numOfFrogsDancing++; frog[i].setColour(OUColour.RED); frog[i].setPosition(1); } else { frog[i].setColour(OUColour.GREEN); } } } /** * 1c */ public int createDance() { String StringDanceMoves; StringDanceMoves = OUDialog.request("Please enter the number of dance moves"); numOfDanceMoves = Integer.parseInt(StringDanceMoves); danceMove1 = OUDialog.request("Please enter a move, flip or side"); if (numOfDanceMoves > 1) { danceMove2 = OUDialog.request("Please enter another dance move"); }

if (numOfDanceMoves > 2) { danceMove3 = OUDialog.request("Please enter another dance move"); } return numOfDanceMoves; } /** * di */ public void setUpDance() { this.selectDancingFrogs(); this.createDance(); } /** * */ public void performDance() {

Other classes are below

Frog class -

public class Frog extends Amphibian { /** * Constructor for objects of class Frog which initialises colour to green * and position to 1. */ public Frog() { super(OUColour.GREEN, 1); }

/* instance methods */

/** * Resets the receiver to its "home" position of 1. */ @Override public void home() { this.setPosition(1); }

/** * Decrements the position of the receiver by 1. */ @Override public void left() { this.setPosition(this.getPosition() - 1); }

/** * Increments the position of the receiver by 1. */ @Override public void right() { this.setPosition(this.getPosition() + 1); }

/** * Causes a change in an appropriate observing user interface. * Icon representing the receiver performs a jump animation */ public void jump() { this.performAction("jump"); } }

and amphibian class

public abstract class Amphibian extends OUAnimatedObject {

/* instance variables */ private OUColour colour; private int position;

/** * Constructor for objects of the abstract class Amphibian. */ public Amphibian(OUColour aColour, int aPosition) { super(); this.colour = aColour; this.position = aPosition; }

/* instance methods */

/** * Moves the receiver to the left. */ public abstract void left();

/** * Moves the receiver to the right. */ public abstract void right();

/** * Resets the receiver to its "home" position. */ public abstract void home();

/** * Returns the position of the receiver. */ public int getPosition() { return this.position; }

/** * Sets the position of the receiver to the value of the argument aPosition. */ public void setPosition (int aPosition) { this.position = aPosition; this.update("position"); }

/** * Sets the colour of the receiver to the argument's colour. */ public void sameColourAs(Amphibian anAmphibian) { this.setColour(anAmphibian.getColour()); }

/** * Returns the colour of the receiver. */ public OUColour getColour() { return this.colour; }

/** * Sets the colour of the receiver to the value of the argument aColour. */ public void setColour(OUColour aColour) { this.colour = aColour; this.update("colour"); }

/** * Sets the colour of the receiver to brown. */ public void brown() { this.setColour(OUColour.BROWN); }

/** * Sets the colour of the receiver to green. */ public void green() { this.setColour(OUColour.GREEN); }

/** * Causes user interface to emit a sound. */ public void croak() { this.performAction("croak"); }

/** * Returns a string representation of the receiver. */ @Override public String toString() { return "An instance of class " + this.getClass().getName() + ": position " + this.getPosition() + ", colour " + this.getColour(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!