-
Calling on a class
Hey, my teacher just introduced us to using multiple classes and I'm not sure I get it. I'm supposed to be making a game of blackjack. I'm testing how to call on the classes he gave us for the moment. I'm doing it wrong, I just don't know how. It comes up with an error that says I'm calling on a non-static variable from a static class. When I take the static out of the main method it tells me there is a NoSuchMethodError. I know what that is, I just don't know how to fix it. Here's my code:
Code:
public class GameOf21 {
public static void main(String[] args) {
final int SENTINEL = 0;
int replay = 1;
int points = 0;
int card1 = new PlayingCard();
while(SENTINEL != replay){
points = card1.pointValue();
System.out.println(points);
}
}
public class PlayingCard {
private int card;
public int PlayingCard() {
card = (int)(13 * Math.random() + 1);
return(card);
}
public int pointValue() {
int points = 0;
switch(card) {
case 1: points = 1; break; //ace
case 2: points = 2; break; //2
case 3: points = 3; break; //3
case 4: points = 4; break; //4
case 5: points = 5; break; //5
case 6: points = 6; break; //6
case 7: points = 7; break; //7
case 8: points = 8; break; //8
case 9: points = 9; break; //9
case 10: //10
case 11: //Jack
case 12: //Queen
case 13: points = 10; break; //King
}
return(points);
}
public String getName() {
String face = "";
switch(card) {
case 1: face = "Ace"; break;
case 2: face = "2"; break;
case 3: face = "3"; break;
case 4: face = "4"; break;
case 5: face = "5"; break;
case 6: face = "6"; break;
case 7: face = "7"; break;
case 8: face = "8"; break;
case 9: face = "9"; break;
case 10: face = "10"; break;
case 11: face = "Jack"; break;
case 12: face = "Queen"; break;
case 13: face = "King"; break;
}
return(face);
}
public String toString() {
return(getName());
}
}
}
-
The problem is, that your PlayingCard class doesn't have a constructor, public int PlayingCard() is not a constructor, but a method.
-
OK here's my new code, but it still tells me that int cannot be dereferenced.
Code:
public class GameOf21 {
public static void main(String[] args) {
final int SENTINEL = 0;
int replay = 1;
int points = 0;
int card1 = 0;
int card2 = 0;
int card3 = 0;
while(SENTINEL != replay){
points = card1.pointValue(); //this is the line that the error references
System.out.println(points);
}
}
public class PlayingCard {
private int card;
public PlayingCard() {
card = (int)(13 * Math.random() + 1);
}
public int pointValue() {
int points = 0;
switch(card) {
case 1: points = 1; break; //ace
case 2: points = 2; break; //2
case 3: points = 3; break; //3
case 4: points = 4; break; //4
case 5: points = 5; break; //5
case 6: points = 6; break; //6
case 7: points = 7; break; //7
case 8: points = 8; break; //8
case 9: points = 9; break; //9
case 10: //10
case 11: //Jack
case 12: //Queen
case 13: points = 10; break; //King
}
return(points);
}
-
never mind that was a stupid error. don't worry about it.
-
You should really read about classes and objects, card1 is of the type int, but you're trying to access the pointsValue() method from the PlayingCard class. Try to write down the proccess of your program on paper, and determine what variables you need, and what type they should be. Also, your pointsValue() method is really wierd, wouldn't this do the same job?
Code:
public int pointsValue() {
if(card > 10) return 10;
return card;
}
-
You want card1 not to be an Integer, but rather, an instance of your beautiful PlayingCard class. Try
Code:
PlayingCard card1 = new PlayingCard ();
-
Alright. Revised code, but I'm getting the same error I was at the start.
Code:
public class GameOf21 {
public static void main(String[] args) {
final int SENTINEL = 0;
int replay = 1;
int points = 0;
PlayingCard card1 = new PlayingCard();
PlayingCard card2 = new PlayingCard();
PlayingCard card3 = new PlayingCard();
while(SENTINEL != replay){
points = card1.pointValue();
System.out.println(points);
}
}
public class PlayingCard {
private int card;
public PlayingCard() {
card = (int)(13 * Math.random() + 1);
}
public int pointValue() {
int points = 0;
if (card > 10){
return(10);
}else {
return(card);
}
}
public String getName() {
String face = "";
switch(card) {
case 1: face = "Ace"; break;
case 2: face = "2"; break;
case 3: face = "3"; break;
case 4: face = "4"; break;
case 5: face = "5"; break;
case 6: face = "6"; break;
case 7: face = "7"; break;
case 8: face = "8"; break;
case 9: face = "9"; break;
case 10: face = "10"; break;
case 11: face = "Jack"; break;
case 12: face = "Queen"; break;
case 13: face = "King"; break;
}
return(face);
}
public String toString() {
return(getName());
}
}
}
-
you really SHOULDNT have two classes (PlayingCard and GameOf21) in the same file.
Quick fix: instead of "public class PlayingCard", say "static class PlayingCard"
Real fix: make another file called PlayingCard.java and stick your "public class PlayingCard"
class there.
-
I'd really like to put it as 2 files, but my teacher wants it all in one. Thanks though. I'll try that.
-
This would be pushing it, but for the heck of it... Split 'em up and stick 'em in a ZIP file! One file, and yet multiple files...
DISCLAIMER
Code:
/* I IN NO WAY ACCEPT RESPONSIBILITY FOR ANY DAMAGES TO YOUR EGO OR
* SELF-CONFIDENCE RESULTING FROM TAKING THIS ADVICE. ANY DAMAGE IS
* PURELY COINCIDENTAL OR RESULTING FROM A LACK OF (UN?)COMMON SENSE.
*/
Cheers,
SingingBoyo
-
On a more serious note...
This may not be relative to the bug you're experiencing, but you are going to hit an infinite loop in your main method. Or rather, you have one. your while loop is basically this...
which is always true. You need to change the values somewhere in there. and points will always be equal to card1.pointsValue() after the first loop. After you work out your syntax/compilation bugs, you need to sit down and work out the process of your program on paper/in a regular text file, and write code accordingly.
-
I know about that. There's going to be a user input where the program asks if you want to retry. I'll fix that though, and see if it helps. I'm not anywhere close to finished. I like to fix my bugs as I go along so I can find them a little bit easier.
-
Thanks a lot iluxa. I used your quick fix and it worked. The problem with your real fix is that its supposed to be in a zip file with two other programs. I don't think you can make a zip file inside a zip file.