lundi 21 mars 2016

Can someone explain how this code works when asked to return a number?

i got some exercises from my professor. So the exercise was to make to classes: Question and Test so on the Test class i got a method where i need to represent the points of each question when i ask e.g i call num 3 and each question that is 3 points will appear and if there are no nums that i called it should return null but i can't understand how this part of code works? If someone could explain it to me i would really appreciate it. Thanks :)

Code:

public Question[] returnPoints(double nP){
    if(index == 0)
        return null;

    int count = 0;

    for(int i = 0; i < index; i++){
    //Question[] questions
        if(questions[i].getPoints() == nP)
            count++;
    }

    if(count ==  0)
        return null;

    Question[] temp = new Question[count];
    int idx = 0;

    for(int i = 0; i < index; i++){
        if(questions[i].getPoints() == nP)
            temp[idx++] = questions[i];
    }
    return temp;
}

Aucun commentaire:

Enregistrer un commentaire