A class testing your code and an example test input file is available for the exercise.
The files needed in this task can be pulled from version control.
They are located in the repository student_template_project.
You must set
student_template_project as a remote in your local repository.
Remotes are set:
clone your own repository if not already cloned
move into your own local repository
add the course repository as a remote there e.g.
git remote add course-upstream firstname.lastname@example.org:compcs140-fall2022/student_template_project.gitNote! Name the remote to your own liking. The URL of the remote is decided based on whether you have set the SSH key to GitLab or not: :
https://, if you are not using an SSH key and
git@, if you are.
merge and fetch the version history of the repository with
git pull course-upstream main --allow-unrelated-historiesThe
--allow-unrelated-historiesdue to a configuration issue where the course repository is not sharing a common history with yours from creation. Once the remote is set and merged, this will not require further actions.
note that you do not have and do not need rights to update the repository.
There is also material on the use of several remotes on Programming 2.
The codes needed in this exercise are in the directory
SudokuTest.java: a test program to test your implementation
input.txt: a test input for the program
output.txt: an example output matching
Place your code into a file named Sudoku.java.
Implement a class
Sudoku that maintains a Sudoku grid and can e.g. check if it is legal.
If you have somehow managed to avoid knowing what Sudoku is, see first e.g. the
To be more precise, you should implement the
Sudoku class to have the following public members:
Sudoku()that initializes the Sudoku object to hold a 9×9 Sudoku grid whose all cells are empty. Empty cells are expressed using space characters
You are free to decide the internal (private) details of how you store the grid.
set(int i, int j, char c): sets the charecter
cinto the grid cell (
j). By this we mean the cell in column
Both the 9 rows and the 9 columns are indexed with 0…8. The function must check that
jare legal row and column indices, and if this is not the case, print a message of form “
Trying to access illegal cell (i, j)!”.
A Sudoku grid is allowed to hold only space characters
' '(ie. empty cells) and the digit characters
'9'. If the parameter
cis some other character, it will not be set and a message of form “
Trying to set illegal character c to (i, j)!” should be printed.
check(): checks if the current values in the Sudoku grid are legal. Returns
true(is legal) or
false(is not legal).
A Sudoku grid is legal if no row, column or 3x3 sub-block contains the same digit character twice (spaces may naturally occur multiple times).
Check first the rows, then the columns, and finally the sub-blocks (inspect the sub-blocks in a row-wise manner, starting with the upper left corner sub-block).
If the check finds two same digit characters from the same row, column or sub-block, record the smallest such duplicate digit character. This ensures that the messages described below will be unique even if there were several different duplicate digits. When a duplicate digit has been found, print one of the messages desribed below and stop the check, returning
Row i has multiple c's!”, where
iis the row index.
Column j has multiple c's!”, where
jis the column index.
Block at (x, y) has multiple c's!”, where
yare the row and column index of the upper left corner cell of the sub-block.
print(): prints the Sudoku grid. The grid borders are expressed using the characters
'+'and each cell value has a space on its both sides. See further details from the provided example output.
You may test your class by using the test program given in the file
SudokuTest.java, the test
data given in the file
input.txt, and the example output given in the file
Place these files and your own class implementation into the same directory, compilte the
program e.g. as
javac *.java, and run the test as
java SudokuTest input.txt. The program
should produce exactly the same output as shown in the file
A+ presents the exercise submission form here.