Results 1 to 13 of 13
Thread: Calling on a class
- 03-23-2010, 05:52 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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:
Java 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()); } } }
- 03-23-2010, 06:07 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
The problem is, that your PlayingCard class doesn't have a constructor, public int PlayingCard() is not a constructor, but a method.
- 03-23-2010, 06:35 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
OK here's my new code, but it still tells me that int cannot be dereferenced.
Java 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); }
- 03-23-2010, 06:39 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
never mind that was a stupid error. don't worry about it.
- 03-23-2010, 06:39 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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?
Java Code:public int pointsValue() { if(card > 10) return 10; return card; }
- 03-23-2010, 06:40 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
You want card1 not to be an Integer, but rather, an instance of your beautiful PlayingCard class. Try
Java Code:PlayingCard card1 = new PlayingCard ();
- 03-23-2010, 07:11 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
Alright. Revised code, but I'm getting the same error I was at the start.
Java 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()); } } }
- 03-23-2010, 07:15 PM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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.
- 03-24-2010, 01:16 AM #9
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
I'd really like to put it as 2 files, but my teacher wants it all in one. Thanks though. I'll try that.
- 03-24-2010, 01:51 AM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
Cheers,Java 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. */
SingingBoyoIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-24-2010, 01:55 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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.Java Code:while(0!=1)
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-24-2010, 10:43 PM #12
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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.
- 03-24-2010, 10:58 PM #13
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
Similar Threads
-
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a class method from another class
By caro in forum New To JavaReplies: 4Last Post: 06-10-2009, 01:12 AM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Inner Class and Calling Values
By a45b22chp in forum New To JavaReplies: 3Last Post: 04-25-2008, 08:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks