Limited access

Upgrade to access all content for this subject

A jewelry store sells a variety of watches. Each watch is made of components that may be manufactured at different times. The store wants to perform routine maintenance on a watch if it is either manufactured before a specified year or any of the watch’s components are manufactured before the specified year.

Given the following classes, which of the choices would be the best implementation of the countWatchesToBeServiced method in the Store class to determine the number of watches that need to be serviced?

public class Store 
{
    private int storeNum;             //Store number
    private ArrayList<Watch> watches; //List of watches for sale

    public int countWatchesToBeServiced(int year)
    {
        // Implementation not shown
    }
}

public class Watch 
{
    private String name; //Name of watch
    private ArrayList<Component> components; //List of watch’s components
    private int yearMade; //Year watch was manufactured

    public Watch(String nam, ArrayList<Component> parts, int yr)
    {
        name = nam;
        components = parts;
        yearMade = yr;
    }

    /* returns the year that the watch was manufactured
     */    
    public int getYearMade()
    {
        return yearMade;
    }

    /* returns true if any of the watch’s components were manufactured
     * before the specified year which is passed as a parameter to the method.
     * The method returns false otherwise.
     */    
    public boolean componentNeedsService(int year)
    {
        // Implementation not shown
    }
}

public class Component 
{
    private String description;  //Description of the component
    private int yearMade;  //Year that the component was manufactured

    public Component(String desc, int yr)
    {
        description = desc;
        yearMade = yr;
    }

    /* returns the year that the component was manufactured
     */    
    public int getYearMade()
    {
        return yearMade;
    }
}
A
public int countWatchesToBeServiced(int year)
{
    int count = 0;
    for (Watch w : watches)
    {
        if (w.getYearMade() < year && w.componentNeedsService(year))
            count++;
    } 
    return count;
}
B
public int countWatchesToBeServiced(int year)
{
    int count = 0;
    for (int i=0; i<watches.size(); i++)
    {
        if (watches.get(i).getYearMade() < year)
            count++;
    } 
    return count;
}
C
public int countWatchesToBeServiced(int year)
{
    for (int i=0; i<watches.size(); i++)
    {
        if (watches.get(i).componentNeedsService(year))
            count++;
    } 
}
D
public int countWatchesToBeServiced(int year)
{
    int count = 0;
    for (int i=0; i<watches.size(); i++)
    {
        if (watches.get(i).getYearMade() < year)
            if (watches.get(i).componentNeedsService(year) < year)
                count++;
    } 
    return count;
}
E
public int countWatchesToBeServiced(int year)
{
    int count = 0;
    for (int i=0; i<watches.size(); i++)
    {
        if (watches.get(i).getYearMade() < year ||
            watches.get(i).componentNeedsService(year))
            count++;
    } 
    return count;
}
Select an assignment template