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

Instructor: Dan Shiovitz

Name: _____________________________________________________________

Mean around 20 this time. Um. I'm not sure exactly what the deal is here. It may be that lecture didn't spend enough time on classes, or if you guys aren't reading the material, or what. But since you absolutely can't do program 4 without understanding the concepts this quiz covers, I hope everyone will study this solution carefully to get what their mistakes were.

  1. Terms and Concepts (8 points)

    1. [8 points total - 1 each] Is the statement below true or false? Circle one:
      1. T F A method's signature consists of the method's parameter type(s).
        False. The signature also consists of the number and order of the parameters as well as their types.
      2. T F Methods are shared among all objects of a class.
        True. Unlike variables.
      3. T F Class (i.e., static) methods are called through the class in which they are defined.
        True. Static methods and variables are referenced with classname.methodname, whereas non-static methods and variables are referenced with objectname.methodname
      4. T F Classes define variables for an object but do not reserve memory for them.
        True. Classes do define the variables for objects, but do not reserve memory for them -- which makes sense, because they don't know how many objects of that class will be created, so they wouldn't know how much memory to reserve.
      5. T F Accessor methods enable changing the values of private instance variables.
        False. That's what mutators do.
      6. T F Members of a class can be accessed outside of the class definition using this
        False. this is only for access inside the class.
      7. T F Variables declared static are instance variables.
        False. "Instance" in this case means "for a single object", while static variables are visible for every object in the class.
      8. T F Encapsulation hides details.
        True. Again, virtually by definition.

  2. QUESTION 2 IS ON THE BACK. TURN THE PAPER OVER AND DO IT FIRST.

  3. Using a Class [7 points]

    Write a code fragment that creates a Popcorn object with 100 pieces of popcorn, then takes ten handfuls (at the beginning, ask the user how many pieces they would like to take in each handful). At the end, print out how many pieces are left. Make sure to handle in some sensible way the case where the user tries to take more pieces than there is popcorn remaining.

    Here's one solution, based on the class definition given later:

    System.out.println("How many pieces of popcorn would you like to take " +
                       "per handful?");
    // We'll assume stdio is defined elsewhere
    int handSize = Integer.parseInt(stdio.readLine());
    Popcorn bowl = new Popcorn(100, handSize);
    
    for (int i = 0; i < 10; i++)
    {
      if (bowl.getCount() >= handSize)
        bowl.takeHandful();
    }
    System.out.println("There are " + bowl.getCount() + " piece(s) left.");
    

  4. Coding a Class [15 points]

    Write a class named Popcorn that simulates a bowl of popcorn. 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 pieces of popcorn in the bowl to 50, and the number of pieces per handful to 5.
    2. An overloaded constructor taking two parameters, the initial number of pieces in the bowl and the number of pieces per handful.
    3. An accessor method that returns the current number of pieces of popcorn in the bowl.
    4. A service method that takes one handful from the bowl.
    class Popcorn
    {
       // We declare our instance variables *private*. That's important
       // for encapsulation reasons, so we know all access to these
       // variables goes either through the accessor or the mutator
       // methods of the class.
     
       // We have two variables in the class, the total number of pieces,
       // and the number of pieces in one handful
       private int numPieces; 
       private int piecesPerHandful;
    
       // The first constructor takes no arguments, and sets the number of
       // pieces to 50, and the number of pieces per handful to 5. Since
       // it's a constructor, it has the same name as the class and has no
       // return type (not even void!).
       public Popcorn() 
       {
         numPieces = 50;
         piecesPerHandful = 5;
       }
       
       // The overloaded constructor takes two arguments, the number of
       // scoops and the number of scoops per serving. NOTE: please pay
       // attention to the distinction between the formal parameters and
       // the class variables. If we were to use a formal parameter named
       // numPieces, we would be unable to access the numPieces variable
       // in the object. Therefore, we have to give the parameters
       // different names, but copy their values into the class variables.
       // It's also a constructor, so it has the same name and return type
       // as previous.
    
       public Popcorn(int initialNumPieces, initialPiecesPerHandful)
       {
         numPieces = initialNumPieces;
         piecesPerHandful = initialPiecesPerHandful;
       }
       
       // An accessor method is one that returns the value of a class
       // member but does NOT modify the class. This one returns the total
       // number of pieces in the bowl
    
       public int getCount()
       {
         return numPieces;
       }
      
       // A service, or mutator method, changes the value of a class
       // variable but does not usually return or print anything. 
       // This one takes out a number of pieces equal to one handful.
      
       public void takeHandful
       {
         numPieces -= piecesPerHandful;
    
         // just in case, and you didn't have to do this on the quiz,
         // let's make sure there's never negative pieces. We can do this
         // because we know the only place the number of pieces can be
         // modified is through this function, because it is a private variable!
     
         if (numPieces < 0)
           numPieces = 0;
       }
    }