Questions about PuzzleModel, PuzzleView/Gui, PuzzleController

  1. The following method is part of 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);
    		}
    	    });
    
        }
    
    

  2. In the 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);
    

  3. The code below creates 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));
             }
      };
    
    

  4. The code below from 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;	
        }
    
  5. The code below is from 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;
        }