MIS 4310-01 Final Take Home Exam (100 points)
DUE: Thursday, December 14th, 2017 by 11:59 PM
Use the code provided on Blackboard as your template. Fill in your own code where
prompted. When finished, upload to Blackboard a file called “treasure_hunt.java”. Change
the block comment to include your own name. This is an individual assignment! Code that
you write that you cannot explain will result in an automatic zero.
Create a Java application that simulates a treasure hunt with a 2D array. Your output
should match the output below with similar input. Do NOT alter the code provided. Fill
in code in the SEVEN (7) locations under each comment // YOUR CODE HERE.
Sample Output:
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.
*/
...
Historical philosophical, theoretical, and legal foundations of special and i...
MIS 4310-01 Final Take Home Exam (100 points) DUE Thursd.docx
1. MIS 4310-01 Final Take Home Exam (100 points)
DUE: Thursday, December 14th, 2017 by 11:59 PM
Use the code provided on Blackboard as your template. Fill in
your own code where
prompted. When finished, upload to Blackboard a file called
“treasure_hunt.java”. Change
the block comment to include your own name. This is an
individual assignment! Code that
you write that you cannot explain will result in an automatic
zero.
Create a Java application that simulates a treasure hunt with a
2D array. Your output
should match the output below with similar input. Do NOT
alter the code provided. Fill
in code in the SEVEN (7) locations under each comment //
YOUR CODE HERE.
Sample Output:
import java.util.Random;
2. 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);
3. 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] + " ");
4. } // 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)
//**************************
5. /*
* 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)
6. //**************************
/*
* 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.
7. * 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".
8. */
//**************************
// 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
9. * 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!");
10. } // 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
//**************************
11. // 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
12. } // end while
/*
* Prints how many digs it took the user to find the
treasure.
*/
System.out.println("You found the treasure after " +
dug + " digs.");
}
}