0% found this document useful (0 votes)
65 views6 pages

Temple of Arrays FRQ - 2D Arrays - Answers

Uploaded by

aryanhalan076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Temple of Arrays FRQ - 2D Arrays - Answers

Uploaded by

aryanhalan076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Archeologist Nevada Smith is on an adventure in a mysterious temple.

He is in a room
with a floor that changes the symbols on its tiles every minute.
The floor is represented by a 2D array of strings, each representing a symbol on a tile.
For the floor to be safe to cross, the following conditions must be met:
1. There must be at least one straight line of tiles from the left side of the floor to the
right side, where all tiles in the line have the same symbol.
2. The floor must NOT contain either X or Y, which are "danger symbols."
3. Not all symbols on the floor can be the same.

A D D X Q
B B B B B
E C F D R
String[][] templeFloor =
{{"A", "D", "D", "X", "Q"},
{"B", "B", "B", "B", "B"},
{"E", "C", "F", "D", "R"}};

Temple of Arrays FRQ V1.1 - Barnum htps://www.youtube.com/@BillBarnum


public class TempleOfArrays
{
/**
* Checks if all symbols in a given line (1D array) are the same.
* Returns true if all symbols are the same, false otherwise.
* Precondition: line contains at least one symbol
* No element of line is null
* Each String in line has a length of 1
*/
public static boolean lineOfIdentical(String[] line)
{ /* To be completed in part (a) */ }

/**
* Determines if the floor is safe based on:
* 1. the absence of danger symbols (X or Y)
* 2. not all symbols are the same
* Returns true if safe, otherwise false
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean isFloorSafe(String[][] floor)
{ /* To be completed in part (b) */ }

/**
* Determines if Nevada Smith can safely cross the floor.
* returns true if the floor is passable, based on the three
* criteria, false otherwise.
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean canCrossFloor(String[][] floor)
{ /* To be completed in part (c) */ }
}

Temple of Arrays FRQ V1.1 - Barnum htps://www.youtube.com/@BillBarnum


(a) Write the lineOfIdentical method. This method takes an array of String
objects as a parameter, representing a row of tiles on the floor. Each String in the array
represents a symbol on a tile. The method returns true if all symbols in the array are the
same; otherwise, it returns false.

/**
* Checks if all symbols in a given line (1D array) are the same.
* Returns true if all symbols are the same, false otherwise.
* Precondition: line contains at least one symbol
* No element of line is null
* Each String in line has a length of 1
*/
public static boolean lineOfIdentical(String[] line)
{
for(int i = 0; i < line.length; i++)
{
if (!line[i].equals(line[0]))
{
return false;
}
}
return true;
}

Temple of Arrays FRQ V1.1 - Barnum htps://www.youtube.com/@BillBarnum


(b) Write the isFloorSafe method. This method takes a 2D array of String
objects, floor, where each String represents a symbol on a floor tile. The method
returns true if the floor is safe based on the conditions below; otherwise, it returns
false.

/**
* Determines if the floor is safe based on:
* 1. the absence of danger symbols (X or Y)
* 2. not all symbols are the same
* Returns true if safe, otherwise false
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean isFloorSafe(String[][] floor)
{
String firstSymbol = floor[0][0];
boolean allSymbolsSame = true;

for(String[] row : floor)


{
for(String symbol : row)
{
if(symbol.equals("X") || symbol.equals("Y"))
{
return false;
}
if (!symbol.equals(firstSymbol))
{
allSymbolsSame = false;
}
}
}
return allSymbolsSame == false;
}

Temple of Arrays FRQ V1.1 - Barnum htps://www.youtube.com/@BillBarnum


(c) Write the canCrossFloor method. This method takes a 2D array of String objects,
floor, representing the temple's floor, where each String represents a symbol on a tile. The
method returns true if there is a safe path across the floor that meets both of the following
conditions:
1. At least one row will return true when passed to the lineOfIdentical method.
2. The floor must be safe according to the isFloorSafe method.
Complete the canCrossFloor method below, which should utilize the lineOfIdentical and
isFloorSafe methods you implemented in parts (a) and (b).

/**
* Determines if Nevada Smith can safely cross the floor.
* returns true if the floor is passable, based on the three
* criteria, false otherwise.
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean canCrossFloor(String[][] floor)
{
if(isFloorSafe(floor) == false)
{
return false;
}

for(int i = 0; i < floor.length; i++)


{
if(lineOfIdentical(floor[i]))
{
return true;
}
}

return false;
}

Temple of Arrays FRQ V1.1 - Barnum htps://www.youtube.com/@BillBarnum


Temple of Arrays FRQ V1.1 - Barnum htps://www.youtube.com/@BillBarnum

You might also like