Static Methods
Author |
Message |
CooKieLord
|
Posted: Fri Nov 16, 2007 4:15 pm Post subject: Static Methods |
|
|
Quote: Of course, it doesn't always make sense for methods to be directly attached to objects. In this case, we have "static" methods, like our "main" method. Static methods are associated with the class itself, rather than an object of that class.
Can someone explain to me more in depth what "statatic" means. I would like to understand it because I am getting an error.
Quote: non-static method constraints(int[][]) cannot be referenced from a static context
constraints = SudokuLib.constraints(sudoku);
Java: | public static void generateConstraints(int[][] sudoku, boolean[][] constraints)
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
// BODY OF THE ALGORITHM
constraints = SudokuLib.constraints(sudoku);
// RETURN RESULT: NONE
}
public boolean[][] constraints (int[][] sudoku) {
int i = sudoku.length;
int j = sudoku[0].length;
if (sudoku[i][j] != 0) {
constraints[i][j] = true;}
else {
constraints[i][j] = false;}
if (i !=0) {
generateConstraints(i-1,j); }
if (j != 0) {
generateConstraints(i, j-1); }
return constraints;
} //end constraints
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Fri Nov 16, 2007 4:47 pm Post subject: RE:Static Methods |
|
|
A non-static method must be invoked on an object. A static method can be invoked without creating an object first.
In your case you should probably have a Sudoku class with each of those methods being non-static. |
|
|
|
|
![](images/spacer.gif) |
CooKieLord
|
Posted: Fri Nov 16, 2007 4:55 pm Post subject: RE:Static Methods |
|
|
The professor wants us to create a Sudoku program. They did some work for us, like the graphic interface and input method. We need to work on the algorithms.
The students need to make the algorithm that checks the validity of the answer, i.e. check for duplicate entries in a row, column or 3x3 zone, etc. They did not give us access to the entire source, we only have a jew .java files with half-completed methods. Some of the files in the package is simply a .class.
As such, I cannot check to see how they call the algorithm. Is it static? Not static? Does it even matter in my case?
Here is the entire class that I'm working on, and with my current progress at the moment.
Java: | import javax.swing.JOptionPane;
public class SudokuLib {
// Q5: TO COMPLETE
// METHOD verifyGuess: verifies if the guess would result is a valid sudoku grid
// GIVENS: the row, the column, and the value to insert (the guess)
// the sudoku grid and its constraints,
// a boolean value indicating if we need to display an error message
// (invoked with true in the program, with false in the tests)
public static boolean verifyGuess (int row, int col, int move, int[][] sudoku, boolean[][] constraints, boolean messages )
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
boolean result; // RESULT: true if the guess does not make the grid invalid
int pos; // INTERMEDIATES: position in the row or column
// BODY OF THE ALGORITHM
result = true;
// Does the player try to change one of the initial values?
if (constraints [row ][col ])
{
if (messages )
JOptionPane. showMessageDialog(null, "You cannot modify an initial value (in red)");
result = false;
}
else
{
; // TO COMPLETE!
}
//Does the player enter a duplicate entry?
//Row
for (int j= 0; j<sudoku. length; j++ ) {
if (move == sudoku [row ][j ]) {
result = false; //the user has entered a duplicate entry.
}//end if
}//end for j
//col
for (int i= 0; i<sudoku [0]. length; i++ ) {
if (move == sudoku [i ][col ]) {
result = false; //the user has entered a duplicate entry
} //end if
}// end for
// RETURN RESULT
return result;
}
public static boolean verifyGuessRow (int row, int col, int[][] sudoku, boolean[][] constraints )
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
boolean result; // RESULT: true if the guess does not make the grid invalid
int pos; // INTERMEDIATES: position in the row or column
// BODY OF THE ALGORITHM
// Does the player try to change one of the initial values?
// RETURN RESULT
return result;
}
// Q4: TO COMPLETE
// METHOD generateSudoku: randomly chooses one sudoku grid from the
// predefined ones, executes a random number of rotations,
// updates the constraints, and returns a sudoku grid
// MODIFIEDS: constraints
public static int[][] generateSudoku (boolean[][] constraints )
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
// BODY OF THE ALGORITHM
// RETURN RESULT
return sudokuGrids ()[3]; // TO REPLACE!
}
// Q3: TO COMPLETE
// METHOD generateConstraints: modifies the matrix of constraints
// that indicates by true a position in the sudoku grid
// where there is a value that is different from 0 (and by false if = 0)
// GIVENS: a matrix of integers representing a sudoku grid
// MODIFIEDS: a matrix of boolean constraints
/* NESTED FOR METHOD public static void generateConstraints(int[][] sudoku, boolean[][] constraints)
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
// BODY OF THE ALGORITHM
for (int i = 0; i<sudoku.length;i++) {
for (int j=0;j<sudoku[0].length; j++) {
switch (sudoku[i][j]) {
case 0: constraints[i][j] = true; break;
default: constraints[i][j] = false; break; } //end switch
} //end for j
} //end for i
// RETURN RESULT: NONE
} //end generateConstraints*/
//Recursive method
public static void generateConstraints (int[][] sudoku, boolean[][] constraints )
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
// BODY OF THE ALGORITHM
constraints = SudokuLib. constraints(sudoku );
// RETURN RESULT: NONE
}
public boolean[][] constraints (int[][] sudoku ) {
int i = sudoku. length;
int j = sudoku [0]. length;
if (sudoku [i ][j ] != 0) {
constraints [i ][j ] = true; }
else {
constraints [i ][j ] = false; }
if (i != 0) {
generateConstraints (i- 1,j ); }
if (j != 0) {
generateConstraints (i, j- 1); }
return constraints;
} //end constraints
// Q2: TO COMPLETE
// METHOD solved: Determine if the grid was entirely solved
// GIVENS: A matrix of integers representing the sudoku
// ASSUMPTIONS: the sudoku contains 9x9 digits between 0 and 9 and it is not invalid
public static boolean solved (int[][] sudoku )
{
// DECLARATION OF VARIABLES / DATA DICTIONNARY
// BODY OF THE ALGORITHM
// RETURN RESULT
return false; // TO REPLACE!
}
// METHOD sudokusGrids: returns a list of initial Sudoku configurations (9x9).
private static int[][][] sudokuGrids ()
{
int[][][] grids; // array of arrays!
grids = new int[][][]
{
// 1st configuration, very easy...
{
{0, 2, 7, 4, 3, 5, 9, 6, 8},
{5, 9, 6, 7, 8, 2, 4, 1, 3},
{4, 3, 8, 9, 6, 1, 7, 2, 5},
{2, 5, 1, 8, 4, 3, 6, 9, 7},
{3, 8, 9, 6, 2, 7, 5, 4, 1},
{6, 7, 4, 1, 5, 9, 8, 3, 2},
{7, 6, 2, 3, 9, 8, 1, 5, 4},
{8, 4, 3, 5, 1, 6, 2, 7, 9},
{9, 1, 5, 2, 7, 4, 3, 8, 6},
},
// 2nd configuration, easy...
{
{5, 4, 0, 6, 0, 0, 3, 0, 0},
{0, 0, 0, 3, 1, 0, 0, 0, 4},
{0, 3, 9, 0, 7, 0, 1, 0, 0},
{0, 0, 0, 8, 0, 0, 0, 2, 1},
{0, 0, 0, 0, 0, 1, 6, 8, 3},
{2, 1, 8, 0, 3, 0, 0, 0, 0},
{1, 9, 0, 0, 0, 0, 4, 6, 0},
{0, 8, 0, 1, 0, 0, 0, 3, 0},
{6, 7, 3, 0, 4, 8, 0, 0, 0},
},
{
// 3rd configuration, realistic...
{0, 0, 0, 0, 0, 0, 0, 1, 0},
{1, 5, 0, 2, 0, 7, 0, 0, 4},
{0, 4, 0, 1, 0, 0, 0, 5, 8},
{7, 0, 0, 6, 0, 0, 0, 0, 1},
{0, 2, 4, 0, 8, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 5, 0, 7},
{4, 0, 0, 0, 3, 2, 0, 7, 0},
{0, 0, 0, 4, 0, 0, 0, 0, 0},
{6, 7, 2, 0, 1, 0, 4, 3, 0}
},
// 4th configuration, very difficult...
{
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 7, 0, 0, 0, 0, 0, 0, 1},
{9, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 5, 7, 0, 8, 4, 0, 0},
{7, 0, 0, 0, 4, 0, 8, 0, 0},
{0, 8, 0, 9, 6, 2, 0, 0, 5},
{8, 0, 0, 0, 7, 0, 0, 0, 4},
{6, 0, 2, 3, 0, 0, 0, 7, 0},
{0, 0, 0, 0, 0, 0, 6, 0, 0},
}
};
// RETURN RESULT
return grids;
}
}
|
I'd like it if you paid attention to Q5 (at the top) and Q3 (near the middle).
Meanwhile, I will dig in to see what exactly is an object. I had always thought that an object would be something more visual such as combo boxes, scroll bars, etc. I am most likely wrong. I am now inclined to think that an object can be an array, a vector, a method or a class.
Would it also be possible to expand a bit more on the subject of static methods?
[Edit]Hold that thought on static methods, I found what I might be needing in the Intro to Java tutorial. I'm reading it and trying to understand it right now.[/edit]
Thanks for your help. |
|
|
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Fri Nov 16, 2007 7:25 pm Post subject: Re: RE:Static Methods |
|
|
CooKieLord @ Fri Nov 16, 2007 4:55 pm wrote: As such, I cannot check to see how they call the algorithm. Is it static? Not static? Does it even matter in my case? Chances are, given how the rest of the code is structured, it is a static method as well.
CooKieLord @ Fri Nov 16, 2007 4:55 pm wrote: Meanwhile, I will dig in to see what exactly is an object. I had always thought that an object would be something more visual such as combo boxes, scroll bars, etc. I am most likely wrong. I am now inclined to think that an object can be an array, a vector, a method or a class. An object can be whatever you say it should be. When you create a class with non-static methods, then create an instance of that class, you are creating an object. The objects you create in programming are much like real life objects; where they have both behaviours and properties. For example, a car can drive (behaviour) and is a certain colour (property).
CooKieLord @ Fri Nov 16, 2007 4:55 pm wrote: Would it also be possible to expand a bit more on the subject of static methods?
[Edit]Hold that thought on static methods, I found what I might be needing in the Intro to Java tutorial. I'm reading it and trying to understand it right now.[/edit] Essentially static methods are useful when they're dealing with operations that don't need to be done on an object itself. Things like factory methods are typically useful most as static methods, as all they do is create objects, they don't modify an object.
The Introduction to Java tutorial is an excellent way to get started.
Think of it this way, let's say you have a database application that deals with records. Would it make more sense to have each record have its own delete() method, or to have a static delete(Record r) method?
Java: | Record r = new Record();
r.delete(); | vs.
Java: | Record r = new Record();
Record.delete(r); | I would say the first seems more "natural." |
|
|
|
|
![](images/spacer.gif) |
|
|