0

I created a function to identify the maximum value at an array. The function call works only when the array has one item. Can you check the code below and tell me what you think?

Output I receive

The Highest Grade Student is: Grade: 0.0

Output Needed (example)

The Highest Grade Student is: 976 Grade: 90.0

Highest Grade Function:

public static String highestGrade(Student[] d)   
{    // the function checks the highest grade and returns the corresponding id
    // d is an array of Student data type                
    double max = d[0].getScore();  // assigning max value for reference
    int gradeCounter = 1; // counter
    String topID = "";  // emplty string

    // looping through array
    for(  ; gradeCounter< d.length; gradeCounter++ )
    {
       if( d[gradeCounter].getID() != "") 
       // Checking if there is an ID assigned
       {   // comparing the score at index with the max value
           if(d[gradeCounter].getScore() > max)
           {   // if the score is higher than the max value, the max is updated
              max = d[gradeCounter].getScore();
              topID=d[gradeCounter].getID();
            }
        }
     }

   return topID; // returning the id that corresponds to the highest grade
}

Print Report Function

public static void printReport(Student[] c)
{
    System.out.print("\n ***Class Report*** \n\n");
    // Looping through the array and printing both Id and Grade
    for(int idCounter = 0; idCounter<c.length; idCounter++ )
    {
        if( c[idCounter].getID() != "")
        {
            System.out.print("ID: "); 
            System.out.print(c[idCounter].getID());  
            System.out.print(" Grade: ");
            System.out.println(c[idCounter].getGrade());
        }
    }

//*******This is the part that has the issue*************

    // providing the user with the id having the highest grade        
    System.out.print("\nThe Highest Grade Student is: ");  

    // assigning a variable to the function call of highestgrade id    
    String studentHighestGrade=highestGrade(c);

    // printing the variable  to provide the id
    System.out.print(studentHighestGrade);

    // providing the user with  grade that corresponds to the id
    System.out.print(" Grade: ");  

    // declaring and initializing a variable
    double valueOfHighestGrade = 0.0;

    // Looping through the array to get the highest grade that 
   // corresponds to the ID
    for(int idCounter = 0; idCounter<c.length; idCounter++ )
    {
     // if the id at index =idCounter equals the highest id then
     // we get the grade (score) at that index and assign it to 
     // valueOfHighestGrade
      if(c[idCounter].getID().equals(studentHighestGrade))
      {
          valueOfHighestGrade=c[idCounter].getScore();
      }

    }
    // printing the highest grade (score)
    System.out.print(valueOfHighestGrade);

    countGrades( c);

    System.out.print("\n ***End of Report*** \n");
}
1
  • 2
    What is the problem? Please provide a MCVE Commented May 25, 2016 at 5:18

3 Answers 3

2

If you only have one Student in your array, as you are doing int gradeCounter = 1; // counter then you will not get the value of the student id,

so before your loop in highestGrade do

topID = d[0].getId(); 

Not sure why you are doing if (c[idCounter].getID() != "") though

Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly. I have another function calculating averages, and I wanted to calculate averages for items that have ids only. So i skipped items with id="" as they would have scores of =0.0 which would impact the average. I guess i just followed that pattern in the highestGrade function :)
0

Advice to use equals method to compare String instance.

Comments

0
  • If the highest score is in first entry u will not get any output bcz u have set “max” as 1st student’s score but not “topID”
  • If the max score will be acquired by multiple students then it will return only the student whose entry is in first .Return type of “highestGrade” is String so you can get only get 1 student’s marks in o/p even if multiple students get highest mark

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.