Results 1 to 4 of 4
Thread: Inheritance method problem
- 11-10-2009, 07:24 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Inheritance method problem
Hi, I am re-making a TicTacToe board game. It inherits the first game I made. Right now I am trying to override the move() method. The new board allows players to choose what to use if they don't want to use X and/or O. The problem is that whenever I use the move method, a System.err comes up saying "ERROR: The ch variable is neither X nor O."
That is the correct message for the superclass, but my subclass is different. But even weirder is I put a System.out saying "SUPERCLASSERRORCALLED" that never prints when that error message comes up. So I'm pretty lost as to why that message is popping up. The subclass System.err will still output also if it applies. So I'll get two System.err messages sometimes.
Here is the move() method for the sub-class
Here is the main I am using for testJava Code://override public boolean move(char ch, int row, int col) { boolean result = false; if(ch == getX() || ch == getO()) { //check ch for X or O if(getLastMoved() == ch) //check if player is moving twice System.err.println("Sorry, you cannot move twice."); else { if(board[row][col] != getX() && board[row][col] != getO()) { //if the space is unoccupied board[row][col] = ch; //make the move result = true; //change result and print outcome System.out.println("The move was legal."); setLastMoved(ch); if(checkForWin(ch)) //if the game has been won, print the winner System.out.println("The game has been won by " + ch + "!"); } else { //if the spot is taken, print an error message System.err.println("ERROR: That spot is already taken!! Please chose again."); } } } else { //if ch isn't xChar or oChar, print an error message System.err.println("ERROR: The ch variable is neither " + getX() + " nor " + getO() +"."); System.out.println("SUBCLASSERROR"); } return result; }
The output I get is --Java Code:public static void main(String args[]) { BetterTTTBoard game = new BetterTTTBoard('S', 'G'); game.move('X', 1, 1); game.move('G', 2, 0); game.move('S', 0, 0); System.out.println(game.toString()); }
This is the error message from the base classJava Code:ERROR: The ch variable is neither S nor G. SUBCLASSERROR The move was legal. ERROR: The ch variable is neither X nor O. The move was legal. ERROR: The ch variable is neither X nor O. S G
Any help would be appreciated.Java Code:else { //if ch is illegal, print error message System.err.println("ERROR: The ch variable is neither X nor O."); System.out.println("SUPERCLASSERROR"); }
- 11-10-2009, 07:47 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Nevermind! Found the problem!
- 11-10-2009, 07:48 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
We are missing a lot of information here.
First of all, is the subclass called BetterTTTBoard? Or is that the name of the superclass. If its the superclass, then that is immediately the problem.
We'd also need to see what the getX and getO methods do, as well as what move looks like in the superclass. A look at your constructor for your subclass wouldn't hurt, either.
Edit: Ah you figured it out, mind explaining what you did wrong for others to learn from?
- 11-10-2009, 08:30 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Yeah well in the super checkForWin(char ch) method, I had an if statement to check if ch was X or O. So in the move(...) method of BetterTTTBoard, it was calling my checkForWin(char ch) in the super class. All I needed to do was override the checkForWin(char ch) and change the check to
Java Code:if(ch == getX() || ch == getO())
Similar Threads
-
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 12:13 AM -
Method problem
By Jeff6461 in forum New To JavaReplies: 1Last Post: 03-02-2009, 06:48 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Method inheritance
By JT4NK3D in forum New To JavaReplies: 7Last Post: 06-02-2008, 06:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks