Castellanos
Castellanos

Reputation: 157

How to use a method from another class inside a certain class?

I have two classes here, and inside my Player class, I want to get the total of the ones from the Scoresheet class. I do not know how to do it though.

public class ScoreSheet {

public int getOnes(ArrayList<Integer> die)
{
    for (int i = 0; i < die.size(); i++)
    {
        if (die.get(i) == 1)
        {
            ones++;
        }
    }
    return ones;
}


public class Player {

private int ones = 0;
private int twos = 0;
private int threes = 0;
private int fours = 0;
private int fives = 0;
private int sixes = 0;
private int threeOfKind = 0;
private int fourOfKind = 0;
private int fullHouse = 0;
private int smallStraight = 0;
private int largeStraight = 0;
private int yahtzee = 0;
private int chance = 0;



public void checkScores(ArrayList<Integer> die)
{
    ones = Player -> ScoreSheet.getOnes(<Integer> die); // this is wrong, need to know
                                                        // how to get total                    
}

Upvotes: 1

Views: 54

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

The Player class needs a ScoreSheet variable that is initialized to the current ScoreSheet object. The variable can be set to the correct object via a constructor parameter or a setScoreSheet(ScoreSheet scoreSheet) setter method. Player can then call the getOnes(...) method or other ScoreSheet methods on the ScoreSheet variable when needed.

Upvotes: 1

Related Questions