-1

I have a program which is meant to be a client/server game question game. I've made it as far as accounting various cases of the client/server sending a termination command for the end of the game.

Now, my issue is that I have a set of primitive int points, attempts, correct which are read by the client from the server in a String as below:

N.B. I do know that Java functions pass parameters by value, not reference, and that assigning the value inside of the function will not change the value of the original.

int points = accepted = correct = 0;
String inbound = check (inbound, points, accepted, correct);
System.out.println(points); // Displays value of 0, when expecting > 0

private static String check (String str, int points, int attempts, int correct) {

    // Expect Q QuestionString
    if (str.substring(0,1).equals("Q")) {
        //System.out.println("This is the question.");
        return str.substring(2, str.length());
    }

    String[] input = str.split(" ");

    // Expect EX # # #
    if (input[0].contains("EX")) {
        points = Integer.parseInt(input[1]);
        attempts = Integer.parseInt(input[2]);
        correct = Integer.parseInt(input[3]);
        return "EX";
    }

    // Expected strings: Correct..., Incorrect.
    return str;
}

I am unsure how to workaround this issue without jeopardizing encapsulation or hindering other concepts.

  • I guess by "jeopardizing encapsulation or hindering other concepts" you mean creating a wrapper class? If that's case, please _do_ "jeopardizing encapsulation or hindering other concepts" since IMO that is the best choice in this case. Other choices include making `points`, `correct` and `attempt` class level variables and assigning values to them. – Sweeper Oct 28 '17 at 14:36

1 Answers1

0

Create a wrapper class to contain those three integer parameters then simply pass an instance of that wrapper to the check method and then modify its contents within the method.

example:

public class Wrapper
{
   private int points;
   private int attempts;
   private int correct;

   public int getPoints() {
       return points;
   }

   public void setPoints(int points) {
      this.points = points;
   }

   public int getAttempts() {
      return attempts;
   }

   public void setAttempts(int attempts) {
     this.attempts = attempts;
   }

   public int getCorrect() {
      return correct;
   }

   public void setCorrect(int correct) {
      this.correct = correct;
   }
}

thus the first part of your code will become:

Wrapper wrapper = new Wrapper();
String inbound = check (inbound, wrapper);
System.out.println(wrapper.getPoints());

and your check method becomes:

private static String check (String str, Wrapper wrapper) { 
        ...
        ...
        if (input[0].contains("EX")) {
           wrapper.setPoints(Integer.parseInt(input[1]));
           wrapper.setAttempts(Integer.parseInt(input[2]));
           wrapper.setCorrect(Integer.parseInt(input[3]));
           return "EX";
        }
        ...
        ...
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126