Results 1 to 12 of 12
Thread: Help with classes
- 10-11-2010, 01:07 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Help with classes
I'm quite new to Java and I have to write a class that works with two previously written files (one class by me, one by my lecturers).
Here is the code written by my lecturers:
Here is a class written previously by myself:Java Code:import java.util.Scanner; public class CardGuessingGame { private static Player house; private static Player player; private static int wins; private static int loses; private static Scanner scan; /** * Initialise the game state. */ public static void init() { house = new Player("House"); player = new Player("Player"); wins = 0; loses = 0; scan = new Scanner(System.in); } /** * Used to simulate playing our game. */ public static void playGame() { char option, playAgain; int houseHandStrength, playerHandStrength; System.out.println("Welcome to our card guess 1.0 game!"); System.out.println(); do { // Deal cards to the house and player. house.acceptDeal(new Card(), new Card()); player.acceptDeal(new Card(), new Card()); System.out.println(house); // Determine whether the player wants to play this hand. do { System.out.print("Deal cards? (Y/N) "); option = Character.toLowerCase(scan.next().charAt(0)); } while (option != 'n' && option != 'y'); if (option == 'y') { System.out.println(player); // Display hand strength of both players. houseHandStrength = house.getHandStrength(); playerHandStrength = player.getHandStrength(); System.out.println("The dealer's hand strength is: " + houseHandStrength); System.out.println("Your hand strength is: " + playerHandStrength); System.out.println(); // If the player has a stronger hand. if (player.getHandStrength() > house.getHandStrength()) { System.out.println("** You won the hand! **"); wins++; } else { System.out.println("The house wins this round!"); loses++; } } // Display the win/lose statistics. System.out.println("Current wins: " + wins); System.out.println("Current loses: " + loses); // Prompt whether the user wants to play again. do { System.out.print("Would you like to play again? (Y/N) "); playAgain = Character.toLowerCase(scan.next().charAt(0)); } while (playAgain != 'n' && playAgain != 'y'); System.out.println(); System.out.println("*******************************************************"); } while (playAgain == 'y'); System.out.println(); System.out.println("Thank you for playing!"); } public static void main(String[] args) { init(); playGame(); } }
And here is what I've written for this new piece of code:Java Code:public class Card { int value; String suit; int suitValue; String valueName; String cardName; public Card() { value = (int)(Math.random() * 13) + 1; suitValue = (int)(Math.random() * 4) + 1; getSuit(); getValue(); getValueName(); toString(); } public String getSuit() { //Selects a suit using a random int if(suitValue == 1) suit = "Spades"; if(suitValue == 2) suit = "Clubs"; if(suitValue == 3) suit = "Hearts"; if(suitValue == 4) suit = "Diamonds"; return suit; } public int getValue() { return value; } public String getValueName() { if(value == 1) valueName = "Ace"; else if(value == 11) valueName = "Jack"; else if(value == 12) valueName = "Queen"; else if(value == 13) valueName = "King"; return valueName; } public String toString() { if(value == 1) cardName = ("The " + valueName + " of " + suit); else if(value == 11) cardName = ("The " + valueName + " of " + suit); else if(value == 12) cardName = ("The " + valueName + " of " + suit); else if(value == 13) cardName = ("The " + valueName + " of " + suit); else cardName = ("The " + value + " of " + suit); return cardName; } }
I use JCreator LE to compile the program, and I get error messages saying "cannot find symbol constructor Player(java.lang.String)" as part of the CardGuessingGame.java, and "cannot find symbol variable getValue" for the Player class under the getHandStrength method.Java Code:public class Player { private Card card1; private Card card2; private int handStrength; public String Player(String name) { return name; } public Card acceptDeal(Card cardOne,Card cardTwo) { card1 = cardOne; card2 = cardTwo; return cardOne; return cardTwo; } public int getHandStrength() { handStrength = (card1.getValue + card2.getValue); return handStrength; } }
I have no idea what I am doing wrong, can someone please give me a pointer in the right direction? Thank you in advance.
- 10-11-2010, 01:28 AM #2
currently what you probably mean to be the constructor is a method.Java Code:public String Player(String name) { return name; }
constructors don't have a return type right.
so perhaps instead of this, change the player signature to be that of a constructor, and create a getName() method to return name separately
Java Code:private String name; public Player(String name) { this.name = name; } public String getName() { return this.name; }
- 10-11-2010, 01:28 AM #3
Can you post the full text of the error messages?
Your edited verions left off some valuable info, like showing the line with the error.
Is getValue a variable or a method? The name would look like a method.
Mehtod calls end with ()
- 10-11-2010, 01:38 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Errors for the CardGuessingGame.java:
cannot find symbol constructor Player(java.lang.String) - Line 18 and 19
I totally forgot about putting the () after a method call, so now that I've changed that, the only error I get for the Player.java is:
unreachable statement - Line 19
I get no errors for the Card.java
- 10-11-2010, 01:41 AM #5
Where is the unreachable statement? What file is it in?
Is it after a return statement?
- 10-11-2010, 01:45 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Java Code:public class Player { private String name; private Card card1; private Card card2; private int handStrength; public String Player(String name) { return name; } public Card acceptDeal(Card cardOne,Card cardTwo) { card1 = cardOne; card2 = cardTwo; return cardOne; [COLOR="Red"]return cardTwo;[/COLOR] //unreachable statement here } public int getHandStrength() { handStrength = (card1.getValue() + card2.getValue()); return handStrength; } }
- 10-11-2010, 01:47 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
I should also mention that I shouldn't have to modify the CardGuessingGame.java file in any way for it to work, if it doesn't work, the problem lies with the Player.java file.
- 10-11-2010, 01:53 AM #8
Can you explain when the second return would be executed?
When the first return is executed, that's the end of execution of statements in that method.
- 10-11-2010, 01:54 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
So how would I be able to return both?
EDIT: I changed the acceptDeal to type void, I don't have to return anything. I still have the two errors with the CardGuessingGame.java fileLast edited by Exothesis; 10-11-2010 at 01:59 AM.
- 10-11-2010, 01:57 AM #10
Please explain the logic of the method.
What does it do?
and what is it supposed to return?
It looks like it should be called setCards() because it only saves the two cards
-
- 10-11-2010, 02:00 AM #12
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
gui classes
By phantom135 in forum AWT / SwingReplies: 12Last Post: 08-05-2010, 05:28 PM -
Three classes help!!
By arrech326 in forum New To JavaReplies: 4Last Post: 11-24-2009, 06:34 AM -
Help with classes
By gnarly hogie in forum New To JavaReplies: 14Last Post: 10-10-2008, 02:29 PM -
Get name of available classes
By escuja in forum CLDC and MIDPReplies: 0Last Post: 07-26-2008, 12:03 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 07:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks