|
I have coded for Sudoku puzzle in java.The thing is my code has limitation for giving inputs for 9*9 grid. How do I make my code adaptable for all the grids.Please have patience.I am new to java.
What changes do I need to make so that the code can run on all grid sizes?The grid is square not a rectangle.
class Solution {
public void solveSudoku(char[][] board) {
if(solveSudoku2(board)) {
return;
}
}
public boolean solveSudoku2(char[][] board) {
boolean isEmpty = true;
int row = -1;
int col = -1;
int n = board.length;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(board[i][j] == '.') {
row = i;
col = j;
isEmpty = false;
break;
}
}
if(!isEmpty) {
break;
}
}
if(isEmpty) {
return true;
}
for(int num = 1; num <= n; num++) {
char char_num = (char)(num + '0');
if(checkSafe(board,char_num,row,col)) {
board[row][col] = (char)(num + '0');
if(solveSudoku2(board)) {
return true;
} else {
board[row][col] = '.';
}
}
}
return false;
}
public boolean checkSafe(char[][] board, char num, int row, int col) {
for(int i = 0; i< board.length; i++ ) {
if(board[row][i] == num) {
return false;
}
}
for(int j = 0; j < board[0].length; j++) {
if(board[j][col] == num) {
return false;
}
}
int checknum = (int)Math.sqrt(board.length);
int startrow = row - row % checknum;
int startcol = col - col % checknum;
for(int k = startrow; k < startrow + checknum; k++) {
for(int l = startcol; l < startcol + checknum; l++) {
if(board[k][l] == num) {
return false;
}
}
}
return true;
}
}
|
|
|
|
|
Sudoku size is is limited by the values that are allowed to be placed in the squares. For digits it is 9 x 9, for alphabetics it could be any number you choose. The main problem is the complexity of the solution as the size increases.
|
|
|
|
|
package Array;
import java.util.Arrays;
public class Homework {
public static int [] sortirovka (int [] array) {
int a;
for (int i = 0; i < array.length; i++) {
int min = array [i];
int index = i;
for (int j = i + 1; j < array.length; j++) {
if (array [j] < min) {
min = array [j];
index = j;
}
}
if (i != index) {
a = array[i];
array[i]= min;
array [index] = a;
}
}
return array;
}
public static void main(String[] args) {
int [] array = {5, 2, 5, 3, 8, 9, 10, -2, 0, 6};
sortirovka (array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
|
|
|
|
|
Yes, quite normal, programming takes some time to learn. You can learn the basics (at your own speed) from The Java™ Tutorials[^].
|
|
|
|
|
Thank you very much, appreciate it.
|
|
|
|
|
You're welcome. Bear in mind that every 'expert' had to start learning from zero at one time.
|
|
|
|
|
Write A Program In Java That Displays A Cuckoo Clock that plays computer time
|
|
|
|
|
Nobody here will do your homework assignment for you, no matter how many times you repost it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
please who can help me whit
java applet cuckoo clock that plays computer time
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've done java as a hobby for a few years, mostly tutorials. I'm working on a few Android tutorials currently, along with Spring framework, and occasionally Python stuff.
I've built several Java projects with tutorials, like a JavaFx email client. I want to build a project on my own. Something not too complicated that will look good to employers.
Does anyone have any suggestions?
|
|
|
|
|
Employers are rarely impressed with work done as a hobby. They are generally looking for experience in the fields that they use in their business. So you should first look around at what jobs are being advertised in the trade press, talk to recruitment agents to see what is needed, etc.
|
|
|
|
|
There are some "employers" who think that if you're really into "programming", then you'll always have some side project going (because you have no life).
And / or, they want you to be a (well known) "blogger"; so they can paint their "About" page with your face (and claim you as one of their own).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Fortunately I have never worked for any such 'enlightened' companies.
|
|
|
|
|
I'm using Wildfly 20.0 version.
I would like to use JDBC in Wldfly for my dynamic web project (with JSF and EJB). So I'm looking for configuring the server to handle security by JDBC connection to database which stores user/role/userrole.
What can I to for my purpose?
|
|
|
|
|
|
I'm reading this guide, very interesting. I will ask you about it when I have more specific doubts.
|
|
|
|
|
I have configured Realm with JDBC, but when I try to login I get this message in Console
TRACE [org.jboss.as.domain.management.security] (default task-1) User 'UserNameDGU' not found in properties file.
and before I have seen this:
DEBUG [org.wildfly.security] (management I/O-1) Unable to create instance: java.security.NoSuchAlgorithmException: Service not registered with Provider WildFlyElytron: WildFlyElytron: SaslServerFactory.DIGEST-SHA -> org.wildfly.security.sasl.digest.DigestServerFactory
|
|
|
|
|
write a general python program that takes into account:
1. The cost of your ideal house. (basic)
2. The cost needed for a down payment. (basic)
3. Your annual salary. (basic)
4. The portion of your salary that you will
|
|
|
|
|
Apart from the fact that point 4 has been cut off early, there is no information about what the program is supposed to do. I could write the following:
def somethings():
print("Ideal house cost £295,000")
print("Down payment £29,500")
print("Annual salary £57,000")
print("The portion of my salary that I will")
But as you can see it is meaningless.
|
|
|
|
|
Message Closed
modified 29-Oct-20 4:20am.
|
|
|
|
|
First off this is nothing to do with Java, so it really belongs in Quick Answers[^] .
Secondly, do not expect people here to do your work for you. Show the code that you have written, explain what does not work (in proper details) and people will try to help you. But remember, this is your assignment, so you are expected to solve it.
|
|
|
|
|
The question is this-:
Write a program using for loop that computes sinx and cosx by using the following power series-
sinx=x-x^3/3!+x^5/5!-x^7/7!+....
cosx=1-x^2/2!+x^4/4!-x^6/6!+.....
Note-(Here ! is used for factorial of that number and ^ for to the power)
What I have tried is-:
class sincos
{
void main(double x,int n)
{
int i,j,k,f;
double sum=0.0;
x=Math.toRadians(x);
{
for(i=1,j=1;i<=n;i=i+2,j++)
{
f=1;
for(k=1;k<=i;k++)
{
f=f*i;
}
if(j%2==0)
sum=sum-(Math.pow(x,i)/f);
else
sum=sum+(Math.pow(x,i)/f);
}
System.out.println("sin"+Math.toDegrees(x)+"="+sum);
}
for(i=0,j=1;i<=n;i=i+2,j++)
{
f=1;
for(k=1;k<=i;k++)
{
f=f*i;
}
if(j%2==0)
sum=sum-(Math.pow(x,i)/f);
else
sum=sum+(Math.pow(x,i)/f);
}
System.out.println("cos"+Math.toDegrees(x)+"="+sum);
}}
My problem is that I am not understanding that whether the source code I wrote is correct or not as the output is coming something else .I checked the code many times but still can't understand where the problem is.
So can you check my source code and correct it according to the question.
-- modified 5-Dec-20 21:01pm.
|
|
|
|
|
Compiling does not mean your code is right!
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.
So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.
Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input Expected output Actual output
1 2 1
2 4 4
3 6 9
4 8 16 Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
{
return value * value;
}
Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|