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.
this
this
is only for access inside the
class.static
are instance variables.static
variables are visible for
every object in the class.
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."); }
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:
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; } }