Sudoku Project Part 3 Due Sunday, 11/21/2021 at 11:59 pm (20 points) Overview1 Getting started1 getRowValues method3 getColumnValues method3 getBoxValues method4 Hints4 Testing your code5...

Create the three methods described in the assignment write up. The file should be able to compile with the SudokuGame.java file.


Sudoku Project Part 3 Due Sunday, 11/21/2021 at 11:59 pm (20 points) Overview1 Getting started1 getRowValues method3 getColumnValues method3 getBoxValues method4 Hints4 Testing your code5 Submitting Your code7 Grading rubric7 Overview You will continue modifying your Sudoku.java file. We will be adding some utility methods that we will use later to help us figure out what values can go in what squares. Specifically, we are going to write three methods that determine all of the values in a particular row, a particular column, and a particular “box”. In case you need it, here are links to the project introduction, part 1, and part 2. Getting started For this part we will continue working on the Sudoku.java file. We are going to add some new methods to help us with the logic of determining what numbers can go in what squares. We will provide starter code on Canvas or the course website. You may use this code if you didn’t finish part 2, or if you simply don’t feel confident in your part 2 solutions, or even if you simply prefer working with the starter code with the comments. The starter code contains the solutions to part 2, so I can’t make it available until after the due date for part 2. Alternatively, if you want to get a head start on part 3, you are free to simply edit the code that is already on your computer. You’ll be modifying the Sudoku.java file. We will provide you with the code for SudokuGame.java that you can use to test your output. If you are modifying your own code, make sure your SudokuGame.java file matches the starter code. Let’s walk through all of the code components that you need to add to Sudoku.java. For this writeup, I will use the following sudoku board for my examples: getRowValues method Write a private method called getRowValues. This method has a single int parameter called row. It returns an ArrayList of all the values that are in that row of the grid. The parameter row will be a row number between 1 and 9 (which is in contrast to the fact that java array indices start counting at 0). Here are some examples from the grid above: · getRowValues(2) should return an ArrayList containing 6, 1, 9, and 5 (in any order) · getRowValues(7) should return an ArrayList containing the values 6, 2, and 8 (in any order) Note: in our Sudoku.java starter code, we have put the following line at the top of the file: import java.util.ArrayList; If you aren’t using the starter code, make sure you put this import statement into your code. getColumnValues method Write a private method called getColumnValues. This method has a single int parameter called col. It returns an ArrayList of all the values that are in that column. The parameter col will be a box number between 1 and 9 (which is in contrast to the fact that java array indices start counting at 0). Here are some examples from the grid above: · getRowValues(2) should return an ArrayList containing 3, 9, and 6 (in any order) · getRowValues(9) should return an ArrayList containing 3, 1, 6, 5, and 9 (in any order) getBoxValues method Write a private method called getBoxValues. This method has a single int parameter called box. It returns an ArrayList of all the values that are in that particular “box”. The parameter box will be a box number between 1 and 9. Here is a diagram that shows which squares correspond to which box: Here are some examples from the example grid we’ve been using: · getBoxValues(1) should return an ArrayList containing 5, 3, 6, 9, and 8 (in any order) · getBoxValues(6) should return an ArrayList containing the values 6, 8, 3, and 2 (in any order) Hints · Remember that the row, col, and box parameters are numbers between 1 and 9, but java indices start at 0. This may get really confusing to remember each time. But luckily you don’t have to keep switching between the two modes of thinking. Simply use the getValue method every time you want to get the number from a particular row and column, and that way you can always use familiar 1-based indexing. The getValue method will handle converting from 1-based indexing to 0-based indexing. Note that hopefully this illustrates the advantage of writing methods - once you get the method right, you can re-use it without having to re-debug it, and you only have to focus on what the method does rather than how the method works. · Don’t forget to make these three methods private · Remember, some squares contain a 0 as a placeholder, to indicate that the square is empty. You want to ignore these squares when you are adding values to your ArrayList · For the getBoxValues method, try to figure out the left-most and top-most square of the box for that row and column. For example, box 1 starts at row 1, column 1. Box 8 starts at row 7, column 4. Once you figure out the starting row and column, you can then write a nested loop to go through all the squares in the box. · One approach would be to use a long chain of if/else statements. However, there is a formula you can use to convert a box number into its topmost row (and similarly, there is a formula that converts a box number into its leftmost column). This formula requires very clever use of the integer division (/) and modulus (%) operators. You will receive one (1) extra credit point if you correctly use this approach. · You may find it more palatable to focus on one method at a time. Unfortunately, if you leave a method blank, the compiler will generate an error because each method needs to return an ArrayList object. Instead you can do the following: have the method create an ArrayList variable, and then immediately return it. This is obviously incorrect from a logic standpoint, but it will placate the compiler and allow you to test the methods that you actually did try to write. Later, when you are ready, you can move on to adding in the logic to actually fill in the ArrayList variable with values. Testing your code You may notice that in the Sudoku.java starter code we have written three other methods for you: public void printRow(int row) { for(int i: getRowValues(row)) { System.out.print(i + " "); } System.out.println(); } public void printCol(int col) { for(int i: getColumnValues(col)) { System.out.print(i + " "); } System.out.println(); } public void printBox(int box) { for(int i: getBoxValues(box)) { System.out.print(i + " "); } System.out.println(); } We are going to use these methods to test if the three methods that you wrote are correct. If you decided not to use the starter code, then you should copy/paste these three methods into your Sudoku.java file. Make sure you maintain correct indentation when you copy/paste the code. Put the following code into your SudokuGame.java file (or download it from Canvas or the course website): public class SudokuGame { public static void main(String[] args) { int[][] exampleGrid = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}}; Sudoku sudoku = new Sudoku(exampleGrid); sudoku.printGrid(); System.out.println();
Nov 22, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here