Limited access

Upgrade to access all content for this subject

Consider the following class.

public class Customer 
{
    private int custNum; 
    private String firstName;
    private String lastName;
    private String homePhone = "";
    private double totPurchaseAmt = 0;;
    private int[] orders;    // list of customer order numbers

    public Customer(int cNum, String fName, String lName, 
                    String hPhone, double totPurch, int[] ords)
    {
        custNum = cNum;
        firstName = fName;
        lastName = lName;
        homePhone = hPhone;
        totPurchaseAmt = totPurch;
        orders = ords;
    }

    public int[] getOrders()
    {
        // returns array of customer order numbers
    }

    // other methods not shown
}

The orders array is an array of customer order numbers, but in this case let’s assume that it might not be full of order numbers. Also, all the valid order numbers (valid means that the order number is greater than zero) may not be in consecutive order. Consider the following segment of code from a main() method in another class.

int[] orders = {1002, 0, 1925, 3245, 0, 0, 0};
Customer pat = new Customer(56, "Pat", "Patterson", "846-288-9655", 
                            300.00, orders);
System.out.println(pat.getOrderCount());

Which of the following methods from Customer would print the total number of valid order numbers for this customer?

A
public int getOrderCount()
{
    int count = 0;
    for (int i=0; i<getOrders().length; i++)
    {
        count++;
    }
    return count;
}
B
public int getOrderCount()
{
    int count = 0;
    for (int i=0; i<getOrders().length; i++)
    {
        if (getOrders()[i]>0)
            count++;
    }
    return count;
}
C
public int getOrderCount()
{
    int count = 0;
    for (int i=0; i<getOrders().length; i++)
    {
        if (getOrders()[i]>0)
            count++;
        else
            return count;
    }
}
D
public int getOrderCount()
{
    return orders.length;
}
E
public int getOrderCount()
{
    int count = 0;
    for (int i=0; i<getOrders().length; i++)
    {
        if (getOrders()[i]==0)
            count++;
    }
    return count - orders.length;
}
Select an assignment template