+-------------------------+ ¦ 34 ¦ 21 ¦ 32 ¦ 41 ¦ 25¦ +----+----+----+----+-----
ID: 3635378 • Letter: #
Question
+-------------------------+¦ 34 ¦ 21 ¦ 32 ¦ 41 ¦ 25¦
+----+----+----+----+-----¦
¦ 14 ¦ 42 ¦ 43 ¦ 14 ¦ 31¦
+----+----+----+----+-----¦
¦ 54 ¦ 45 ¦ 52 ¦ 42 ¦ 23¦
+----+----+----+----+-----¦
¦ 33 ¦ 15 ¦ 51 ¦ 31 ¦ 35
+----+----+----+----+-----¦
¦ 21 ¦ 52 ¦ 33 ¦ 13 ¦ 23¦
+-------------------------+
Write a program to explore the above array for a treasure. The values in the array are clues. Each cell contains an integer between 11 and 55; for each value the ten's digit represents the row number and the unit's digit represents the column number of the cell containing the next clue.
Starting at the upper left corner(1, 1), use the clues to guide your search of the array. (The first three clues are 11, 34, 42). i.e. Cell (1, 1) contains 34, which means go to cell (3, 4). Cell (3, 4) contains 42 which means go to cell 4, 2.
The treasure is a cell whose value is the same as its coordinates.
-Your program must first read in the treasure map data into a 5 by 5 array.
-Your program should output the cells it visits during its search, and a message indicating where you found the treasure.
-You will need to use arrays, methods, and objects.
Explanation / Answer
Hope this helps, please rate! public class treasure { /** * @param args */ public static void main(String[] args) { //creating and initializing array int[][]array = new int[6][6]; array[1][1] = 34; array[1][2] = 21; array[1][3] = 32; array[1][4] = 41; array[1][5] = 25; array[2][1] = 14; array[2][2] = 42; array[2][3] = 43; array[2][4] = 14; array[2][5] = 31; array[3][1] = 54; array[3][2] = 45; array[3][3] = 52; array[3][4] = 42; array[3][5] = 23; array[4][1] = 33; array[4][2] = 15; array[4][3] = 51; array[4][4] = 31; array[4][5] = 35; array[5][1] = 21; array[5][2] = 52; array[5][3] = 33; array[5][4] = 13; array[5][5] = 23; //begin searching through the array for the treasure search(array,1,1); } public static void search(int[][]array, int i, int j){ System.out.println("Looking for treasure in "+ array[i][j]); if(array[i][j] == (i*10)+j){ System.out.println("Treasure found in "+ ((i*10)+j)); } else{ System.out.println("Not treasure found!"); String s = Integer.toString(array[i][j]); i = Integer.parseInt(s.substring(0,1)); j = Integer.parseInt(s.substring(1,2)); search(array,i,j); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.