?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Car Repairs Using Arrays of Car and Repair Objects

APCSA-TVWTF1

Given the following Car and Repair classes:

public class Car
{
    /*  array of up to 20 repairs made to the car  */    
    private Repair[] repairs = new Repair[20];

    /** Constructor method to create a Car 
     * 
     * @param mk = make of the car
     * @param md = model of the car
     * @param serial = unique serial number of the car
     */
    public Car(String mk, String md, String serial)
    {  /*  Implementation not shown  */   }

    /**
     * @return array of repairs made to the car
     */
    public Repair[] getRepairs()
    {  /*  Implementation not shown  */  }

    /**
     * @return unique serial number of car
     */
    public String getSerialNum()
    {  /*  Implementation not shown  */  }

    /*  Other variables and methods not shown  */
}

public class Repair 
{
    /** Constructor method for creating a car repair
     * 
     * @param serial = unique serial number of car
     * @param type = type of repair
     * @param cost = cost of the repair
     * @param dt = date of repair
     */
    public Repair(String serial, String type, double cost, String dt)
    {  /*  Implementation not shown  */  }

    /**
     * @return type of repair
     */
    public String getType()
    {  /*  Implementation not shown  */  }

    /**
     * @return cost of repair
     */
    public double getCost()
    {  /*  Implementation not shown  */  }

    /**
     * @return date of repair
     */
    public String getDate()
    {  /*  Implementation not shown  */  }

    /**
     * @return unique serial number of car being repaired
     */
    public String getSerialNum()
    {  /*  Implementation not shown  */  }

    /*  Other variables and methods not shown  */
}

Given an array of all repairs made to cars at a local repair shop and another array of cars, which of the following segments of code would copy each of a car’s repairs from the allRepairs array to the correct car in the cars array? Note that a unique car is identified by its serial number.

Repair[] allRepairs = /* assume an array of repairs to all cars at a local car repair shop  */
Car[] cars = { new Car("Volkswagen", "Jetta", "VWJT34256"),
               new Car("Ford", "Mustang",  "FDMS62900"),
               new Car("Toyota", "Camry", "TYCA99234")
             };
A
int r=0;
for (int c=0; c < cars.length; c++)
{
    for (int a=0; a<allRepairs.length; a++)
    {
        if (cars[c].getSerialNum().equals(allRepairs[a].getSerialNum()) && 
               r<cars[c].getRepairs().length)
        {
            cars[c].getRepairs()[r]=allRepairs[a];
            r++;
        }
    }
}
B
for (int c=0; c < cars.length; c++)
{
    for (int r=0; r<cars[c].getRepairs().length; r++)
    {
        for (int a=0; a<allRepairs.length; a++)
        {
            if (cars[c].getRepairs()[r].getSerialNum() 
                .equals(allRepairs[a].getSerialNum()))
            {
                cars[c].getRepairs()[r]=allRepairs[a];
            }
        }
    }    
}
C
int r=0;
for (int c=0; c < cars.length; c++)
{
    for (int a=0; a<allRepairs.length; a++)
    {
        if (cars[c].getSerialNum()
                .equals(allRepairs[a].getSerialNum()))
        {
            cars[c].repairs[r]=allRepairs[a];
            r++;
        }
    }
}
D
int r=0;
for (int c=0; c < cars.length; c++)
{
    for (int a=0; a<allRepairs.length; a++)
    {
        if (r<cars[c].getRepairs().length && 
            cars[c].getSerialNum()
                .equals(allRepairs[a].getSerialNum()))
        {
            cars[c].repairs[r]=allRepairs[a];
            r++;
        }
    }
}
E
int r=0;
for (int a=0; a<allRepairs.length; a++)
{
    for (int c=0; c < cars.length; c++)
    {
        if (cars[a].getSerialNum()
                .equals(allRepairs[c].getSerialNum()) && 
             r<cars[a].getRepairs().length)
        {
            cars[a].getRepairs()[r]=allRepairs[c];
            r++;
        }
    }
}