0

The goal of this code is to compare two different string[] arrays, and check to see how many of the elements match. This will allow the methods to then see whether a student has passed or failed a test, along with determining their letter grade. However, every test done results in true being returned for them passing and an "A" being returned for their letter grade. Now that the elements are being compared correctly, I still have the issue of every element passing the .equals(). i.e. answerKey[1] = "B"; studentAnswers[1] = "C"; if these two elements were to be compared, it would pass the .equals().

public class ListOfAnswers
{
    static final String[] answerKey = {"A", "B", "B", "C", "D", "B", "C", "C", "D", "E",
                                       "C", "D", "D", "E", "A", "A", "A", "D", "D", "E"};
    String[] studentAnswers;

    public ListOfAnswers(String[] ans)
    {
        studentAnswers = ans;
    }

    public boolean checkPassOrFail()
    {
        int answersRight = 0;
        for (int cnt = 0; cnt < studentAnswers.length; cnt++)
        {
            if(studentAnswers[cnt].equals(answerKey[cnt]))
            {
                answersRight++;
            }
        }
        if (answersRight >= 14)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public String computeGrade()
    {
        int score = 0;
        String grade = "";
        for (int cnt = 0; cnt < studentAnswers.length; cnt++)
        {
            if (studentAnswers[cnt].(answerKey[cnt]))
            {
                score++;
            }
        }

        if (score < 10)
        {
            grade = "E";
        }
        else if (score == 10)
        {
            grade = "D";
        }
        else if ((score==11)||(score==12))
        {
            grade = "C-";
        }
        else if (score==13)
        {
            grade = "C";
        }
        else if (score == 14)
        {
            grade = "C+";
        }
        else if (score == 15)
        {
            grade = "B-";
        }
        else if (score == 16)
        {
            grade = "B";
        }
        else if (score == 17)
        {
            grade = "B+";
        }
        else if (score == 18)
        {
            grade = "A-";
        }
        else if ((score == 19)||(score==20))
        {
            grade = "A";
        }
        return grade;
    }
} 

The test data looks as follows

public class ListOfAnswersTester
{
    public static void main(String[] args)
    {
        String[] danAnswers = {"A", "B", "B", "D", "D", "B", "C", "C", "D", "E", "C",
                               "D", "D", "E", "A", "A", "A", "D", "D", "E"};
        ListOfAnswers danAnswerList = new ListOfAnswers(danAnswers);

        String[] bobAnswers = {"A", "C", "B", "C", "D", "B", "C", "C", "D", "E", "C",
                               "D", "A", "E", "A", "A", "A", "D", "E", "E"};
        ListOfAnswers bobAnswerList = new ListOfAnswers(danAnswers);

        System.out.println("Student Dan's Pass/Fail: " + danAnswerList.checkPassOrFail());
        System.out.println("Student Dan's Grade: " + danAnswerList.computeGrade());

        System.out.println("Student Bob's Pass/Fail: " + bobAnswerList.checkPassOrFail());
        System.out.println("Student Bob's Grade: " + bobAnswerList.computeGrade());
     }
}
nook
  • 2,378
  • 5
  • 34
  • 54
Ryan Sisson
  • 139
  • 2
  • 4
  • 12

3 Answers3

2

The problem is here:

if(studentAnswers[cnt] == answerKey[cnt])

To compare Strings and other Object instances, you must use equals method:

if(studentAnswers[cnt].equals(answerKey[cnt]))

This must be fixed in both methods ListOfAnswers#checkPassOrFail and ListOfAnswers#computeGrade.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Damn, you beat me by a second. :) – Devolus May 08 '13 at 15:28
  • the == might still work, as single character string constants might be "internalized" ... but it is more by accident that they do. However I with that fix I would expect everyone to get a count of zero with the original code ... – Clemens Klein-Robbenhaar May 08 '13 at 15:32
  • @ClemensKlein-Robbenhaar maybe it's an exercise to show the difference between using `==` and `equals` (since this code looks like homework). Anyway, if you change the test to read the data from an outside source like a file, then the `==` won't work (in this case `String` pooling won't work). – Luiggi Mendoza May 08 '13 at 15:38
  • Thank you for the .equals() fix, but there was also a typo as pointed out. – Ryan Sisson May 08 '13 at 15:47
  • Just in case anyone doubts it: I am *not* advocating using '==' with strings - always use equals there unless you really know what you are doing. I just wanted to point out that the problem here might be something else. – Clemens Klein-Robbenhaar May 08 '13 at 15:47
  • @ClemensKlein-Robbenhaar last comment must be part of your answer as well. – Luiggi Mendoza May 08 '13 at 15:48
1

In Java when you want to compare objects you must use equal();

studentAnswers[cnt] == answerKey[cnt]

studentAnswers[cnt].equals(answerKey[cnt])
Devolus
  • 21,661
  • 13
  • 66
  • 113
0

I see a typo here:

ListOfAnswers bobAnswerList = new ListOfAnswers(danAnswers);

should be:

ListOfAnswers bobAnswerList = new ListOfAnswers(bobAnswers);

or you give the good answers of Dan to Bob ;)

Oh, and as others said, always use equals when comparing (non-null) strings unless you really want to check for identity (I see question is already updated to do that).

Clemens Klein-Robbenhaar
  • 3,457
  • 1
  • 18
  • 27