Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Objective

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

2.1. 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).

4. Bonus: within the setSolvedPuzzle method, add code to write the solved puzzle to the file where the original puzzle was found

2.2. Node.java: This data type will be used to create a linked list. This object contains data (SudokuPuzzle) and a link to the next Node. You do not need to modify this file.

2.3. PlayedSudokus.java:

This is an object that defines a linked list composed of nodes, where each node contains a SudokuPuzzle. In this file, you can observe that getters and setters are not needed because of the use of this program, and constructors are already given to you. Feel free to add more constructors as you see fit, but you do not have to. You have to create:

1. A (non static) method, size, which computes the size of the linked list. This method should be iterative (it should use a loop: for or while; while is recommended).

2. A (non static) method, removeHead, which modifies the original list by removing its first node.

3. A (non static) method, removeTail, which modifies the original list by removing its last node.

4. Bonus: A (non static) method, addNth, which takes a new member M and an integer n, and modifies the original list where M has been added as the nth node in the list (or at the end of the list if n is larger than the size of the list + 1).

2.4. playSudoku.java:

This is the java file where you are going to use the objects SudokuPuzzle and PlayedSudokus, in order to handle and manage your MinersSudoku game. Some methods are given to you, but most are incomplete and you need to complete them. Here is the list of these methods:

1. Complete the main method following the given instructions as comments.

2. Complete the sorting algorithm for arrays of puzzles where instructed.

3. Write a method that prints all elements of an array of puzzles.

4. Write a method that takes an array A of puzzles and creates a linked list of puzzles from A that are solved (the ones for which withSolution is true).

