Concept explainers
Please draw a UML class diagram for the following code:
public class SudokuSolver {
public static boolean solveSudoku(int[][] board) {
int N = board.length;
int[] emptyCell = findEmptyCell(board);
if (emptyCell == null) {
return true;
}
int row = emptyCell[0];
int col = emptyCell[1];
for (int num = 1; num <= 9; num++) {
if (isValidMove(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true;
}
board[row][col] = 0;
}
}
return false;
}
public static int[] findEmptyCell(int[][] board) {
int N = board.length;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (board[row][col] == 0) {
return new int[]{row, col};
}
}
}
return null;
}
public static boolean isValidMove(int[][] board, int row, int col, int num) {
return isValidRow(board, row, num) &&
isValidCol(board, col, num) &&
isValidRegion(board, row - row % 3, col - col % 3, num);
}
public static boolean isValidRow(int[][] board, int row, int num) {
for (int col = 0; col < board.length; col++) {
if (board[row][col] == num) {
return false;
}
}
return true;
}
public static boolean isValidCol(int[][] board, int col, int num) {
for (int row = 0; row < board.length; row++) {
if (board[row][col] == num) {
return false;
}
}
return true;
}
public static boolean isValidRegion(int[][] board, int startRow, int startCol, int num) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row + startRow][col + startCol] == num) {
return false;
}
}
}
return true;
}
public static void printBoard(int[][] board) {
int N = board.length;
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
System.out.print(board[row][col] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] puzzle = {
{0, 0, 3, 0, 0, 8, 0, 2, 0},
{6, 0, 0, 3, 0, 0, 0, 0, 9},
{0, 9, 0, 7, 0, 0, 1, 0, 4},
{9, 0, 0, 2, 0, 0, 5, 4, 3},
{0, 8, 0, 4, 0, 1, 0, 0, 0},
{4, 6, 2, 0, 0, 7, 0, 0, 1},
{1, 0, 6, 0, 0, 3, 0, 9, 0},
{8, 0, 0, 0, 0, 9, 3, 0, 6},
{0, 3, 0, 6, 0, 0, 7, 0, 0}
};
if (solveSudoku(puzzle)) {
printBoard(puzzle);
} else {
System.out.println("No solution exists.");
}
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images
- Image attachedarrow_forwardFix all logic and syntax errors. Results Circle statistics area is 498.75924968391547 diameter is 25.2 // Some circle statistics public class DebugFour2 { public static void main(String args[]) { double radius = 12.6; System.out.println("Circle statistics"); double area = java.Math.PI * radius * radius; System.out.println("area is " + area); double diameter = 2 - radius; System.out.println("diameter is + diameter); } }arrow_forwardQuestion 2 Analyze the following code. // Program 1 public class Test{ public static void main(String[] args) { Object al = new A0); Object a 2 = new A0); System.out.println(((A)a 1).equals((A)a 2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2 public class Test{ public static void main(String[] args) { A a1 = new A(); A a2 = new A0); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } Program 1 displays true and Program 2 displays true Program 1 displays false and Program 2 displays true O Program 1 displays true and Program 2 displays false Program 1 displays false and Program 2 displays falsearrow_forward
- 1 public class Main { 1234567 7 8 9 10 11 ENERGET 12 13 14 15 16 17 18} public static void main(String[] args) { String present = ""; double r = Math.random(); // between 0 and 1 if (r > >>arrow_forwardpublic static int countIt (int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < 10; j++) count += 1; return count; } Big-O notation: Explanation:arrow_forwardFix the typo in the following program. Your solution should be a single line of code with the typo fixed: public class Rational private int numerator; private int denominator; public Rational(int numerator, int denominator) this.numerator = numerator: this.denominator = denominator; public int getNumerator() { return this.numerator; } public int getDenominator (){ return this.denominator; } public double getFraction() { return (double)numerator / (double)denominator; public String toString () | return numerator+"/"+denominator; public static void main (Stringl) args) Rational r = new Rational (2, 3); System.out.println (r); double real = getFraction (): System.out.println (real);arrow_forward
- complete the missing code. public class Exercise09_04Extra { public static void main(String[] args) { SimpleTime time = new SimpleTime(); time.hour = 2; time.minute = 3; time.second = 4; System.out.println("Hour: " + time.hour + " Minute: " + time.minute + " Second: " + time.second); } } class SimpleTime { // Complete the code to declare the data fields hour, // minute, and second of the int type }arrow_forwardFind the error(s) in the following class definition: public class Circle private double radius; public double setRadius(double a) { radius = a; public void getRadius() { return radius;arrow_forwardpublic class main { public static void main(String [] args) { Dog dog1=new Dog(“Spark”,2),dog2=new Dog(“Sammy”,3); swap(dog1, dog2); System.out.println(“dog1 is ”+ dog1); System.out.println(“dog2 is ”+ dog2); } public static void swap(Dog a, Dog b) { String nameA = a.getName(); String nameB = b.getName(); a.setName(nameB); b.setName(nameA); } What is the output of the main()?arrow_forward
- public class Test { } public static void main(String[] args){ int a = 10; System.out.println(a*a--); }arrow_forwardpublic class Test { } public static void main(String[] args) { String str = "Salom"; System.out.println(str.indexOf('t)); }arrow_forwardpublic class Main { public static void main(String[] args) { System.out.println("Your factorial is: " + factorial(9)); }} public static int factorial(int number) { if (number == 0) { return 1; } return number * factorial(number -1); } i need help fixing this code involving recursionarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education