Limited access

Upgrade to access all content for this subject

Consider the following class.

    public class Athlete 
    {
        private String name;
        private String sport;
        private int ranking;

        public Athlete(String name, String sport, int ranking)
        {
            this.name = name;
            this.sport = sport;
            this.ranking = ranking;
        }

        public String getName()
        {
            return name;
        }

        public String getSport()
        {
            return sport;
        }

        public int getRank()
        {
            return ranking;
        }
    }

Consider the following code.

    Athlete[] athletes = new Athlete[10];
    athletes[0] = new Athlete("Steve", "football", 12);
    athletes[1] = new Athlete("Karen", "softball", 40);
    athletes[2] = new Athlete("Greg", "football", 55);
    athletes[3] = new Athlete("Ralph", "tennis", 5);
    athletes[4] = new Athlete("Sally", "golf", 5);
    printFootballInfo(athletes);

Consider the following methods.
I.

public static void printFootballInfo (Athlete[] athletes)
{
    for (int i=0; i<athletes.length; i++)
    {
        if (athletes[i].getSport().equals("football"))
            System.out.println(athletes[i].getName() +  " " + athletes[i].getRank());
    }
}

II.

public static void printFootballInfo (Athlete[] athletes)
{
    for (Athlete a : athletes)
    {
        if (a.getSport().equals("football") && a != null)
            System.out.println(a.getName() + " " + a.getRank());
    }
}

III.

public static void printFootballInfo (Athlete[] athletes)
{
    for (int i=0; i<athletes.length; i++)
    {
        if (athletes[i] != null && athletes[i].getSport().equals("football"))
            System.out.println(athletes[i].getName() + " " + athletes[i].getRank());
    }
}

Which of these methods would print football player information without throwing an error if this code were executed from the main method?

A

I only.

B

III only.

C

I and II only.

D

II and III only.

E

I, II, and III

Select an assignment template