CS 302 sect. 310
Quiz 4 (3% - 30 points)

Instructor: Dan Shiovitz

Name: _____________________________________________________________

Mean around 23, which is good, since we've seen at least half this before.

  1. Terms and Concepts (6 points)

    [6 points total - 1 each] Is the statement below true or false? Circle one:

    1. T F Arrays are instantiated using new.
      True. Normal syntax. Unless you're using an initializer list.
    2. T F When an array of objects is instantiated, each object in the array is instantiated automatically.
      False. Every slot is filled in with null. If you want the objects, you'll have to new them yourself.
    3. T F Arrays expand as necessary to accomodate new elements.
      False. You'll need to make a new array if you need a bigger block.
    4. T F box[6] refers to the sixth element of box (assume box is an array that contains ten elements)
      False. Arrays are indexed 0 to N-1. This is a common thing to mess up, so be careful.
    5. T F The type of an array of ints is int.
      False. Arrays are of type "array-of-whatever", not of type "whatever".
    6. T F The java index operator (ie, []) performs automatic bounds-checking.
      True. An error gets thrown if you try array[-1] or array[something too big].

  2. Writing Arrays (9 points)

    1. [3 points] Write a code fragment to declare an array of integers that is initialized to have the values 10 through 15. Your solution should make use of an initializer list.

      Pretty straightforward if you know the syntax:

           int[] intArray = { 10, 11, 12, 13, 14, 15 };
           
      (Notice this way does not use new.)

    2. [6 points] Assume an array Letters of unknown size has been instantiated and had values filled in. Write a code fragment to print out every other elements of Letters (i.e., print out the first, third, fifth, and so on, but not the second or fourth)

      The important concept here is that all arrays have a length member, which is public and final. The value that's in length is the actual length of the array, not the last usable index in it.

           for (int i = 0; i < Letters.length; i += 2)
             System.out.println(Letters[i]);
           

  3. Coding a Class [10 points]

    Write a class named Carrots that simulates a field of carrots. Full credit will require good use of encapsulation. This class has the following methods:

    1. A constructor having no parameters, that sets the initial number of carrots to 20, and the number last eaten to 0.
    2. An overloaded constructor taking two parameters, the initial number of carrots, and the number last eaten.
    3. An accessor method that returns the current number of carrots.
    4. A mutator method that causes some carrots to be eaten by hungry rabbits. Each time this method is called, the rabbits eat one more carrot than they did the last time it was called. Therefore, if the constructor were to initialize the number last eaten to 3, the first time this method is called, the rabbits would eat 4 carrots. The number of carrots should never be allowed to drop below 0.

    class Carrots
    {
      private int currentNum;
      private int lastEaten;
    
      public Carrots()
      {
        currentNum = 20;
        lastEaten = 0;
      }
        
      public Carrots(int newCurrent, int newEaten)
      {
        currentNum = newCurrent;
        lastEaten = newEaten;
      }
    
      public int carrotCount()
      {
        return currentNum;
      }
    
      public void getEaten()
      {
        currentNum -= ++lastEaten;
        if (currentNum < 0)
          currentNum = 0;
      } 
    }
    

  4. Using a Class [5 points]

    Write a code fragment that creates a Carrots object (with N carrots initially, and 1 carrot eaten last (you may assume N is an integer that has been declared and initialized)), and then repeatedly calls the mutator method until the carrots are all gone. Each time before decreasing the number of carrots, you should print out the current total. When there are no carrots left, print out "The End".

    Carrots McGregor = new Carrots(N, 1);
    while (McGregor.carrotCount() > 0)
    {
      System.out.println("There are " + McGregor.carrotCount() +
                         " carrot(s) in the field");
      McGregor.getEaten();
    }
    System.out.println("The End");