-4

So, I am trying to create one setter method for multiple instance variables of one constructor. I have already made a getter that works this way:

public int getQuiz(int num) {

    int quiz = -1;
    int[] tempArray = {this.qz1, this.qz2, this.qz3, this.qz4, this.qz5};

    if(num != 0) {
        quiz = tempArray[num - 1];
    }
    else quiz = tempArray[num];

    return quiz;
}

Here the method has in its arguments the number of the quiz (qz) variable whose value it is supposed to return (the setter would, of course, have two args: int num and int score). The method is meant to work from 1, but if I forget that, I do not want an error if I ask for quiz 0, so that is the purpose of the if else.

This approach, however, will not work for a setter, as the array only contains the values of the instance variables, so changes to the array would not project into the instance variables. I am aware that this could be done with several if statements, I am just looking for a more elegant solution if there is one.

This is the constructor and the instance variables:

private String Name;
private int qz1, qz2, qz3, qz4, qz5;

Student(String Name, int qz1, int qz2, int qz3, int qz4, int qz5) {
    this.Name = Name;
    this.qz1 = qz1;
    this.qz2 = qz2;
    this.qz3 = qz3;
    this.qz4 = qz4;
    this.qz5 = qz5;
}

If you think that anything could be done better in the getter method please let me know as well.

Thank you for your answers!

simon.b
  • 1
  • 1
  • 5
  • If you downvote, please tell me what is wrong. – simon.b Apr 12 '17 at 08:30
  • Why the `tempArray[num - 1]`? This will give the same result for `num` being 1 or 0. Also, what if `num` is negative or larger than 5? – tobias_k Apr 12 '17 at 08:31
  • I explained the first part in the question, as for the second part - I will get an error, as I should if I ask for the minus second instance variable or the sixth quiz and I will have to change that call. Thanks. – simon.b Apr 12 '17 at 08:34

3 Answers3

0

I would suggest to make an int array to store your variables, so instead of: private int qz1, qz2,.... do

private int [] quizValues;

You can then get a value for a quiz:

public int getQuizValue(int storePositionOfQuiz) {
  // check for outOfBounds!
  return this.quizValues[storePositionOfQuiz];
}

If you want you can then initialize the quiz values with using a int-array as parameter

public void setQuizValues(int [] newValues) {
  this.quizValues = newValues;
}
M. Haverbier
  • 383
  • 2
  • 13
0

If you have variables like int qz1, qz2, qz3, qz4, qz5; it is usually a good idea to make those a list or an array, as you already do in your getter. As you said, the problem is that the values set in the array do not transfer to the original integer variables. But why not just use the array in the class itself, instead of those different integer variables? That lets you use the array in both the getter and the setter (and the constructor and probably a few other places, like for hashCode, toString and equals).

private int[] quizArray = new int[5]; // set values in constructor

public int getQuiz(int num) {
    try {
        return quizArray[num];
    } catch (ArrayIndexOutOfBoundsException e) {
        return -1; // or raise exception
    }
}

public boolean getQuiz(int num, int value) {
    try {
        quizArray[num] = value;
        return true;
    } catch (ArrayIndexOutOfBoundsException e) {
        return false; // or raise exception
    }
}
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

A few things, I would like to point out:

  1. getQuiz() method you've described is not exactly a getter. And it is fine for it to exist in such way. Just don't call it a getter.

  2. Instead of using tempArray in getQuiz() method, why don't you use it as class variable (by renaming it to something like quizzes? This will reduce the work of declaring so many variables like qz1, az2, ...

  3. And, now, you can also add a setQuiz(int index, int value) method which will serve the purpose of setter.

santosh-patil
  • 1,540
  • 1
  • 15
  • 29