Results 1 to 9 of 9
  1. #1
    Actaeonis is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Referring to other classes.

    Hello! I have a problem concerning referring to other classes.
    I have two classes, one called Blackjack which is the main class and one called Spelerkaarten (which is Dutch for playercards). In this last class I give my card a certain value with an if-statement. Now I would like to use these values in my other class, blackjack. And this is the part where I'm encounter a problem. It seems that what I'm doing doesn't work.

    Java Code:
    package blackjack;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Blackjack {
    
     
        public static void main(String[] args) throws IOException {
             
       
            
            
            
                    
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            
            
            Spelerkaarten kaartenspeler = new Spelerkaarten();
           
           
            System.out.println("Welkom bij blackjack! Wat is je naam?");
            String naam = br.readLine();
            System.out.println("Van harte welkom " + naam + " je zal vandaag opnemen tegen onze supercomputer in het spelletje blackjack!");
            System.out.println("Hier volgen je eerste drie kaarten!");
            System.out.println(kaartenspeler.spelerkaart1);
            System.out.println(kaartenspeler.spelerkaart2);
            
            
            
            
            
            
        }
    }
    Java Code:
            
    package blackjack;
    
    
    
    public class Spelerkaarten {
        
    
    public static void main(String[] args)  {
        
            int spelerwaarde1 = (int) Math.floor((Math.random()*1)+13);
            int spelerwaarde2 = (int) Math.floor((Math.random()*1)+13);
            
            String spelerkaart1 = "";
            String spelerkaart2 = "";
            
            if (spelerwaarde1 < 10) {
               spelerkaart1 = "Uw eerste kaart is een " + spelerwaarde1 + "!";
    }
            if (spelerwaarde1 == 11) {
               spelerkaart1 = "Uw eerste kaart is een boer!";
            
    }
            if (spelerwaarde1 == 12) {
               spelerkaart1 = "Uw eerste kaart is een dame!";   
    }
            if (spelerwaarde1 == 13) {
               spelerkaart1 = "Uw eerste kaart is een heer!";
              
            
    }
            if (spelerwaarde2 < 10) {
               spelerkaart1 = "Uw tweede kaart is een " + spelerwaarde2 + "!";
    }
            if (spelerwaarde2 == 11) {
               spelerkaart1 = "Uw tweede kaart is een boer!";
            
    }
            if (spelerwaarde2 == 12) {
               spelerkaart1 = "Uw tweede kaart is een dame!";   
    }
            if (spelerwaarde2 == 13) {
               spelerkaart1 = "Uw tweede kaart is een heer!";
              
            
    }
            
    }
    }
    Thanks in advance!
    Last edited by Actaeonis; 11-29-2012 at 05:19 PM. Reason: Wrong code

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Referring to other classes.

    That code there, in main(), does nothing with the 'kaartenspeler' (it's a variable, so should start with a lowercase letter) reference.
    All it does is create that object, then a couple of ints and then a couple of Strings.
    None of them are related at all.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Actaeonis is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Referring to other classes.

    Quote Originally Posted by Tolls View Post
    That code there, in main(), does nothing with the 'kaartenspeler' (it's a variable, so should start with a lowercase letter) reference.
    All it does is create that object, then a couple of ints and then a couple of Strings.
    None of them are related at all.
    I'm sorry but I don't really know what you mean. However I noticed I forgot a part of my code, I edited it.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Referring to other classes.

    OK, so which of those two main()'s are you actually using as the entry point to your code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Actaeonis is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Referring to other classes.

    Quote Originally Posted by Tolls View Post
    OK, so which of those two main()'s are you actually using as the entry point to your code?
    That class Blackjack serves as the main class, this is the class where I want to generate the output. The first three cards I want to let the player draw are calculated in the class Spelerkaarten, and printed in Blackjack. That's why I'd want to take those strings from Spelerkaarten to Blackjack. I hope that awnsers your question.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Referring to other classes.

    Except Spelekarten has not attributes, and no methods other than that main() one, which is not how these things are normally written.

    Do you know how classes and objects work?
    I'm just trying to see where you are in your learning.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Actaeonis is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Referring to other classes.

    Quote Originally Posted by Tolls View Post
    Except Spelekarten has not attributes, and no methods other than that main() one, which is not how these things are normally written.

    Do you know how classes and objects work?
    I'm just trying to see where you are in your learning.
    I do not know, no.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Referring to other classes.

    Then I'm afraid you're going to need the tutorials.
    The basics at the very least.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: Referring to other classes.

    He's right, you need to start spending as much time as you can on OOP tutorials/books. It's not that you don't use the same programming syntax etc. that you already know, but thinking in objects is a whole different way of programming. You build your programs by creating classes, out of those classes you make objects, which are instances of those classes.

    Say you have a "Person" class which has some field values(variables/objects of other classes)like height, weight, eyeColor etc. and methods to get them(called "getters", gets the field values, fx getHeight() ) methods to set them(called setters/mutators, fx setHeight() ) an object of that class would be a person with a certain height etc. this doesn't change the person class at all though, it just makes a "reference" to the person class (the class never changes) only the objects of the class(the object of the class is a "reference" to the class) you make changes to their field values, to reflect various people-objects you made from your person class.

    like this:
    Java Code:
    Person joe = new Person();
    joe.setHeight( 100 );
    joe.setWeight( 120 );
    joe.setEyeColor( "green" );
    now the object Joe is an instance of the Person class, but with his own unique field values. ( a field value is just a variable that's listed in the top of the class above everything else, it can be a primitive type or another class object)
    Think of it as creating "organisms/machines" that can do many jobs and contain field values (properties) which can be manipulated.

    Anyways to fully understand you need to take the time to read on it alot, read about objects and when you feel you understand, read about inheritance.

    Just a beginner myself but this is my advice.

    A typical class is set up like this.

    Java Code:
     
    public class Person {
    
        // Field values (properties of each object).
    
       <field value 1>;
       <field value 2>;
        etc....
       
        // Constructor(s) 
       public Person() {
       
       }
       
       // more contructors.... if u need them.
    
    
        // Methods.
        
        public void set<fieldvalue1>( <data type> newValue ) {
           this.<field value1> = newValue;
        }
            // lots of methods, getters and setters etc.   
    }
    Last edited by ObedMarsh; 11-30-2012 at 04:31 PM.

Similar Threads

  1. Referring to enum values with instantiation of an object
    By Aggnavarius in forum New To Java
    Replies: 1
    Last Post: 09-18-2012, 09:49 AM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. Replies: 3
    Last Post: 10-24-2011, 06:03 PM
  4. referring a unknown object in its subclass
    By Sliks in forum Advanced Java
    Replies: 3
    Last Post: 06-18-2011, 12:20 PM
  5. Referring to a super class
    By blug in forum New To Java
    Replies: 7
    Last Post: 03-20-2011, 05:06 AM

Posting Permissions

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