Question
Written in Java Consider an NX N grid in which some squares are occupied. Two squares belong to the same group if they share a
Written in Java
Consider an NX N grid in which some squares are occupied. Two squares belong to the same group if they share a common edge. In Figure 7.30 there is one group of four occupied squares, three groups of two occupied squares, and two individual occupied squares. Assume that the grid is represented by a two-dimensional array. Write a program that a. Computes the size of a group when a square in the group is given b. Computes the number of different groups c. Lists all groups I have code listed below what do i need to fix in order to get the sample output that is posted with this as well. Because i should be able to get that output.
Code:
//Import the needed packages import java.util.Set; import java.util.Scanner; import java.util.HashSet; //Class public class NxNGrid //Declare and initialize NxN grid private boolean[][] nxngrid = null; //Constructor public NxNGrid(boolean[][] inputgrid) //Initialize NxN grid nxngrid = inputgrid; l/Driver method main() public static void main(String[] args) //Declare and initialize grid indices int index1, index2 = 0; //Create a new Scanner Scanner sc = new Scanner (System.in); l/Prompt the user for square position System.out.print("Enter the square position in the group: "); //Read first index index1 = sc.nextInt(); //Read second index index2 = sc.nextInt(); //Declare and initialize Input NxN grid boolean[][] nxnGridData = {false, false, false, false, false, false, false, false, false, true}, {false, false, false, true, true, false, false, false, false, true}, {false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, true, false, false, true, false, false}, {false, false, false, true, false, false, false, true, false, false}, {false, false, false, false, false, false, false, true, true, false}, {false, false, false, false, true, true, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false} //Construct a new grid NxNGrid grid = new NxNGrid(nxnGridData); [/Call groupSize() int size = grid.groupSize(index1, index2); //Display the output System.out.println(" Group size:" + size +" "); //Display the output System.out.println("Total number of Group:" + grid.numOfGroupsAndListGroups()); //Close the Scanner sc.close(); I/Method groupSize() public int group Size(int index1, int index2) //Check condition if (index1=nxngrid.length II index2>=nxngrid.length) I/Throw an exception throw new IndexOutOfBoundsException("Invalid indices!"); //Create a new HashSet Set record = new HashSet(); //Call the function recompute() recompute(record, index1, index2); //Return return record.size(); //Method recompute() public void recompute(Set pos, int index1, int index2) //Check condition if (index1=nxngrid.length II index2>=nxngrid.length) //Return return; //Check condition if (nxngrid[index1][index2]) //Create a new instance for Position class Position a = new Position(index1, index2); //Check condition if (!pos.contains(a)) l/Add pos.add(a); //Call the function recompute() recompute(pos, index 1-1, index2); //Call the function recompute() recompute(pos,index1+1, index2); //Call the function recompute() recompute(pos,index1, index2-1); //Call the function recompute() recompute(pos, index1, index2+1); //Method numOfGroupsAndListGroups() public int numOfGroupsAndListGroups() I/Create a new HashSet Set> total = new HashSet>(); //Declare the needed variable for indices int index1, index2; // Loop to iterate through the rows for (index1=0;index1 //Loop to iterate through the columns for (index2=0;index2 //Create a new HashSet Set a = new HashSet(); //Call the function recompute() recompute(a,index1,index2); //Check condition if (!a.isEmpty()) // Add to total total.add(a); //Declare and initialize index1 index1=1; //Loop to iterate through total for (Set b: total) //Display groups System.out.println("Group "+index1+": "); // Loop to iterate for (Position c:b) l/Display System.out.println(c.toString()); } I/Increment index1 index1++; //Print a new line System.out.println(); //Return return total.size(); } //Class Position private static class Position //Declare variables for indices int index1, index2; l/Constructor public Position(int index1, int index2) { //Initialize indices this.index1 = index1; this.index2 = index2: //Method equals() public boolean equals(Object o) //Check condition if (0 == null) // Return return false; //Check condition if (o.getClass() != getClass()) //Return return false; //Update Position other = (Position) o; //Return return (other.index1 == index1) && (other.index2 == index2); //Method hashCode() public int hashCode() 1/Keturn return index1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started