Consider the class as partially defined here:
//class PizzaOrder ------------------------------------
class PizzaOrder
{
// static public members
public static final int MAX_TOPPINGS = 20;
// instance members
private String toppings[];
private int numToppings;
// constructor
public PizzaOrder()
{
numToppings = 0;
toppings = new String[MAX_TOPPINGS];
}
// accessor tells # toppings on pizza, i.e., #toppings in array
public int getNumToppings() { return numToppings; }
// etc.
}
We want to write an instance method, addTopping() that will take a String parameter, topping, and add it to the toppings[] array at the next available location as determined by the int member numToppings. Assume any String passed in is valid and no other method modifies the toppings[] array or numToppings.
The client would call it like so:
somePizzaOrder.addTopping( "onions" );
Which is a correct definition for addTopping() based on what you see, above?
A.
public void addTopping( String topping )
{
toppings[numToppings++] = topping;
}
B.
public boolean addTopping( String topping )
{
if ( numToppings > MAX_TOPPINGS )
return false;
toppings[numToppings++] = topping;
return true;
}
C.
public void addTopping( String topping )
{
toppings[numToppings] = topping;
}
D.
public boolean addTopping( String topping )
{
if ( numToppings >= MAX_TOPPINGS )
return false;
toppings[numToppings++] = topping;
return true;
}
E.
public boolean addTopping( String topping )
{
if ( numToppings >= MAX_TOPPINGS || numToppings < 0 )
return false;
toppings[numToppings++] = topping;
return true;
}
F.
public boolean addTopping( String topping )
{
numTopping++;
if ( numToppings >= MAX_TOPPINGS )
return false;
toppings[numToppings] = topping;
return true;
}