java - Objects' variables changed by method in another class -


this question has answer here:

i created simple method in cardgames class replicates card game play around conditional statements. call method separate player class because player earns/loses points based on card. want method able change player objects points variable.

what want have happen when playsimplecardgame gets called player object, method changes player object's points.

but when run points not change. i've tried extending/implementing both classes (i.e. shooting in dark). created instance variable points in cardgames class player object not have points variable. missing?

public class player  {     private int points;      public static void main(string[] args)     {         cardgames steve = new cardgames();         system.out.println(steve.playsimplecardgame("red"));         system.out.println(steve.playsimplecardgame("red"));         system.out.println(steve.playsimplecardgame("black"));         system.out.println(steve.playsimplecardgame("black"));         system.out.println(steve.points);     }  }  public class cardgames {      /*      * rules of game:       * if draw red card, point.        * if draw black card, lose 2 points.      */     public int playsimplecardgame(string color)     {         if (color.equalsignorecase("red"))             return points = points + 1;         else              return points = points - 2;     } } 

public class player {     private int points;      public player(){         points=0;     }      public static void main(string[] args)     {         cardgames game = new cardgames();         player steve = new player();         system.out.println(game.playsimplecardgame("red", steve));         system.out.println(game.playsimplecardgame("red", steve));         system.out.println(game.playsimplecardgame("black", steve));         system.out.println(game.playsimplecardgame("black", steve));         system.out.println(steve.points);     }      public int getpoints() {         return points;     }      public void addpoints(int p) {         this.points = points + p;     }  }  public class cardgames {     /*      * rules of game:       * if draw red card, point.        * if draw black card, lose 2 points.      */     public int playsimplecardgame(string color, player player)     {         if (color.equalsignorecase("red"))         {             player.addpoints(1);             return player.getpoints();         }         else         {             player.addpoints(-2);             return player.getpoints();         }     } } 

firstly, there no need extend cardgames class player class. secondly, if wish it, bad design. won't go design part. following code should answer problem :

public class player {     public integer points;      public player(){         points=0;     }      public static void main(string[] args)     {         cardgames game = new cardgames();         player steve = new player();         system.out.println(game.playsimplecardgame("red", steve));         system.out.println(game.playsimplecardgame("red", steve));         system.out.println(game.playsimplecardgame("black", steve));         system.out.println(game.playsimplecardgame("black", steve));         system.out.println(steve.points);     } }  public class cardgames {     /*      * rules of game:       * if draw red card, point.        * if draw black card, lose 2 points.      */     public int playsimplecardgame(string color, player player)     {         if (color.equalsignorecase("red"))             return player.points = player.points + 1;         else              return player.points = player.points - 2;     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -