Josh
Josh

Reputation: 163

I have one error with java arrays(store the letter grade in an array)

public char calculateGrade(int [] scores, char [] grades){
    for (int r = 0; r < scores.length; r++){
        //System.out.println(scores[r] + " ");
        if (scores[r] > 90)
            grades = 'A';
        else if (scores[r] > 80)
            grades = 'B';
        else if (scores[r] > 70)
            grades = 'C';
        else if (scores[r] > 60)
            grades = 'D';
        else
            grades = 'F';

        return grades;
    }
}

Above is one of my methods. It reads a part of a file (scores[]) and determines what letter grade they are. What I need to know how to do is store the letter grade in an array I have already crated called grades[].

Upvotes: 0

Views: 1954

Answers (7)

Dwight
Dwight

Reputation: 360

All of what I'm seeing doesn't consider the fact that it would seem as though you are attempting to pass by reference, which cannot be done in java. You either need to change the return type of your method (currently it is expecting a single character), or have the method just access a global variable, rather than passing it a variable. It does look like homework so I'm not going to go any further.

Upvotes: 0

user166390
user166390

Reputation:

Avoid side-effects in functions, where possible, consider this as an alternative approach:

// Given a score, return the appropriate Grade for it.
public char calculateGrade(int score){
   // See function contract.
}

// Later...
int scores[] = {10,70,50,90};
int grades[] = new char[scores.length]; // but really, use an ArrayList or simialr...
for (var i = 0; i < scores.length; i++) {
   // We used this form of "for" to keep an index
   grades[i] = calculateGrade(scores[i]);
}

// And perhaps there might be a function like this as well:

// Given a set of scores, get the associated grades.
public char[] calculateGrades(int[] scores) {
   // See above.
   // Note how this avoids mutating an input parameter.
   // Keeping functions "more refined" also generally helps.
}

Happy coding.

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117617

public char calculateGrade(int[] scores)
{
    char grade = 'F';
    for(int score : scores)
    {
        if(score >= 90) grade = 'A';
        else if(score >= 80) grade = 'B';
        else if(score >= 70) grade = 'C';
        else if(score >= 60) grade = 'D';
        else grade = 'F';
    }
    return grade;
}

Upvotes: 0

Ross Larson
Ross Larson

Reputation: 2437

grades[r] = 'A'; (and similar for others)

This assumes grades is of same length as scores

Upvotes: 2

Mahesh
Mahesh

Reputation: 34625

grades = 'A';

A is of type char while the identifier grades is of type char[]. What you have to do is to accumulate the grades in the array using [] operator. Also you should return grades once you are done with the for loop and not at the first iteration. Make sure that the length of array grades is equivalent to scores.

Upvotes: 0

Tam&#225;s
Tam&#225;s

Reputation: 398

Try this:

public void calculateGrade(int [] scores, char [] grades){
    for (int r = 0; r < scores.length; r++){
        if (scores[r] > 90)
            grades[r] = 'A';
        else if (scores[r] > 80)
            grades[r] = 'B';
        else if (scores[r] > 70)
            grades[r] = 'C';
        else if (scores[r] > 60)
            grades[r] = 'D';
        else
            grades[r] = 'F';
    }
}

Upvotes: 0

Jason Day
Jason Day

Reputation: 8839

This looks suspiciously like a homework assignment, so look at how you access the scores array, and how you access the grades array. Do you see the difference?

Upvotes: 0

Related Questions