Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 9 will use a 2D array of integers to represent a swamp. You will be give

ID: 3656038 • Letter: P

Question

Program 9 will use a 2D array of integers to represent a swamp. You will be given a swamp that has already been dimensioned and initialized. Every value of 0 represents quicksand while every value of 1 represents solid ground. You will also be given an x,y pair representing your starting position inside the swap on a block of solid ground. Your job will be to follow the trail of 1's (solid ground) to the edge of the swamp - or until it dead ends. You will take one step at a time in any direction as long as that step is onto solid ground ( 1 ), and not into quicksand ( 0 ). Put simply, you are dropped into the swamp onto a block of solid gound and starting there must follow the trail of safe steps one at a time until you either reach the edge of the swamp (rescued) or reach a dead end (stranded). You are guaranteed the following: There are no cycles in your path to freedom or dead end (thus no backtracking/recursion needed). You will start out on a block of solid ground. There is only one possible path to take, and thus only one possible correct output for any given swamp. The only modifications you are to make to the starter file are to fill in the code for onEdge() and nextStep(). Do not write any code that produces output. I have taken care of that to facilitate script grading. Your output must match mine exactly. Program9.java import java.io import java.util public class Program9 { public static void main(String[] args) { int myRow=2, myCol=3 // you are dropped into the swamp at: [2][3] int[][] swamp1 { { 0,0,0,0,0,0,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,1 } { 0,0,0,1,0,0,0,0,1,0 } { 0,0,0,1,0,0,0,1,0,0 } { 0,0,0,0,1,0,1,0,0,0 } { 0,0,0,0,1,0,0,1,0,0 } { 0,0,0,0,1,0,0,0,1,0 } { 0,0,0,0,0,1,0,1,0,0 } { 0,0,0,0,0,0,1,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } } printSwamp( "SWAMP1",swamp1 ); printEscapePath( swamp1, myRow,myCol ); // Now swamp2 myRow=2; myCol=8; // you are dropped into the swamp at [2][8] int[][] swamp2 = { { 0,0,0,0,0,0,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } { 0,0,1,0,0,0,0,1,1,0 } { 0,0,0,1,0,0,1,0,0,0 } { 0,0,0,0,1,1,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } { 0,0,0,0,0,0,0,0,0,0 } }; printSwamp( " SWAMP2",swamp2 ) printEscapePath( swamp2, myRow,myCol ) } // END MAIN // DO NOT MODIFY THIS METHOD - USE AS GIVEN private static void printSwamp(String label, int[][] swamp ) { System.out.println( label ); for(int r = 0; r < swamp.length; r++) { for(int c = 0; c < swamp[r].length; c++) System.out.print( swamp[r][c] + " ") System.out.println(); } } // DO NOT MODIFY THIS METHOD - USE AS GIVEN private static void printEscapePath(int[][] swamp, int myRow, int myCol) { int[] coords = new int[2] coords[0]=myRow; coords[1]=myCol; System.out.println("STARTING AT: " + "["+ myRow + "][" + myCol + "]"); while ( !onEdge( swamp, coords ) ) { // nextStep returns true if there IS a safe step, else returns false // If it returns true then before it returns, it marks the spot you are sitting on as -1 // then updates the x and y inside the coords to be the new coords of where it is stepping to. // You must mark your curr position as -1 so you don't keep going back and forth if ( nextStep( swamp, coords ) ) { System.out.println( "STEPPED TO: [" + coords[0] + "][" + coords[1] + "]" ); } else // YOU'RE CROC MEAT :=( { System.out.println( "STRANDED AT: [" + coords[0] + "][" + coords[1] + "]" ); return; } // END WHILE NOT OUT OF SWAMP YET // IF YOU MAKE IT HERE THEN YOU ARE ON THE EDGE .. FREEDOM!!! } System.out.println( "ESCAPED AT: [" + coords[0] + "][" + coords[1] + "]" ); } // END PRINT ESCAPE PATH // YOU MUST WRITE THIS METHOD // LOOKS AT EVERY ADJACENT SQUARE TO SEE IF IT'S A 1 // IF IT IS, THEN THE CURRENT X,Y IS CHANGED TO -1 (i.e. BEEN HERE) // AND THE COORDS ARE CHANGED TO THOSE OF THE NEARBY SQAURE WITH A 1 IN IT // IF NO SAFE STEP IS FOUND THEN JUST RETURN FALSE. DO NOT MODIFY COORDS private static boolean nextStep( int[][] swamp, int[] coords ) { // YOUR CODE HERE return false; // just to make it compile you edit as needed } // YOU MUST WRITE THIS METHOD // RETURNS TRUE IF YOUR CURR COORDS ARE ON THE FIRST OR LAST ROW OR COLUMN // i.e RETURNS TRUE IF YOU ARE ON THE EDGE/BORDER OF THE SWAMP // ELSE RETURNS FALSE private static boolean onEdge( int[][] swamp, int[] coords ) { return false; // just to make it compile you edit as needed } }

Explanation / Answer

Swamp #1 has an escape path from the drop in point to the edge. int myRow=2, myCol=3; // you are dropped into the swamp here: (in red below)int[][] swamp1 ={ { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,1 }, { 0,0,0,1,0,0,0,0,1,0 }, { 0,0,0,1,0,0,0,1,0,0 }, { 0,0,0,0,1,0,1,0,0,0 }, { 0,0,0,0,1,0,0,1,0,0 }, { 0,0,0,0,1,0,0,0,1,0 }, { 0,0,0,0,0,1,0,1,0,0 }, { 0,0,0,0,0,0,1,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 } }; Swamp #2 is a DEAD END. There is no way out since it dead ends before reaching the edge. int myRow=2, myCol=8; // you are dropped into the swamp here: (in red below) int[][] swamp2 = { { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,1,0,0,0,0,1,1,0 }, { 0,0,0,1,0,0,1,0,0,0 }, { 0,0,0,0,1,1,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0 } }; The only modifications you are to make to the starter file are to fill in the code for onEdge() and nextStep(). Do not write any code that produces output. I have taken care of that to facilitate script grading. Your output must match mine exactly.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote