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 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.");
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:
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; } }