I made two classes. The first one is the Player class and the second the Bag. What I want is to print the score of the player in main(). In Bag, I count the score and I want to set it in Player. This is a part of the Bag class
public void count( int number)
{ .............
scr=k*2;//gives an int
setScore(scr);//set the score in Player class
}
Player:
public class Player {
public Player() {
}
private String name;
private int score;
public Player(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
In main() I try to print the score with this code but I can’t:
Player p=new Player();
System.out.println( p.getScore());
It returns me score=0 (which I set it at the begging of main)
I hope that you understand what I want to say.
For this to work, the Bag class should have a reference to the Player class. So essentially you should change your code to follow the below pattern
And your main method