The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Need help with java
Discuss Need help with java in the Java Help forum on Dev Shed. Need help with java Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 24th, 2012, 09:01 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
Need help with java
i just wanted to know java. was looking at code found online. It supposed to run but its not. it does compile but gives me error in running that "you need public static void main"...new to java. so any one can explain thanks
Code:
// Ex 7.22 KnightsTour.java
// Simulates the Knight's Tour problem by Euler
public class KnightsTourTester {
public static void main(String[] args) {
KnightsTour kt = new KnightsTour();
// Now you can call any public methods from it
// But I don't know about this particular code in how it's intended to be used.
kt.begin(0,0);
kt.begin(7,7);
}
}
class KnightsTour {
int board[][] = new int[8][8]; // array for the chessboard
int accessibility[][] = new int[8][8]; // determine the accessibility
int[] horizontal = {2, 1, -1, -2, -2, -1, 1, 2}; // horizontal movement
int[] vertical = {-1, -2, -2, -1, 1, 2, 2, 1}; // vertical movement
int currentRow; // keep track of the current position
int currentColumn;
int moveNumber; // select the type of move
int moveCounter = 0; // count how many moves have taken place
int hori, vert; // for code shortening
int possibility = 0; // possible moves counter
int favorableMove, leastPos; // favorable move
int tempV, tempH; // temp values
public void begin(int a, int b){ // initializes the values
for(int i = 0; i < board.length; i++){ // initialize board to 0
for(int u = 0; u < board[i].length; u++){
board[i][u] = 0;
} // end inner for
} // end outer for
runAccess();
currentRow = 7;
currentColumn = 7;
move();
} // end begin
public void runAccess(){
for(int i = 0; i < accessibility.length; i++){ // counts the possible moves
for(int u = 0; u < accessibility[i].length; u++){
currentRow = i;
currentColumn = u;
access();
accessibility[i][u] = possibility;
}
}
}
public void move(){ // attempts to move the knight
board[currentRow][currentColumn] = moveCounter + 1; // mark position as "used"
System.out.printf("Row: %d Column: %d, moveType: %d, moveCount: %d\n", currentRow,
currentColumn, moveNumber, moveCounter); // print details for each move
tempH = currentColumn; // store values for access update
tempV = currentRow;
runAccess();
currentColumn = tempH; // reassign values
currentRow = tempV;
access();
moveNumber = favorableMove; // move to most favorable square
hori = horizontal[moveNumber]; // to shorten the code
vert = vertical[moveNumber];
currentRow += vert; // update current position
currentColumn += hori;
++moveCounter;
if(moveCounter == 16){
System.out.printf("Row: %d Column: %d, moveType: %d, moveCount: %d\n", currentRow,
currentColumn, moveNumber, moveCounter); // details for last move
runAccess();
finalPrint(); // print the final path
System.exit(1); // terminate
}
else{
move();
}
} // end move()
public void finalPrint(){ // prints the order where the knight travelled
for(int i = 0; i < board.length; i++){
for(int u = 0; u < board[i].length; u++){
System.out.printf("%2d ", board[u][i]);
}
System.out.println();
}
}
public void access(){
moveNumber = 0;
possibility = 0;
favorableMove = 0;
leastPos = 9;
for(int i = 0; i < 8; i++){
moveNumber = i; // tries to move the knight with eight different moves
hori = horizontal[moveNumber]; // to shorten the code
vert = vertical[moveNumber];
if(currentRow + vert < accessibility.length && currentColumn + hori
< accessibility[i].length && currentRow + vert >= 0 &&
currentColumn + hori >= 0){ // if move is legal...
if(board[currentRow + vert][currentColumn + hori] == 0){
++possibility; // if square is not yet visited,
// mark it as a possible move
if(accessibility[currentRow + vert][currentColumn + hori] <
leastPos){
favorableMove = i;
leastPos =
accessibility[currentRow + vert][currentColumn + hori];
}
}
}
} // end for
}
}
|

November 25th, 2012, 09:48 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | but gives me error in running that "you need public static void main"... |
The java program requires that the class you pass to it to start execution of the class have a main() method as described in the error message. That method is where the execution starts for the class when called by the java program.
|

