Java Programing

profileAbdulwahed
treasure_hunt.java.txt

import java.util.Random; import java.util.Scanner; /* * First Last * MIS 4310 Fall 2017 * FINAL TAKE HOME EXAM */ public class treasure_hunt { public static void main(String[] args) { boolean found = false; // is true if the treasure is found int dug = 0; // holds the number of times user digs Random gen = new Random(9); Scanner scan = new Scanner(System.in); int locationRow; // stores the row where the treasure is int locationCol; // stores the column where the treasure is /* * Generates a String array that looks like * undug grass when printed to represent the backyard. */ String[][] backyard = new String[5][5]; for (int row = 0; row < backyard.length; row++) { for (int col = 0; col < backyard[row].length; col++) { backyard[row][col] = "^"; System.out.print(backyard[row][col] + " "); } // end inner loop System.out.println(); } // end outer loop /* * Generate a random number for both the row and column * location of the treasure. Store the results in * locationRow and locationCol, respectively. * The random numbers should be within the array dimensions. */ //************************** // YOUR CODE HERE (1 OF 7) //************************** /* * A while loop that runs as long as the treasure * is not found. */ while (found == false) { /* * Declare two integer variables, guessRow and guessCol * and set them both to -1. * Increment the variable dug to represent a dig. */ //************************** // YOUR CODE HERE (2 OF 7) //************************** /* * Ask the user which row they want to dig. * Use a while loop to validate the input. * Run the while loop as long as the guessRow * value is outside the bounds of the array. */ //************************** // YOUR CODE HERE (3 OF 7) //************************** /* * Ask the user which column they want to dig. * Use a while loop to validate the input. * Run the while loop as long as the guessCol * value is outside the bounds of the array. */ //************************** // YOUR CODE HERE (4 OF 7) //************************** /* * Use the if statement below to check if both the guessRow * and guessCol are both correct. If they are, set * found equal to true and print "You found the treasure!" * Then, mark the location by setting the array value * to an "X". Use a nested for loop to print the array * with the "X". */ //************************** // YOUR CODE HERE (5 of 7) (inside the if statement () and {}) //************************** if () { } // end if /* * The else statement for the if statement you wrote * is provided. Within the statement, you will see code * that guides the user using an if, if else, and else * statement block to the correct ROW. * * Using this block as your guide, write another block * of if, if else, and else that guides the user to the * correct COLUMN. * * Find the YOUR CODE HERE 6 and begin coding after. */ else { System.out.println("Only dirt at (" + guessRow + ", " + guessCol + ")"); if (guessRow == locationRow) { System.out.println("You're in the right row!"); } // end if else if (guessRow > locationRow ) { System.out.println("The treasure is to the north."); } // end else if else { System.out.println("The treasure is to the south."); } // end else //************************** // YOUR CODE HERE (6 of 7) //************************** /* * Set the backyard value at guessRow and guessCol * equal to the letter "O". Print the array afterward * using a nested for loop. */ //************************** // YOUR CODE HERE (7 of 7) //************************** } // end else } // end while /* * Prints how many digs it took the user to find the treasure. */ System.out.println("You found the treasure after " + dug + " digs."); } }