Results 1 to 11 of 11
  1. #1
    anyone is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default How to run method from a different object?

    Hi,

    I am making a simple card game and am having a problem running a method from a different object.
    I have a class called decks which has a method call nextCard, I want to run this method from a class called dealer.
    I understand the idea and think I am just stuck with the syntax.

    Java Code:
    public class dealer
    {
        
        private decks decks;
    
        /**
         * Constructor for objects of class dealer
         */
        public dealer(decks newDeck)
        {
            decks = newDeck;
        }
    
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
    
        public void nextCard()
        {
         decks nextCard=decks.nextCard();
        }
        
    }
    When I create a new instance of dealer I am able to select an instance of decks so all the information is inherited. But nextCard in dealer doesn't compile with the error "incompatible types - found void but expected decks".

    Any help will be much appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by anyone View Post
    Hi,

    I am making a simple card game and am having a problem running a method from a different object.
    I have a class called decks which has a method call nextCard, I want to run this method from a class called dealer.
    I understand the idea and think I am just stuck with the syntax.

    Java Code:
    public class dealer
    {
        
        private decks decks;
    
        /**
         * Constructor for objects of class dealer
         */
        public dealer(decks newDeck)
        {
            decks = newDeck;
        }
    
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
    
        public void nextCard()
        {
         decks nextCard=decks.nextCard();
        }
        
    }
    When I create a new instance of dealer I am able to select an instance of decks so all the information is inherited. But nextCard in dealer doesn't compile with the error "incompatible types - found void but expected decks".

    Any help will be much appreciated.
    The method nextCard in your decks class is defined to return a void (i.e. nothing, check that class) but you treat it to return a 'decks' type value; of course that does not compute and makes your compiler whine about it.

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    The method nextCard in your decks class is defined to return a void (i.e. nothing, check that class) but you treat it to return a 'decks' type value; of course that does not compute and makes your compiler whine about it.
    I may be reading it wrong, but I don't see the code for the nextCard method of the deck class. Rather I see a similarly named method from the 'dealer' class. I'm not sure what deck's method will return, but I'm hoping that it will be a Card. Regardless, I agree, that it is very unlikely to return a deck object, because if it does, this program is completely borked.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    I may be reading it wrong, but I don't see the code for the nextCard method of the deck class. Rather I see a similarly named method from the 'dealer' class. I'm not sure what deck's method will return, but I'm hoping that it will be a Card. Regardless, I agree, that it is very unlikely to return a deck object, because if it does, this program is completely borked.
    I didn't see the decks.nextCard() code either but the compiler's error method gave away its return type: void, and it is used as if it returns an entire decks value; the compiler immediately protested against such blasphemy of course ;-)

    kind regards,

    Jos

  5. #5
    anyone is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the help but I am not quite getting this.
    In the decks class I have a method called nextCard that uses system.out.println to print the next card in the deck.
    All I want to do is call this method from from the dealer class. Is this even possible?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    I didn't see the decks.nextCard() code either but the compiler's error method gave away its return type: void, and it is used as if it returns an entire decks value; the compiler immediately protested against such blasphemy of course ;-)
    Ah, I didn't see the compiler protest portion. It indeed doesn't protest too much. Thanks.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by anyone View Post
    Thanks for the help but I am not quite getting this.
    In the decks class I have a method called nextCard that uses system.out.println to print the next card in the deck.
    All I want to do is call this method from from the dealer class. Is this even possible?
    Yes, you certainly can call it, but don't try to call it and try to use its return type to create a new decks object as you did in your code above.

  8. #8
    anyone is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yes, you certainly can call it, but don't try to call it and try to use its return type to create a new decks object as you did in your code above.
    Great. This is my problem and what I don't understand(clearly there is lots more I don't understand as well!). The method was just one of many guesses to get it to work. I don't know exactly which bit of the code is wrong.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got a lot of reading and studying to do.

    You're doing this:
    Java Code:
    decks nextCard = decks.nextCard();
    which calls deck's nextCard method but tries to set the result returned (here void or nothing) into a decks object called nextCard. Dont' do that. If you want to just call the method, then just call the method.

    Java Code:
    decks.nextCard();
    Incidently, your class decks and variable decks have the same name which is very bad form. The class should be capitalized, Decks. If it were my program, I'd make it singular Deck (and the variable deck).

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by anyone View Post
    Great. This is my problem and what I don't understand(clearly there is lots more I don't understand as well!). The method was just one of many guesses to get it to work. I don't know exactly which bit of the code is wrong.
    Calling a method (on an object) in another class basically runs as follows:

    Java Code:
    public class AClass {
       // constructor and other stuff here ...
    
       // everybody can call this method:
       public int aMethod(int x) { // a silly example method
          return x+41;
       }
    }
    // ...
    public class AnotherClass {
       private AClass anObject;
       public AnotherClass(AClass object) { // contructor
          anObject= object; // keep a reference to the other thing
       }
       // ...
       public void method() {
          System.out.println(anObject.aMethod(1)); // call method in other class
       }
    }
    This is just a silly example but it shows how to call a method on an object from another class; all the object needs is a reference to that other object.

    kind regards,

    Jos
    Last edited by JosAH; 01-16-2010 at 09:04 PM.

  11. #11
    anyone is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you both so much for this, you have both been a massive help. It makes sense now.

Similar Threads

  1. returning an object from a method
    By bigj in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 12:39 PM
  2. Calling Object.method
    By Fedor in forum New To Java
    Replies: 1
    Last Post: 04-11-2009, 01:44 PM
  3. Return an object to use in another method
    By TidusSolan in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 08:00 PM
  4. [SOLVED] Non-synchronized instance method of an Object
    By piyu.sha in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2008, 06:35 AM
  5. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:10 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
  •