viewing paste Unknown #27583 | Text

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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;
   }
 
Viewed 814 times, submitted by Guest.