The objective of this project is to practice with various forms of iteration that use iterators and recursion. The project is a bit smaller than previous ones for two reasons. The first one is that...

Follow the instructions in file attached please


The objective of this project is to practice with various forms of iteration that use iterators and recursion. The project is a bit smaller than previous ones for two reasons. The first one is that debugging recursive code takes usually more time despite the fact that the size of the code is probably smaller. The second one is that in this project there is a significant extra credit component, so you should have more time to work on it if you chose to do so. This is the list of classes you must implement: Board Defines a virtual 2D board that can be used for various boardgames BoardGenerator Provides several methods for generating the initial state of a Board BoardIterator An Iterator that provides various alternative forms of iteration over a Board Direction An enumeration that defines the possible directions that an iterator can follow DirectionalIterable An extension of the Iterable interface that provides additional to the default functionality Puzzle Computes the solution for a numerical puzzle GUI Provides the graphical user interface for this project 4 Direction (5pts) Direction is an enumeration that has the following four members: HORIZONTAL Denotes a direction of iteration over a 2D array that follows the pattern depicted in this picture VERTICAL Denotes a direction of iteration over a 2D array that follows the pattern depicted in this picture HORIZONTALSNAKE Denotes a direction of iteration over a 2D array that follows the pattern depicted in this picture VERTICALSNAKE Denotes a direction of iteration over a 2D array that follows the pattern depicted in this picture 5 DirectionalIterable (5pts) DirectionalIterable is an interface that inherits from the interface Iterable and provides the following method. Iterator iterator(Direction d) The purpose of this method is to overload the iterator method that doesn't accept any parameters. By that, it can generate different iteration paths depending on the parameter that is passed to it. 6 Board (15pts) Board is a generic class that implements the DirectionalIterable interface. Its purpose is to store the 2D board and provide various methods for managing and interacting with it. private T[][] matrix The array to store the 2D board (you can't use ArrayList or other Collections) Board(int size) This constructor creates a square-size board with all elements having a null value. Board(int height, int width) This constuctor creates a rectangular board with all elements having a null value. Hint: Don't forget that you must reuse, not rewrite code when possible. Note: this is the only method you are allowed to use the @SuppressWarnings annotation to avoid the warnings of the compiler. T getElement(int row, int col) Getter for the elements of the 2D board void setElement(int row, int col, T el) Setter for the elements of the 2D board int getHeight() Getter for the height of the 2D board int getWidth() Getter for the width of the 2D board boolean isValidIndex(int row, int col) Validation check for a 2D board index. Hint: Don't forget that you must reuse, not rewrite code when possible – if we include this method in this specification it means that it's not private and therefore it means that it can be used in other classes too. String toString() We won't test this method but it's highly recommended you implement it for debugging purposes. Especially with recursion, it helps a lot if you can print the whole 2D board easily. iterator methods Factory method(s) that return a new BoardIterator object. Check the Iterable interface for more details on the syntax. Hint: For testing your iterators, you should write a for-each loop (not a for loop) in the main method. 7 BoardIterator (20pts) BoardIterator is a generic class that implements the Iterator interface. Its purpose is to provide an iterator for the Board class. constructor The implementation of the constructor(s) depends on the implementation of the iterator methods in class Board. This is because we don't directly instantiate an Iterator by calling new BoardIterator() but we call the iterator method of the class that implements the Iterable interface. methods Check the Iterator interface for more details on the methods you must implement. remove() For this method in particular, do not provide an implementation. Instead, it must raise an UnsupportedOperationException if anyone tries to invoke it. 8 BoardGenerator (25pts) BoardGenerator is a class that provides static methods for generating various kinds of initial states for a Board. public static Board loadIntegersFromFile(String filename) It reads data from a text file and creates the respective Board. The file is guaranteed to have the correct amount of data (no validation required). The first number in the file denotes the number of rows in the board. The second number denotes the number of columns in the board. And following are the values for all the elements of the board. A '-' character denotes an empty cell in the board (you must assign the null value in these cases). 5 7 1 6 - 9 8 7 2 3 1 7 - 9 4 - 4 5 8 1 2 - 6 9 2 6 - 5 8 3 - - - 5 - - 1 public static int[] loadRowSumsFromFile(String filename) It reads data from a text file and creates an array of integers that holds the sums of each row of a board. The file is guaranteed to have the correct amount of data (no validation required). The first number in the file denotes the number of rows in the board. And following are the sums of the rows. In the example below, on the left side we see the actual data in the file, and on the right side we see a potential board (among many boards) that this data can originate from: 5 36 1 6 3 9 8 7 2 31 3 1 7 2 9 4 5 29 4 5 8 1 2 3 6 37 9 2 6 4 5 8 3 30 8 7 4 5 3 2 1 public static int[] loadColSumsFromFile(String filename) It reads data from a text file and creates an array of integers that holds the sums of each column of a board. The file is guaranteed to have the correct amount of data (no validation required). The first number in the file denotes the number of columns in the board. And following are the sums of the columns. In the example below, on the left side we see the actual data in the file, and on the right side we see a potential board (among many boards) that this data can originate from: 7 25 21 28 21 27 24 17 1 6 3 9 8 7 2 3 1 7 2 9 4 5 4 5 8 1 2 3 6 9 2 6 4 5 8 3 8 7 4 5 3 2 1 public static Board randomIntegers(int height, int width, int maxRandomNumber, int nullElements, int[] rowSum, int[] colSum) It creates and returns a board with dimensions height x width. The elements of the board are random integers in the range [1, maxRandomNumber]. All elements within a row must be unique. The same applies to all elements within a column. The rowSum and the colSum are pre-allocated arrays that are passed empty to the method and are populated with the sum of the elements in each row or column respectively. Last, nullElements is the number of elements that will be randomly selected and nulled from the board before the method returns (but after the calculation of the sums). The following is an example of a 6x5 board that is generated by this method and next to it is the initially generated board before removing the nullElements. The passed parameters in this case were maxRandomNumber=9 and nullElements=5. The contents of the returned rowSum and colSum are also depicted. Hint: You don't need recursion for this method. You can assume that maxRandomNumber will be larger than both dimensions of the board. returned board initial board returned rowSum returned colSum 3 2 4 6 null 3 2 4 6 8 23 25 20 26 24 30 30 29 32 26 31 6 9 3 2 5 6 9 3 2 5 null null 6 1 null 4 7 6 1 2 7 1 8 4 6 7 1 8 4 6 1 6 null 5 3 1 6 9 5 3 9 4 2 8 7 9 4 2 8 7 public static Board completeSquareBoard(int size, int nullElements) It creates a square-size board with dimensions size x size. The elements of the board are random integers in the range [1, size]. All elements within a row must be unique. The same applies to all elements within a column. The rowSum and the colSum are not needed in this case because the values can be inferred mathematically at the calling site. The following is an example of a 3x3 board that is generated by this method. nullElements=3 in this case. Hint: You need recursion for this method – you will practically make
Dec 02, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here