Stuck - simple program
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 elds 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.println("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.
It's hard to know what's going on without code, but from what you've told us and my crystal ball, we know that Game has two Player objects. It may also have a variable which tells us which Player's turn it is.
I would think that Game will get the Player's name by calling a getName() method or something similar on the Player whose turn it is. Does Player have such a method?
While Game has only one play() method, I would assume that the Player class also has a similarly named method. This is the one that Game will call repeatedly on alternating players (when you hear "repeatedly" in programming -- think loop). So while you're right there is only one play() method for game, you're not calling this on the players but instead are calling their own play() methods.
To call a method on a Player class simply do something like player1.play();
Are you sure a loop is needed?
so something like that would be wrong?
private void play()
System.out.println("You're player one, its your turn first");
System.out.println("You're player two, its your turn next");
Am I sure about a loop? Heck no. It's your program and none of us know much about the program or requirements, so I'm sure about nothing.
If the players will only do one turn each, then no loop is needed. If the players need to play until a certain score is reached or a certain number of turns are played then you'll likely need a loop of some format or another.
Your code looks OK on first glance, but I am a little concerned that you get the player's score but don't do anything with them such as store them in a variable and then display them.