Results 1 to 13 of 13
  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default 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()); 
    } 
    }
        }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The problem is, that your PlayingCard class doesn't have a constructor, public int PlayingCard() is not a constructor, but a method.

  3. #3
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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); 
    }

  4. #4
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    never mind that was a stupid error. don't worry about it.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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;
    }

  6. #6
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    You want card1 not to be an Integer, but rather, an instance of your beautiful PlayingCard class. Try

    Java Code:
    PlayingCard card1 = new PlayingCard ();

  7. #7
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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()); 
    } 
        }
    
    }

  8. #8
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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.

  9. #9
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    I'd really like to put it as 2 files, but my teacher wants it all in one. Thanks though. I'll try that.

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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
    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.
     */
    Cheers,
    SingingBoyo
    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!

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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...
    Java Code:
    while(0!=1)
    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.
    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!

  12. #12
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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.

  13. #13
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  2. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 02:12 AM
  3. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 08:35 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  5. Inner Class and Calling Values
    By a45b22chp in forum New To Java
    Replies: 3
    Last Post: 04-25-2008, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •