1

I'm working on a program that allows a user to choose a home and away football team between four teams. I created a generic superclass team that defines the points assigned per safety/field goal/touchdown. A random number is generated and then based on that number the program steps through a conditional if/else statement to determine action and points.

This is in the SuperClass:

public void possessionPoints()
{
    if(points<lowNopoints){
        score = noPoints;
        totalScore = totalScore + score;
        System.out.println("No points, plus " + score);
    }
    else if(points<lowSafetypoint){
        score = safetyPoint;
        totalScore = totalScore + score;
        System.out.println("Safety, plus" + score);
    }
    else if(points<lowFieldgoal){
        score = fieldGoal;
        totalScore = totalScore + fieldGoal; 
        System.out.println("Field goal, plus" + score);
    }
    else{
        score = touchDown;
        totalScore = totalScore + touchDown;
        System.out.println("Touchdown, plus" + score);
    }

    ArrayList<Integer> totalScore;
    totalScore = new ArrayList<>();
    totalScore.add(score);

    //the sum score
    int sum = totalScore.stream().mapToInt(Integer::intValue).sum();
    System.out.println("Current score is: " + sum);
}

Note: above totalScore is intialized as public static int totalScore = 0;

Throughout it all, I want to keep track of totalScore. I have this setup in my superclass, however, when the program is run it adds up the score through the entire game and does not differentiate between teams.

Output:

Home team action. No points, plus 0 Current score: 0

Away team action. Field goal, plus3 Current score: 3

Home team action. Field goal, plus3 Current score: 6

Away team action. Field goal, plus3 Current score: 9

Home team action. Safety, plus2 Current score: 11

Also, if it helps, this is all that I set in the each subclass for the other teams below. I do not do anything with totalScore.

public class PackersSub extends GenericSuper{
public PackersSub()
{
    lowNopoints = 4;
    lowSafetypoint = 5;
    lowFieldgoal = 7; 
}

Any ideas on how to fix this issue? I want to keep track of totalScore per team. Thank you!

  • @SURESH ATTA I have looked over the "Java Static vs Instance" question. It helped me understand that static variable was the wrong type to go for. However, it doesn't help me answer my specific question of fixing this issue. // I'm not sure if I should be working with `totalScore` in the superclass and just modify the setup or if I should be placing it elsewhere? – PametnaGabi Dec 13 '15 at 19:55

1 Answers1

1

If you want to keep track at per-team level, then you should define it as member variable, so that each team will have it own copy of totalScore field.

Having a static filed in the super class means that it will always be a total aggregation of all the action which is happening in the sub-classes. Because only one copy of static fields is maintained per-class. In your case you have defined it in the super-class, which makes it a global field for score aggregation.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • Could you please clarify? I removed the word `static` but then the `totalScore` doesn't add up at all in the program. I understand now that static will keep the `totalScore` variable the same throughout the superclass and all subclasses. However, where should the change be made and how? – PametnaGabi Dec 13 '15 at 19:51
  • @PametnaGabi You need to make it part of your sub class, which represents the team here. – YoungHobbit Dec 14 '15 at 03:38