CS 302 sect. 301
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 An array of booleans can be passed as a parameter to a method expecting a boolean.
      False. Arrays are of type "array-of-whatever", not of type "whatever". You can only pass an array of booleans to a method expecting a boolean.
    2. T F Arrays are instantiated using new.
      True. Normal syntax. Unless you're using an initializer list.
    3. T F A Java array of size N is indexed from 1 to N.
      False. Indexed 0 to N-1. This is a common thing to mess up, so be careful.
    4. 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.
    5. T F Once it is instantiated, an array's size cannot be changed.
      True. Yep. You'll need to make a new one if you need a bigger block.
    6. T F Arrays are similar to, but are not, Java objects.
      False. There are only two kinds of things in this world: primitives and objects. Despite some funny syntax, Java arrays are objects, and you can do anything to them that you can do to other objects.

  2. Writing Arrays (9 points)

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

      Pretty straightforward if you know the syntax:

           char[] charArray = { 'f', 'e', 'd', 'c', 'b', 'a' };
           
      (Notice this way does not use new.)
    2. [6 points] Assume an array Digits of unknown size has been instantiated and had values filled in. Write a code fragment to print out the elements of Digits in reverse order (i.e., printing out the last element first)

      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 = Digits.length - 1; i >= 0; i--)
             System.out.println(Digits[i]);
           

  3. Coding a Class [10 points]

    Write a class named Rabbits that simulates a population of rabbits. 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 rabbits to 2, and the population limit to 100.
    2. An overloaded constructor taking two parameters, the initial number of rabbits in the population, and the population limit.
    3. An accessor method that returns the current number of rabbits.
    4. A mutator method that doubles the number of rabbits in the current population. If, after doubling, the population is over the limit, then the rabbits have run out of carrots and the population should be set to 0.

    class Rabbits
    {
      private int currentPop;
      private int maxPop;
    
      public Rabbits()
      {
        currentPop = 2;
        maxPop = 100;
      }
        
      public Rabbits(int newCurrent, int newMax)
      {
        currentPop = newCurrent;
        maxPop = newMax;
      }
    
      public int rabbitCount()
      {
        return currentPop;   
      }
    
      public void multiply()
      {
        currentPop *= 2;
        if (currentPop > maxPop)
          currentPop = 0;
      } 
    }
    

  4. Using a Class [5 points]

    Write a code fragment that creates a Rabbits object (with 5 rabbits in the initial population, and a population limit of N (which you can assume is an integer that has been declared and initialized)) and then repeatedly increases the population until it exceeds the limit. Each time the population increases, print out the new total. When the population has gone over the limit, end the loop and print out "The End".

    Rabbits Warren = new Rabbits(5, N);
    while (Warren.rabbitCount() > 0)
    {
      Warren.multiply();
      System.out.println("There are now " + Warren.rabbitCount() + " rabbits.");
    }
    System.out.println("The End.");