Exceptions: Note: in the above methods, you have to handle exceptions. Throughout this activity, you are expected to handle exceptions at least 2 times, among 2 different types of exceptions.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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 */

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Node{

private SudokuPuzzle puzzle; //this is the data each node contains

private Node next; //link to the next nodes

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

* Note that the signatures are different from those we are used to

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

// Default constructor

public Node() {

}

/* Constructor 2 ************************************************************

* Here this constructor consists in starting the inventory with only one puzzle

* So there is this puzzle in the list and nothing in the rest of the list

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

public Node(SudokuPuzzle P) {

this.puzzle = P;

next = null;

}

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

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

* One method per attribute

* Note that the methods are not static

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

public void setPuzzle(SudokuPuzzle P) {

this.puzzle = P;

}

public void setNext(Node n) {

this.next = n;

}

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

* Methods that allow to access the values of the attributes

* One method per attribute

* Note that the methods are not static

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

public SudokuPuzzle getPuzzle() {

return puzzle;

}

public Node getNext() {

return next;

}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* This class is a blueprint (user-defined type) for a linked list of Sudoku Puzzles */

public class PlayedSudokus {

/**************** 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" we are

* going to build

* DO NOT MODIFY THE LIST OF ATTRIBUTES

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

// Attributes ARE GIVEN TO YOU...

private Node head;

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

* Note that the signatures are different from those we are used to

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

// Constructors

public PlayedSudokus() {

}

/* Constructor 2 ************************************************************

* Here this constructor consists in starting the inventory with only one puzzle

* So there is this puzzle in the list and nothing in the rest of the list

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

public PlayedSudokus(Node P) {

head = P;

}

/* Constructor 3 ************************************************************

* Here this constructor consists in building a new inventory from a given puzzle

* and an existing list of puzzles. So there is this puzzle in the list and the "old"

* list as the rest of the list

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

public PlayedSudokus(Node p, PlayedSudokus n) {

p.setNext(n.head);

this.head = p;

}

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

* Other methods

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

// A (non static) method, print, that prints the linked list of toys

public void print() {

Node iter = this.head;

while (iter != null) {

iter.getPuzzle().print();

iter = iter.getNext();

}

}

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

* TO DO: A (non static) method, size, that returns the number of items in

* the list

* This method has to use a loop (NO RECURSION).

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

public int size() {

int size = 0;

Node iter = this.head;

while (iter != null) {

size++;

iter = iter.getNext();

}

return size;

}

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

* A (non static) method, addTail, that takes a new list of puzzles L and modifies

* the original list where L has been added as the last nodes in the list.

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

public void addTail(Node L) {

Node iter = this.head;

//while (/* complete here */) {

// iter = iter.getNext();

//}

// at this point, iter is the last node of the list = the tail

iter.setNext(L);

}

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

* A (non static) method, addTail, that takes a puzzle P and modifies the original list where

* P is now the last puzzle in the list

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

public void addTail(SudokuPuzzle M) {

Node L = new Node(M);

Node iter = this.head;

// complete code here

// at this point, iter is the last node of the list = the tail

iter.setNext(L);

}

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

* TO DO:

* 1: A (non static) method, removeHead, that modifies the original list by cropping out its first node

* 2: A (non static) method, removeTail, that modifies the original list by removing its last node

* BONUS:

* 3. A (non static) method, addNth, that takes a new member M and

* an integer n, and modifies the original list where M has been added

* as the nth node in the list (or at the end of the list if n is larger

* than the size of the list + 1).

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

public void removeHead() {

// complete code here

}

public void removeTail() {

// complete code here

}

public void addNth(SudokuPuzzle M, int n) {

// complete code here

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.*; import java.util.*;

public class playSudoku {

/************************************************************************************************** * This method takes an array of SudokuPuzzle instances and sorts them by: * - solved / not solved: not solved puzzles should come first * - original number of zeroes should be in increasing order in each (solved/not solved) category * Some of the code is provided. * TO DO: you have to complete the code according to the comments. See below, starting on line 55. *************************************************************************************************/ public static void sortSudokuPuzzles(SudokuPuzzle[] repositoryOfSudokus) { int notSolved = 0; int last = repositoryOfSudokus.length-1; SudokuPuzzle aux; // this for loop results in splitting not solved (coming first) from the solved (coming last) puzzles for (int i=0; i i) { //swap last and i aux = repositoryOfSudokus[last]; repositoryOfSudokus[last] = repositoryOfSudokus[i]; repositoryOfSudokus[i] = aux; last--; notSolved++; } else { break; } } } // notSolved is the number of not solved Sudokus System.out.println("In this array, there are " + notSolved + " unsolved sudoku puzzles");

// now we are going to sort each part (not solved first, and then solved) // below: we sort the first part of nonSolved sudoku puzzles in increasing number of their original number of zeroes int indexOfMaxNumberZeroes = 0; for (int i=notSolved-1; i>=0; i--) { indexOfMaxNumberZeroes = 0; // look for sudoku puzzle with max number of zeroes between 0 and i for (int j=0; j<=i; j++) { if (repositoryOfSudokus[j].getOriginalNumZeroes() > repositoryOfSudokus[indexOfMaxNumberZeroes].getOriginalNumZeroes()) indexOfMaxNumberZeroes = j; } // swap indexOfMaxNumberZeroes with i aux = repositoryOfSudokus[indexOfMaxNumberZeroes]; repositoryOfSudokus[indexOfMaxNumberZeroes] = repositoryOfSudokus[i]; repositoryOfSudokus[i] = aux; }

// TO DO: below: YOU sort the second part of remaining sudoku puzzles in increasing number // of their original number of zeroes // this portion of your array starts at index notSolved

}

/************************************************************************************************** * TO DO: This method takes an array of SudokuPuzzle instances and prints each of them: * - it HAS to use the method print from SudokuPuzzle file *************************************************************************************************/ public static void printRepoOfSudokuPuzzles(SudokuPuzzle[] repositoryOfSudokus) { /* Complete with your code here */ }

/************************************************************************************************** * TO DO: This method takes an array of SudokuPuzzle instances and creates a list of the puzzles in * the array for which there exists a solution *************************************************************************************************/ public static PlayedSudokus createListOfPlayedSudokus(SudokuPuzzle[] repositoryOfSudokus) { PlayedSudokus L = new PlayedSudokus(); /* Complete with your code here */ return L; }

/************************************************************************************************** * This is the main method. * The first line is provided. * TO DO: for the rest of the code, we provide the pseudocode of it and you have to complete it **************************************************************************************************/ public static void main(String[] args) throws FileNotFoundException, IOException { /* Create instances of our new type SudokuPuzzle using textfiles */ SudokuPuzzle P0 = new SudokuPuzzle("sudoku3.txt"); /* then: ********************************************************* * - set solution of P0 from sudoku3Solution.txt * - create P1 from sudoku.txt * - create P2 from sudoku2.txt * - create P3 from sudoku4.txt *****************************************************************/

/* then: ********************************************************* * - print P0 * - print P1 * - print P2 * - print P3 * using the print method that was defined in SudokuPuzzle *****************************************************************/

/* then: ********************************************************* * - ask the user if s/he wants to play sudoku * (the user has to pick one of the puzzles of the array) * - once the puzzle is picked: re-use the algorithm of your comprehensive lab #2 * but whenever the user completes a sudoku, you need to update the * puzzle with its solution (in the attribute solvedPuzzle) and change * the attribute withSolution * - similarly as in comp. lab #2, let the user keep playing until s/he * decides that s/he wants to exit or when there are no more new puzzles * to play (whichever comes first) *****************************************************************/

/* then: ********************************************************* * - create a 1D array of elements of type SudokuPuzzle called repository * repository contains {P0, P1, P2, P3, P3, P0} * - sort repository using the above method called sortSudokuPuzzles * - print repository, now a sorted array *****************************************************************/

/* then: ********************************************************* * - create a linked list of played sudoku from the array repository * - print out its size * - print out its content, using the method print defined for * data of type PlayedSudokus *****************************************************************/

} }

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

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions

Question

When is it appropriate to use a root cause analysis

Answered: 1 week ago