?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Array of Athlete Objects: Determining Best Rank

APCSA-IKGYTP

The Athlete class is used to keep track of an athlete’s name, sport, ranking, and age. Assume that there are accessor methods named getName(), getSport(), getRank(), and getAge() in the Athlete class to retrieve this information from an Athlete object.

Which segment of code will print the name, age, ranking, and sport of the single athlete with the best ranking in the athletes array?

Note the best ranking is defined as the lowest ranking number (i.e. a ranking of 5 is better than a ranking of 10).

Athlete[] athletes = // Assume that his array is full of Athlete objects
A
int min=Integer.MIN_VALUE, minI=Integer.MIN_VALUE;
for (int i=0; i<athletes.length; i++)
{
    if(athletes[i].getRank() < min)
    {
        min = athletes[i].getRank();
        minI = i;
    }    
}
System.out.println(athletes[minI].getName() + " is " + 
    athletes[minI].getAge() + 
    " years old and has a ranking of " + 
    athletes[minI].getRank() + " in " + 
    athletes[minI].getSport());
B
int min=9999, minI=9999;
for (int i=0; i<athletes.length; i++)
{
    if(athletes[i].getRank() < min)
    {
         min = athletes[i].getRank();
    }    
}
System.out.println(athletes[min].getName() + " is " + 
    athletes[min].getAge() + 
    " years old and has a ranking of " + 
    athletes[min].getRank() + " in " + 
    athletes[min].getSport());
C
int min=9999, minI=9999;
for (int i=0; i<athletes.length; i++)
{
    if(athletes[i].getRank() < min)
    {
        min = athletes[i].getRank();
        System.out.println(athletes[i].getName() + " is " + 
             athletes[i].getAge() + 
            " years old and has a ranking of " + 
            athletes[i].getRank() + 
            " in " + athletes[i].getSport());
    }    
}
D
int min=Integer.MAX_VALUE, minI=Integer.MAX_VALUE;
for (int i=0; i<athletes.length; i++)
{
    if(athletes[i].getRank() < min)
    {
        min = athletes[i].getRank();
        minI = i;
    }    
}
System.out.println(athletes[minI].getName() + " is " + 
    athletes[minI].getAge() + 
    " years old and has a ranking of " + 
    athletes[minI].getRank() + " in " + 
    athletes[minI].getSport());
E
int min=-1, minI=-1;
for (int i=0; i<athletes.length; i++)
{
    if(athletes[i].getRank() < min)
    {
        min = athletes[i].getRank();
        minI = i;
    }    
}
System.out.println(athletes[minI].getName() + " is " + 
    athletes[minI].getAge() + 
    " years old and has a ranking of " + 
    athletes[minI].getRank() + " in " + 
    athletes[minI].getSport());