CPSC 170 Lab 5: More GUIs and Recursion

As usual, create a lab5 subdirectory for today's lab, open this document in Netscape, and start emacs.

More Ways to Listen

  1. File BGChanger.java contains an applet that has a single button that changes the background color from red to blue -- this was the last problem in last week's lab. You can work from this file or from your solution. Save it as BGChanger1.java in your directory (you will also need to change the class name). Now modify it so that it has two buttons instead of just one. One button should say "Change to red" and the other should say "Change to blue". The background should be white when the applet is loaded, but should change to the color indicated when a button is pressed. The text will not change on the buttons. Note the following:

    Print BGChanger1.java.

  2. Copy your program from #1 into file BGChanger2.java and modify it so that instead of using setActionCommand/getActionCommand to determine which button generated an event, you have two (inner) classes that implement the ActionListener interface. Each will have its own actionPerformed method; the actionPerformed method in one class should set the background to blue, and in the other should set it to red. Now create an instance of the first (blue) class to listen for action events on the blue button, and create an instance of the second (red) class to listen for action events on the red button. Note that you no longer need your original actionPerformed method, and that the applet should no longer be an ActionListener. See p. 360-361 for an example of this way to make a listener, except this example is using a MouseListener instead of an ActionListener and the inner class extends a MouseAdapter instead of implementing an ActionListener. But it's the same concept!

    Print BGChanger2.java.

  3. File DrawCircles.java contains a definition for an applet that you wrote in lab 11 last semester. It uses the Circle class in Circle.java. Save these and the associated html file to your lab5 directory and run the applet. You should see the following behavior: Study the code for DrawCircles.java so you understand how it works. You can look at the code for Circle.java if you like, but all you need to know is how its public methods are used in DrawCircles. You won't need to modify Circle.java.

    Now modify DrawCircles.java as follows:

    1. Instead of listening for the mouse events directly in the applet, create an inner class that extends the MouseAdapter class and make an instance of this class listen for mouse events. This is much like what you did for BGChanger2.java. The modified applet should behave just like the original.
    2. Change the behavior of DrawCircles so that the user can use the mouse to drag the circle around. That is, if the user presses the mouse button and holds it down while moving the mouse, the circle should follow it. To do this you will need to create a MouseMotionListener to listen for the MouseEvent that is generated when the mouse is dragged. Recall from last semester that the MouseMotionListener interface has just two methods: public void mouseMoved(MouseEvent e) and public void mouseDragged(MouseEvent e). You will need to implement just the mouseDragged method; when it is called, just get the click point from the event that is passed in using the getPoint() method, just like in mouseClicked. Then if a circle exists, move it to the click point and repaint. This definition for mouseDragged should go in its own inner class that extends the MouseMotionAdapter class, and you will need to create an instance of it to pass to addMouseMotionListener in the applet.

    Print DrawCircles.java.

Recursion

  1. Computing a positive integer power of a number is easily seen as a recursive process. Consider ab: File Power.java contains a main program that reads in integers base and exp and calls method power to compute baseexp. Fill in the code for power to make it a recursive method to do the power computation. The comments provide guidance.

    Print Power.java.

  2. File IntegerList.java contains the IntegerList class from lab 1; IntegerListTest.java contains a simple menu-driven test program that lets the user create, sort, and print a list and search for an element using sequential or binary search. (Remember that the list has to be sorted before you can use binary search!) The only change in this file from lab 1 is that the binary search method is recursive (binarySearchRec). Study it carefully; notice that it immediately calls reallyBinarySearchRec, which does all the work -- exactly what we discussed in class today.

    Add a method seqSearchRec to the IntegerList class that does a recursive sequential search. Now change IntegerListTest.java so that it calls seqSearchRec instead of seqSearch when the user asks for a sequential search. Think about how sequential search can be viewed recursively; if you are looking for an item in a list starting at index i:

    Like binarySearchRec, seqSearchRec will need to set up another method (e.g., reallySeqSearchRec) to do most of the work. This is because the recursive method needs more information (the index to start at) than you want to pass to the top-level search routine, which just needs the thing to look for.

    Print IntegerList.java.

HAND IN: