Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective modify the Java files with the instructions that are provided to you for each file. SudokuPuzzle.java: Follow all instructions and complete all methods in

Objective

modify the Java files with the instructions that are provided to you for each file.

SudokuPuzzle.java:

Follow all instructions and complete all methods in this file, as relevant with respect to the given list of attributes. Areas that require your attention are preceded by the mention TO DO: In particular, you have to:

1. Complete the print() method that prints the attributes of Sudoku Puzzles

2. Complete the missing getters

3. Write a setter for the currentNumZeroes and for solvedPuzzle (and hence for withSolution). There will be 2 methods to set the solvedPuzzle (see SudokuPuzzle.java).

================================================================================

import java.io.*;

import java.util.*;

public class SudokuPuzzle {

/**************** ATTRIBUTES *******************************************

/* Here go your attributes, i.e., the information that is contained in

* your new "type"

* We can also see these new types as "blue-prints" of "things"/ instances

* that we are going to build

* DO NOT MODIFY THIS LIST OF ATTRIBUTES

***********************************************************************/

private String filename = ""; // name of the file where the sudoku puzzle is stored

private int[][] puzzle = new int[9][9]; // the 2D array of the sudoku puzzle

private int[][] solvedPuzzle = new int[9][9]; // the 2D array of the solved sudoku puzzle:

// used for reference when checking a user's guess

private int originalNumZeroes;// number of zeroes in the original puzzle

private int currentNumZeroes; // number of zeroes in the current puzzle (that is being played)

private boolean valid; // true if the file contains at least 9 lines (we won't check further)

// false otherwise

private boolean withSolution; //true if the puzzle has a solution stored in solvedPuzzle,

//false otherwise.

/***************** METHODS *********************************************

* Note that none of the methods below are static.

***********************************************************************/

/**************** CONSTRUCTORS *************************************/

/* default constructor: provided to you. You should not touch the next two lines of code */

public SudokuPuzzle() {

}

/* This method plays the role of loadSudoku in Comprehensive Lab 2 */

public SudokuPuzzle(String file) throws IOException {

filename = file;

int count = this.numLines();

valid = true;

FileReader fr = new FileReader(file);

BufferedReader textReader = new BufferedReader(fr);

if (count == 9){

withSolution = false; // initializing the attribute withSolution

// now reading the file and storing the sudoku puzzle

String line;

String[] digitsOnLine = new String[9];

for (int i=0; i<9; i++){

line = textReader.readLine();

digitsOnLine = line.split(",");

for (int j=0; j<9; j++) {

puzzle[i][j] = Integer.valueOf(digitsOnLine[j]);

}

}

}

else{

valid = false;

}

if (valid){

originalNumZeroes = this.numZeroes();

currentNumZeroes = originalNumZeroes;

}

textReader.close();

}

/***************** SETTERS / MUTATORS **********************************

* Methods that allow to set or modify the values of the attributes

* Usually, we would recommend defining one method per attribute

* In this case, only three setters make sense:

* 1. GIVEN: setFilename: when setting the filename, we can set all other attributes

* As a result, this setter method is very similar to the second

* constructor: see above.

* 2. TO DO: setCurrentZeroes: as you play the game, the number of zeroes decreases.

* We need a method to be able to update the value of the attribute

* called currentNumZeroes.

* 3. TO DO: setSolvedPuzzle: this method receives the name of a text file reads

* the text file with the solved puzzle and stores it in the solvedPuzzle 2D array.

* 4. TO DO: setSolvedPuzzle: if the user solves the sudoku, this method should

* be called to populate the 2D-array called solvedPuzzle, only if the solution

* does not exist.

* BONUS: write the solved sudoku into the file called filename

***********************************************************************/

public void setFilename(String file) throws FileNotFoundException, IOException {

filename = file;

int count = this.numLines();

valid = true;

FileReader fr = new FileReader(file);

BufferedReader textReader = new BufferedReader(fr);

if (count == 9){

withSolution = false; // initializing the attribute withSolution

// now reading the file and storing the sudoku puzzle

String line;

String[] digitsOnLine = new String[9];

for (int i=0; i<9; i++){

line = textReader.readLine();

digitsOnLine = line.split(",");

for (int j=0; j<9; j++) {

puzzle[i][j] = Integer.valueOf(digitsOnLine[j]);

}

}

}

else{

valid = false;

}

if (valid){

originalNumZeroes = this.numZeroes();

currentNumZeroes = originalNumZeroes;

}

textReader.close();

}

/* TO DO: a setter for currentNumZeroes and 2 for solvedPuzzle */

/**************** GETTERS / ACCESSORS **********************************

* Methods that allow to access the values of the attributes

* One method per attribute

* Note that the methods are not static

***********************************************************************/

/* GIVEN: the method getFilename retrieves the value of the attribute

* called filename */

public String getFilename() {

return this.filename;

}

/* TO DO: add getters for all other attributes */

public boolean getWithSolution() {

/* complete code here */

return false;

}

public int getOriginalNumZeroes() {

/* complete code here */

return 0;

}

/* Complete with other setter methods */

/***********************************************************************

* Below are some helpful methods (preceded with "GIVEN") that are provided to you.

* They are meant to help you with the constructors but also with the getter methods.

* You also need to implement the methods preceded by "TO DO"

***********************************************************************/

/* GIVEN: This method checks how many lines there are in the file we want to access */

public int numLines() throws FileNotFoundException, IOException {

FileReader fr = new FileReader(filename);

BufferedReader textReader = new BufferedReader(fr);

int counter = 0;

while (textReader.ready()) {

textReader.readLine();

counter++;

}

textReader.close();

return counter;

}

/* GIVEN: This method counts the number of zeroes in a puzzle */

/* We assume that the parameter is a full rectangular table; i.e., not ragged */

public int numZeroes() {

int counter = 0;

for (int i=0; i

for (int j=0; j

if (puzzle[i][j]==0) counter++;

}

}

return counter;

}

/* GIVEN: This method prints the 2D array of the sudoku puzzle */

public void printPuzzle() {

System.out.println();

for (int i=0; i<9; i++) {

for (int j=0; j<9; j++) {

if (puzzle[i][j] == 0) System.out.print(". ");

else System.out.print(puzzle[i][j] + " ");

if (j==2 || j==5) System.out.print("| ");

}

System.out.println();

if (i==2 || i == 5) {

System.out.println("----- + ----- + -----");

}

}

System.out.println();

}

/***********************************************************************

* TO DO: Here we are asking you to design a method that prints the information

* about a Sudoku puzzle (which is, an instance of SudokuPuzzle)

* Here, you should print:

* - the name of the file from which the sudoku was read (if any)

* - the sudoku puzzle itself

* - indicate whether there is a solution available (but not print the solution if available)

* - indicate a level of difficulty (based on the original number of zeroes)

* - indicate the current number of zeroes

***********************************************************************/

public void print() {

/* Complete with your code here */

}

}

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

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions

Question

How do books become world of wonder?

Answered: 1 week ago

Question

3. List ways to manage relationship dynamics

Answered: 1 week ago