November 25th, 2012, 10:16 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
oh. okay. I tried that it gives me output then. but i tried to change it to get run for 4x4 . but it gives me error. I think it should run for every board.
|

November 25th, 2012, 10:30 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
You'll have to post the full text of the error messages if you want help with them.
|

November 25th, 2012, 10:32 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
errors
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at KnightsTour.access(Main.java:118)
at KnightsTour.runAccess(Main.java:49)
at KnightsTour.begin(Main.java:35)
at KnightsTourTester.main(Main.java:7)
|

November 25th, 2012, 10:34 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at KnightsTour.access(Main.java:118) |
There is an array index that is past the end of the array at line 118. It is trying to index the 5th element in an array that has less than 5 elements.
|

November 25th, 2012, 10:42 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
if(currentRow + vert < accessibility.length && currentColumn + hori
< accessibility[i].length && currentRow + vert >= 0 &&
currentColumn + hori >= 0)
i see nothing wrong here though.
|

November 25th, 2012, 10:50 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
You need to look at the value of the index and the size/length of the array.
The max index value allowed is the length-1 of the array.
|

November 25th, 2012, 11:33 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
ok. whats wrong in my main. why it just prints out that (0,0) output not the other one?
|

November 25th, 2012, 11:36 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
What do you expect to see printed? It looks like the value of the variables being printed is zero. Where does the code give those variables values so they won't be zero?
|

November 25th, 2012, 11:47 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
should print this. starting at position (0,0) and (7,7)
1 4 23 20 57 6 51 44
22 19 2 5 50 43 58 7
3 24 21 56 27 60 45 52
18 29 26 49 42 55 8 59
25 14 35 28 61 48 53 46
30 17 32 41 54 39 62 9
13 34 15 36 11 64 47 38
16 31 12 33 40 37 10 63
7 10 51 42 39 12 37 32
52 43 8 11 50 33 40 13
9 6 53 64 41 38 31 36
54 59 44 49 34 61 14 29
5 20 63 60 45 30 35 24
58 55 48 21 62 25 28 15
19 4 57 46 17 2 23 26
56 47 18 3 22 27 16 1
just prints out the first one.
|

November 25th, 2012, 11:58 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Are you saying the code only prints out the first group of numbers, but it should print out both groups of numbers?
Time to debug the code to see why it doesn't print out all the values you want to see printed.
Add some println statements to the code to see why it doesn't print out what you want to see printed.
Print out a message when each method is entered and when it exits so you can see where the execution flow is going.
Last edited by NormR : November 25th, 2012 at 12:02 PM.
|

November 25th, 2012, 12:02 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 42 m 24 sec
Reputation Power: 0
|
|
|
yeah. i tried it should print both bunch of numbers. but it prints out just one.
|

November 25th, 2012, 12:11 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
You need enough println statements in the code so you can see where the code is executing.
If one println prints out and the next one does not print, then you know that the code stopped executing somewhere between those two println() calls.
Code:
System.out.println("location 123");
.... some more code
System.out.println("location 133");
If location 123 prints out and location 133 does NOT print out then you know the code stopped executing between those two println statements. Look at that code closely or add more println statements until you find where the code is stopping.
|

November 27th, 2012, 07:00 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 6 m 19 sec
Reputation Power: 0
|
|
|
Basic java program
I was wondering if someone could help me design this java program.
The game displays three symbols each showing either A, B or
C where each symbol is equally likely to occur. The player wins if all the symbols are the
same (eg B B B is displayed) or all three symbols are dierent (eg B A C is displayed).
The cost of playing one round playCost (an integer greater than or equal to zero) and the
number of rounds to play noOfRounds (an integer greater than or equal to zero) are input by
the user of the program. The program should disallow negative input. An amount which is
three times the cost of playing one round (i.e. 3 x playCost) is won if three symbols the same
are displayed. An amount of two times the cost of playing one round (i.e. 2 x playCost) is
won if the three symbols all dierent are displayed.
Each round the three symbols should be displayed. Additionally show the cost that round,
the total cost so far, the winnings that round, the total winnings so far and a running total
of total winnings minus total costs each round. After all rounds have been played print out
the number of rounds played and the average winnings (or losses) per round.
I know I have to use the math.random somewhere but im not sure where.
Thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|