?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Optimize Method With If/Else Statement Processing Array

APCSA-QXRET4

Consider the following method.

// Pre-condition: purchases is full of purchase amounts
public static double[] computeSalesTax(double[] purchases)
{
    double[] taxes = new double[purchases.length];
    for (int i=0; i<purchases.length; i++)
    {
        if (purchase[i] <10)
        {
            double percent = .075;
            taxes[i]=purchases[i]*percent;
            purchases[i]=0;
        }
        else
        {
            double percent = .075;
            double temp = purchases[i]*percent;
            taxes[i]=temp;
        }
    }
    return taxes;
}

How could the following method be optimized, either to make it faster or to make the code more readable (less lines of code)?

A
public static double[] computeSalesTax(double[] purchases)
{
    double[] taxes = new double[purchases.length];
    for (int i=0; i<purchases.length; i++)
    {    
        if (purchases[i] < 10)
            purchases[i]=0;
        double percent = .075; 
        taxes[i]=purchases[i]*percent;
    }
    return taxes;
}
B
public static double[] computeSalesTax(double[] purchases)
{
    double[] taxes = new double[purchases.length];
    for (int i=0; i<purchases.length; i++)
    {    
        double percent = .075; 
        taxes[i]=purchases[i]*percent;
        purchases[i]=0;
    }
    return taxes;
}
C
public static double[] computeSalesTax(double[] purchases)
{
    double percent = .075; 
    double[] taxes = new double[purchases.length];
    for (int i=0; i<purchases.length; i++)
    {    
        taxes[i]=purchases[i]*percent;
        purchases[i]=0;
    }
    return taxes;
}
D
public static double[] computeSalesTax(double[] purchases)
{
    double percent = .075; 
    double[] taxes = new double[purchases.length];
    for (int i=0; i<purchases.length; i++)
    {    
        if (purchases[i] < 10)
        {    
            taxes[i]=purchases[i]*percent;
            purchases[i]=0;
        }
        else
            taxes[i]=purchases[i]*percent;
    }
    return taxes;
}
E
public static double[] computeSalesTax(double[] purchases)
{
    double percent = .075; 
    double[] taxes = new double[purchases.length];
    for (int i=0; i<purchases.length; i++)
    {    
        taxes[i]=purchases[i]*percent;
        if (purchases[i] < 10)
            purchases[i]=0;
    }
    return taxes;
}