?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Printing a Customer Object

APCSA-AW1VGK

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

    // other data and methods not shown
}

And also the following segment of code to create and print a customer:

int[] orders = {1002, 1925, 3245};
Customer kyle = new Customer(56, "Kyle", "Groce", "346-288-9655", 200.00, orders);
System.out.println(kyle);

If the output of this print statement is:

Customer: 56 Groce, Kyle has orders of 1002, 1925, 3245.

which of the following methods in the Customer class facilitates this output?

A
public void printString()
{
    System.out.print( "Customer: " + custNum + " " + lastName + ", " + firstName + " has orders of ");
    for (int i=0; i<orders.length-1; i++)
    {
        System.out.print(orders[i] + ", ");
    }
    System.out.print(orders[orders.length-1] + ".");
}
B
public void printString()
{
    System.out.print( "Customer: " + custNum + " " + lastName + ", " + firstName + " has orders of ");
    for (int i=0; i<orders.length; i++)
    {
           System.out.print(orders[i] + ", ");
    }
}
C
public String toString()
{
    String str = "Customer: " + custNum + " " + lastName + ", " + firstName + " has orders of ";
    for (int i=0; i<orders.length-1; i++)
    {
         str += orders[i] + ", ";
    }
    return str + orders[i] + ".";;
}
D
public String toString()
{
    String str = "Customer: " + custNum + " " + lastName + ", " + firstName + " has orders of ";
    for (int i=0; i<orders.length; i++)
    {
        str += orders[i] + ", ";
    }
    return str;
    System.out.print(str);
}
E
public String toString()
{
    String str = "Customer: " + custNum + " " + lastName + ", " + firstName + " has orders of ";
    for (int i=0; i<orders.length-1; i++)
    {
        str += orders[i] + ", ";
    }
    str += orders[orders.length-1] + ".";
    return str;
}