PuzzleGui
, this
method is called as part of the initialization of that class. Explain
the purpose of the method (do a best guess based on the names
of the parameters and variables).
private void constrainResize() { addComponentListener(new ComponentAdapter(){ public void componentResized(ComponentEvent e) { int w = getWidth(); int h = getHeight(); w = Math.max(w,h); setSize(w,w); } }); }
PuzzleGui
class the private instance
variable myUndo
is of type JMenuItem
. Here's
the code that initializes this variable. In broad terms, explain
each major part of this initialization.
myUndo = new JMenuItem(new AbstractAction("Undo") { public void actionPerformed(ActionEvent e) { myControl.undoMove(); } }); setEnabledUndo(false); menu.add(myUndo);
moveMaker
, an object
which is attached to each of the n2 JButtons
in a PuzzleGui
view that's the view of an n x n puzzle.
Why is it possible to share the same object among all the buttons,
e.g., rather than using a different button-specific listener for
each button? (explain what happens when a button is pressed based
on the code below).
ActionListener moveMaker = new ActionListener(){ public void actionPerformed(ActionEvent e) { int val = Integer.parseInt(e.getActionCommand()); myControl.makeMove(new PuzzleMove(val)); } };
PuzzleModel
makes
a move in the model. Explain based on best-guess information
(based on the names of the methods and variables) the purpose
of the statements in the scope of the if
statement
that calls isNeighbors
. Note: the instance
field myList
stores all moves for the purpose of
undo.
private boolean doMove(PuzzleMove move, boolean doStore) { int index = getIndex(move.getValue()); int blankIndex = getIndex(getBlankValue()); if (isNeighbors(index,blankIndex)) { int temp = myNumbers[blankIndex]; myNumbers[blankIndex] = myNumbers[index]; myNumbers[index] = temp; myControl.showBoard(myNumbers); if (doStore){ myList.add(move); } return true; } return false; }
PuzzleModel
. Explain
each line of this function. The instance variable myList
is of type java.util.LinkedList
rather than
ArrayList
because the latter field doesn't have
a method removeLast
.
/** * Undoes the last move (if there is one) and returns true iff * another undo is possible * @return true if another undo is possible, else returns false */ public boolean undo() { if (myList.size() != 0) { doMove((PuzzleMove) myList.removeLast(),false); } return myList.size() > 0; }
IconCounterCounter
class static method
getCount
returns the number of times it's called
with a specific parameter, e.g., it returns 1 the first time
called with a parameter whose value is the image for
"blarg.jpg"
, 2 the second time called with the
same parameter, etc. The prototype is:
public static int getCount(Image im)
Internally, the IconCounterCounter
class uses
a HashMap
to store an image and its associated count of how
many times getCount
has been called.
If a TreeMap
is used instead of a HashMap
internally, the code will compile fine. However, when the PuzzleApp
program is run, the following class-cast exception is generated. Explain
this.
Exception in thread "main" java.lang.ClassCastException: sun.awt.windows.WImage at java.util.TreeMap.compare(TreeMap.java:1081) at java.util.TreeMap.getEntry(TreeMap.java:341) at java.util.TreeMap.get(TreeMap.java:260) at IconCounterCounter.getCount(IconCounterCounter.java:31)