Activity 10.20.1.
Complete method simulate below. You must use
hopDistance appropriately to receive full credit.
FrogSimulation class. You will write two of the methods in this class.
public class FrogSimulation
{
/** Distance, in inches, from the starting position to the goal. */
private int goalDistance;
/** Maximum number of hops allowed to reach the goal. */
private int maxHops;
/** Constructs a FrogSimulation where dist is the distance, in inches, from the starting
* position to the goal, and numHops is the maximum number of hops allowed to reach the goal.
* Precondition: dist >= 0; numHops >= 0
*/
public FrogSimulation(int dist, int numHops)
{
goalDistance = dist;
maxHops = numHops;
}
/** Returns an integer representing the distance, in inches, to be moved when the frog hops.
*/
private int hopDistance()
{ /* implementation not shown */ }
/** Simulates a frog attempting to reach the goal as described in part (a).
* Returns true if the frog successfully reached or passed the goal during the simulation;
* false otherwise.
*/
public boolean simulate()
{ /* to be implemented in part (a) */ }
/** Runs num simulations and returns the proportion of simulations in which the frog
* successfully reached or passed the goal.
* Precondition: num > 0
*/
public double runSimulations(int num)
{ /* to be implemented in part (b) */ }
}
FrogSimulation sim = new FrogSimulation(24, 5);
hopDistance appropriately to receive full credit.
StringChecker sc1 = new CodeWordChecker(5, 8, "$");
StringChecker sc2 = new CodeWordChecker("pass");
CodeWordChecker class. Your implementation must meet all specifications and conform to all examples.
public class WordPair
{
/** Constructs a WordPair object. */
public WordPair(String first, String second)
{ /* implementation not shown */ }
/** Returns the first string of this WordPair object. */
public String getFirst()
{ /* implementation not shown */ }
/** Returns the second string of this WordPair object. */
public String getSecond()
{ /* implementation not shown */ }
}
public class WordPairList
{
/** The list of word pairs, initialized by the constructor. */
private ArrayList<WordPair> allPairs;
/** Constructs a WordPairList object as described in part (a).
* Precondition: words.length >= 2
*/
public WordPairList(String[] words)
{ /* to be implemented in part (a) */ }
/** Returns the number of matches as described in part (b).
*/
public int numMatches()
{ /* to be implemented in part (b) */ }
}
WordPairList class. The constructor takes an array of strings words as a parameter and initializes the instance variable allPairs to an ArrayList of WordPair objects.
String[] wordNums = {"one", "two", "three"}; WordPairList exampleOne = new WordPairList(wordNums);
("one", "two"), ("one", "three"), ("two", "three")
String[] phrase = {"the", "more", "the", "merrier"}; WordPairList exampleTwo = new WordPairList(phrase);
("the", "more"), ("the", "the"), ("the", "merrier"), ("more", "the"), ("more", "merrier"), ("the", "merrier")
String[] moreWords = {"the", "red", "fox", "the", "red"}; WordPairList exampleThree = new WordPairList(moreWords);
("the", "red"), ("the", "fox"), ("the", "the"), ("the", "red"), ("red", "fox"), ("red", "the"), ("red", "red"), ("fox", "the"), ("fox", "red"), ("the", "red")
exampleThree.numMatches() should return 2.
ArrayTester.
public class ArrayTester
{
/**
* Returns an array containing the elements of column c of arr2D in the same
* order as they appear in arr2D. Precondition: c is a valid column index in
* arr2D. Postcondition: arr2D is unchanged.
*/
public static int[] getColumn(int[][] arr2D, int c)
{
/* to be implemented in part (a) */
}
/**
* Returns true if and only if every value in arr1 appears in arr2.
* Precondition: arr1 and arr2 have the same length. Postcondition: arr1 and
* arr2 are unchanged.
*/
public static boolean hasAllValues(int[] arr1, int[] arr2)
{
/* implementation not shown */
}
/** Returns true if arr contains any duplicate values; false otherwise. */
public static boolean containsDuplicates(int[] arr)
{
/* implementation not shown) */
}
/**
* Returns true if square is a Latin square as described in part (b); false
* otherwise. Precondition: square has an equal number of rows and columns.
* Precondition: square has at least one row.
*/
public static boolean isLatin(int[][] square)
{
/* to be implemented in part (b) */
}
}
getColumn, which returns a one-dimensional array containing the elements of a single column in a two-dimensional array. The elements in the returned array should be in the same order as they appear in the given column. The notation arr2D [r][c] represents the array at row r and column c.
getColumn method.
int [] [] arr2D = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 5, 3 }};
int[] result = ArrayTester.getColumn (arr2D, 1);
result will have the following contents. result: {1, 4, 7, 5}
getColumn below.
isLatin, which returns true if a given two-dimensional square array is a Latin square, and otherwise, returns false.

ArrayTester class provides two helper methods: containsDuplicates and hasAllValues. The method containsDuplicates returns true if the given one-dimensional array arr contains any duplicate values and false otherwise. The method hasAllValues returns true if and only if every value in arr1 appears in arr2. You do not need to write the code for these methods.

isLatin below. Assume that getColumn works as specified, regardless of what you wrote in part (a). You must use getColumn, hasAllValues, and containsDuplicates appropriately to receive full credit.
isLatin below.