need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dirtycash
    New Member
    • Nov 2007
    • 2

    #1

    need help

    Hi guys im trying to create a simple card game. This is the last class i need to create in order for my assignment
    to be finished and im very stuck!
    Here are my instructions -


    The Game class has two fields representing Player one and Player two.
    The constructor for this class has two parameters which are the names of the two players. The
    constructor will initialise the two fields.
    The Game class has a single method called play which has no parameters and does not return
    a value. This method should print out a suitable introduction and then:

    1. Instruct player one to play first. The instruction should give the name of player one.
    so something like system.out.prin tln("Player One needs to go first") but how to i call the getName method i have which is in another class and connect it here?
    2. Invoke the play method of Player one.
    this bit i dont understand, because there is simply one play method right? The instructions dont indicate 2 seperate
    play methods for each player?
    3. Announce the score for Player one.
    Simply again, im not sure how to call a method which is in another class.

    any input would be appreciated.
  • Aardsquid
    New Member
    • Nov 2008
    • 4

    #2
    Well, to call a method in another class is quite simple:
    Code:
    OtherClass.method ( /*args*/ );
    Originally posted by dirtycash
    Invoke the play method of Player one.
    It looks like the instructions at some point ask to create a Player class. Make it private and put it inside Game; add the fields:
    Code:
    private class Player {
        private String name;
        private int score;
    }
    The constructor needs to set the name, that's it. It might be good to have a get and set method for "name" as well. If you migrate the play method into Player, you can instantiate a Player in your Game class:
    Code:
    Player player1 = new Player (TheOtherClass.getName ());
    Then get the score:
    Code:
    int score = player1.play ();
    A second player can also be created and referenced using the same methods.

    I hope this helps.

    Comment

    Working...