CS 302 sect. 301
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 Members of a class can be accessed outside of the class definition using this
        False. this is only for access inside the class.
      2. T F Classes define variables for an object and reserve memory for them.
        False. 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.
      3. T F Mutator methods enable changing the values of private instance variables.
        True. That's what mutators do.
      4. 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.
      5. T F An abstraction hides details.
        True. Again, virtually by definition.
      6. T F Methods are shared among all objects of a class.
        True. Unlike variables.
      7. 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
      8. T F A method's signature consists of the method's parameter type(s) and its return type.
        False. For two reasons: first, the return type is not part of the signature. Second, because the signature also consists of the number and order of the parameters as well as their types.

  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 Cone object with no scoops in it, then adds N servings (each of two scoops) to it. Each time you add a serving to the cone, print out the total number of scoops in the code. You may assume N is an integer variable that has been declared and initialized to some value.

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

    Cone waffle = new Cone(0, 2);
    for (int i = 0; i < N; i++)
    {
      waffle.addServing();
      System.out.println("There are now " + waffle.getScoopCount() + "
      scoops.");
    }
        

  4. Coding a Class [16 points]

    Write a class named Cone that simulates an ice-cream cone. 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 scoops to 0, and the number of scoops per serving to 1.
    2. An overloaded constructor taking two parameters, the initial number of scoops and number of scoops per serving.
    3. An accessor method that returns the current number of scoops in the cone.
    4. A service method that adds one serving's-worth of scoops to the cone.

    People had a lot of problems with this. I'll try and comment my solution to point out problems.

    class Cone
    {
       // 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 scoops,
       // and the number of scoops in one serving.
       private int numScoops; 
       private int scoopsPerServing;
    
       // The first constructor takes no arguments, and sets the number of
       // scoops to 0, and the number of scoops per serving to 1. Since
       // it's a constructor, it has the same name as the class and has no
       // return type (not even void!).
       public Cone() 
       {
         numScoops = 0;
         scoopsPerServing = 1;
       }
       
       // 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
       // numScoops, we would be unable to access the numScoops 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 Cone(int initialNumScoops, initialScoopsPerServing)
       {
         numScoops = initialNumScoops;
         scoopsPerServing = initialScoopsPerServing;
       }
       
       // 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 scoops in the cone.
    
       public int getScoopCount()
       {
         return numScoops;
       }
      
       // A service, or mutator method, changes the value of a class
       // variable but does not usually return or print anything. 
       // This one adds a number of scoops equal to one serving to the
       // total number of scoops.
      
       public void addServing()
       {
         numScoops += scoopsPerServing;
       }
    }