Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have to write a Java program for the following instructions below. Can i get some help. //imported needed packages import javax.swing.text.Position; import java.util.Set; import
I have to write a Java program for the following instructions below. Can i get some help.
//imported needed packages import javax.swing.text.Position; import java.util.Set; import java.util.Scanner; import java.util.HashSet; //Class public class NxNGrid { private boolean[][]nxngrid = null; public NxNGrid(boolean[][]inputgrid) { nxngrid = inputgrid; } public static void main(String[] args) { int index1, index2 = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter the square position in the group: "); index1 = sc.nextInt(); index2 = sc.nextInt(); 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,} }; NxNGrid grid = new NxNGrid(nxnGridData); int size = grid.groupSize(index1,index2); System.out.println(" Group size: " + size + " "); System.out.println("Totla number of Groups:" + grid.numOfGroupsAndListGroups()); sc.close(); } public int groupSize(int index1, int index2){ if(index1 =nxngrid.length || index2 >= nxngrid.length){ throw new IndexOutOfBoundsException("Invalid indices!"); } Set record = new HashSet(); recompute(record,index1,index2); //Return return record.size(); } //Method Recompute() public void recompute(Set pos, int index1, int index2){ if(index1 = nxngrid.length || index2 >= nxngrid.length){ return; } if(nxngrid[index1][index2]){ Position a = new Position(index1,index2); if(!pos.contains(a)){ pos.add(a); recompute(pos,index1-1,index2); recompute(pos,index1+1,index2); recompute(pos, index1,index2-1); recompute(pos,index1,index2+1); } } } public int numOfGroupsAndListGroups(){ Set> total = new HashSet>(); int index1,index2; for(index1=0; index1 { for(index2=0;index2 { Set a = new HashSet(); recompute(a,index1,index2); if(!a.isEmpty()) total.add(a); } } index1 =1; for(Set b: total){ System.out.println("Group " + index1+":"); for(Object c: b){ System.out.println(c.toString()); } index1++; System.out.println(); } //Return return total.size(); } private static class Position{ int index1, index2; public Position(int index1, int index2){ this.index1 = index1; this.index2 = index2; } public boolean equals(Object o){ if(o == null) return false; Position other = (Position)o; return (other.index1 == index1)&&(other.index2 == index2); } public int hashCode(){ return index1 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 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
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