?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Searching for a Movie Kiosk

APCSA-REP6BK

Given the following classes:

public class MovieKiosk 
{
    int kioskNum;
    ArrayList<Movie> movies = new ArrayList<Movie> ();

    public MovieKiosk(int kiosk, ArrayList<Movie> movieList)
    {
        kioskNum = kiosk;
        movies = movieList;
    }

    public int getKioskNum()
    {
        return kioskNum;
    }

    public ArrayList<Movie> getMovies()
    {
        return movies;
    }

    // other data and methods not shown    
}

public class Movie
{
    public String getName()
    {
        // returns the title of the movie
    }

    // other data, constructors and methods not shown    
}

A company keeps track of all their movie kiosks in an array list. Each movie kiosk contains an arrayList of movies that are in the kiosk machine.

Fill in the code in the following method so that it will search for a movie title and return an array list of the kiosk numbers of the kiosks that have the movie?

public static ArrayList<Integer> findKiosks(ArrayList<MovieKiosk> kiosks, String title)
{
    // Insert your code here
}
A
for (int i=0; i<kiosks.size(); i++)
{
    for (Movie m : kiosks)
    {
        if (m.getName().equals(title))
            return kiosks.get(i).getKioskNum();
    }
}
return null;
B
ArrayList<Integer> kioskNums = new ArrayList<Integer>();
for (int i=0; i<kiosks.size(); i++)
{
    for (Movie m : kiosks)
    {
        if (m.getName().equals(title))
            kioskNums.add(kiosks.get(i).getKioskNum());
    }
}
return kioskNums;
C
ArrayList<Integer> kioskNums = new ArrayList<Integer>();
for (int i=0; i<kiosks.size(); i++)
{
    for (int j=0; j<kiosks.get(j).getMovies().size(); j++)
    {
        if (kiosks.get(j).getName().equals(title))
             kioskNums.add(kiosks.get(j).getKioskNum());
    }
}
return kioskNums;
D
ArrayList<Integer> kioskNums = new ArrayList<Integer>();
for (Movie m : kiosks.getMovies())
{
    if (m.getName().equals(title))
        kioskNums.add(kiosks.getKioskNum());
}
return kioskNums;
E
ArrayList<Integer> kioskNums = new ArrayList<Integer>();
for (int i=0; i<kiosks.size(); i++)
{
    for (Movie m : kiosks.get(i).getMovies())
    {
        if (m.getName().equals(title))
            kioskNums.add(kiosks.get(i).getKioskNum());
    }
}
return kioskNums;