Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with Java 2D Arrays PRECODE: /** * Creates a 9x9 array of numbers and sets it up as a * Sudoku problem. Zeros

Need help with Java 2D Arrays

PRECODE:

/** * Creates a 9x9 array of numbers and sets it up as a * Sudoku problem. Zeros in the array indicate places to be filled in. * This method is complete. Do not change this method. */ public Sudoku() { problem = new int[][] { { 0, 0, 4, 0, 0, 0, 0, 6, 7 }, { 3, 0, 0, 4, 7, 0, 0, 0, 5 }, { 1, 5, 0, 8, 2, 0, 0, 0, 3 }, { 0, 0, 6, 0, 0, 0, 0, 3, 1 }, { 8, 0, 2, 1, 0, 5, 6, 0, 4 }, { 4, 1, 0, 0, 0, 0, 9, 0, 0 }, { 7, 0, 0, 0, 8, 0, 0, 4, 6 }, { 6, 0, 0, 0, 1, 2, 0, 0, 0 }, { 9, 3, 0, 0, 0, 0, 7, 1, 0 } };

/** * Takes a 9x9 array of numbers and sets it up as a Sudoku problem. * * @param problem The array representing the Sudoku problem. * This method is complete. Do not change this method. */ public Sudoku(int[][] problem) { this.problem = problem; }

Code that i'm having trouble with:

/** * Prints the 9x9 array (problem) as a Sudoku board. * If the array element is 0, print a . instead of the 0. */ public void print() { for(int rowNumber = 0; rowNumber%3 <= 0 ; rowNumber++) { System.out.println("+-------+-------+-------+"); for(int columnNumber = 0; columnNumber%3 <= 0; columnNumber++) { System.out.println("|"); } } }

/** * Returns a 3x3 array representing a "box" of the 9x9 array. * The parameters boxRow and boxColumn are in the range 0 to 2, * since there are three rows and three columns of "boxes." * * @param boxRow The row number, 0, 1, or 2. * @param boxColumn The column number, 0, 1, or 2. * @return The 3x3 array representing a "box" of the 9x9 array. */ public int[][] getBox(int boxRow, int boxColumn) { int[][] box = new int[3][3]; //complete this method return box;

}

explanation of what i'm supposed to be doing:

public void print() { Use a nested loop to print out a 2D- array

a: Use a for loop to go through the rows If the row number is divisible by 3 with no remainder, print +-------+-------+-------+ (+ with 7 -'s 3 times)

Use a for loop to go through the columns If the col number is divisible by 3 with no remainder print | If the value in the array at the particular row and col is 0, print ". " otherwise print the value.

When you are done going through all the columns in a row, print |

When you are all done printing all of the rows, print the final +-------+... }

/** * Returns a 3x3 array representing a "box" of the 9x9 array. * The parameters boxRow and boxColumn are in the range 0 to 2, * since there are three rows and three columns of "boxes." * * @param boxRow The row number, 0, 1, or 2. * @param boxColumn The column number, 0, 1, or 2. * @return The 3x3 array representing a "box" of the 9x9 array. */ int[][] getBox(int boxRow, int boxColumn) { int[][] box = new int[3][3]; First do a validity check on the parameters to make sure they are in the correct range.

Define 2 local variables for the rowOffset (boxRow * 3) and the colOffset (boxColumn * 3)

Use a nested for loop to fill the box local variable box[row][col] = problem[rowOffset + row][colOffset + col] return box;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

Are these written ground rules?

Answered: 1 week ago

Question

differentiate the function ( x + 1 ) / ( x ^ 3 + x - 6 )

Answered: 1 week ago