CPSC 170 Lab 5: Inheritance and Mouse Events

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

Inheritance

File Dog.java contains a declaration for a Dog class. Save this file to your directory and study it -- notice what instance variables and methods are provided. Files Labrador.java and Yorkshire.java contain declarations for classes that extend Dog. Save and study these files as well.

File DogTest.java contains a simple driver program that creates a dog and makes it speak. Study DogTest.java, save it to your directory, and compile and run it to see what it does. Now modify these files as follows:

  1. Add statements in DogTest.java after you create and print the dog to create and print a Yorkshire and a Labrador. Note that the Labrador constructor takes two parameters: the name and color of the labrador, both strings. Don't change any files besides DogTest.java. Now recompile DogTest.java; you should get an error saying something like
    ./Labrador.java:16: cannot resolve symbol
    symbol  : constructor Dog  ()
    location: class Dog
    
    But if you look at line 16 of Labrador.java, it's just a {. In fact, the constructor the compiler can't find (Dog()) isn't called anywhere in this file.
    1. What's going on? Hint: What did we say about constructors in subclasses? Fix the problem (which really is in Labrador) so that DogTest.java creates and makes the Dog, Labrador, and Yorkshire all speak.

  2. Add code to DogTest.java to print the average breed weight for both your Labrador and your Yorkshire. Use the avgBreedWeight() method for both. Look at the error that you get. Figure out what is wrong and fix the problem by adding the needed code to the Yorkshire class.

  3. Add an abstract int avgBreedWeight() method to the Dog class. Remember that this means that the word abstract appears in the method header after public, and that the method does not have a body (just a semicolon after the parameter list). It makes sense for this to be abstract, since Dog has no idea what breed it is. Now any subclass of Dog must have an avgBreedWeight method; since both Yorkshire and Laborador do, you should be all set.

    Save these changes and recompile DogTest.java. You should get an error in Dog.java (unless you made more changes than described above). Figure out what's wrong and fix this error, then recompile DogTest.java. You should get another error, this time in DogTest.java. Read the error message carefully; it tells you exactly what the problem is. Fix this by changing DogTest (which will mean taking some things out).

Print DogTest.java, Dog.java, Labrador.java, and Yorkshire.java to turn in.

Overriding the Equals Method

File Player.java contains a class that holds information about an athlete: name, team, and uniform number. File ComparePlayers.java contains a skeletal program that uses the Player class to read in information about two baseball players and determine whether or not they are the same player.
  1. Fill in the missing code in ComparePlayers so that it reads in two players and prints "Same player" if they are the same, "Different players" if they are different. Note that the Player class has a readPlayer() method that does all the work for you! Use the equals method, which Player inherits from the Object class, to determine whether two players are the same. Are the results what you expect?

  2. The problem above is that as defined in the Object class, equals does an address comparison. It says that two objects are the same if they live at the same memory location, that is, if the variables that hold references to them are aliases. The two Player objects in this program are not aliases, so even if they contain exactly the same information they will be "not equal." To make equals compare the actual information in the object, you can override it with a definition specific to the class. It might make sense to say that two players are "equal" (the same player) if they are on the same team and have the same uniform number.
Print Player.java to turn in.

A Sorted Integer List

File IntList.java contains code for an integer list class. Save it to your directory and study it; notice that the only things you can do are create a list of a fixed size and add an element to a list. If the list is already full, a message will be printed. File ListTest.java contains code for a class that creates an IntList, puts some values in it, and prints it. Save this to your directory and compile and run it to see how it works.

Now write a class SortedIntList that extends IntList. SortedIntList should be just like IntList except that its elements should always be sorted. The easiest way to do this is probably to modify the add strategy: put the new element at the end as before, then use insertion sort to sort the array. Remember that insertion sort runs in linear time on nearly-sorted arrays, so this is about as efficient as inserting the item directly into its sorted place.

Think carefully about what methods and instance variables you have to define in SortedIntList and what you can inherit directly from IntList -- don't override anything you don't have to. To test your class, modify ListTest.java so that after it creates and prints the IntList, it creates and prints a SortedIntList containing the same elements. Add them in the same order as for the original list, but they should come out in sorted order.

Print SortedIntList.java and IntList.java to turn in.

Mouse Events

You have written Java programs that respond to GUI events -- a user pressing a button, checking a box, etc. Programs can also respond to mouse events -- moving and clicking the mouse -- that are not tied to any particular GUI component. Java divides these into two categories: mouse event and mouse motion events. A MouseListener (a class that implements the MouseListener interface) must provide bodies for methods corresponding to each of the mouse events. A MouseMotionListener (implementing the MouseMotionListener interface) must provide bodies for methods corresponding to each of the mouse motion events. These events are described on pages 425-426 of the text.

  1. File MousePlay.java contains a program that displays a message saying when the mouse button is pressed and released. It uses MousePlayPanel.java to define the JPanel that is actually displayed. Save these files to your directory, run MousePlay to see how it works, and open MousePlayPanel in emacs. Note that MousePlayPanel has an inner class MousePlayListener that implements MouseListener by giving bodies for the five MouseListener methods. Even though nothing is done when the mouse is clicked or when it enters or exits, those methods still appear with empty bodies -- this is required for the MouseListener interface to be implemented.

    Modify MousePlayPanel so that it also prints appropriate messages when the mouse is clicked, when it enters the panel, and when it exits. Play with the resulting program until you understand how the mouse events are generated.

  2. Files Dots.java and DotsPanel.java contain revised versions of the code in listings 7.23 and 7.24 in the text. Dots draws a dot every time the uses presses the mouse button; only the current dot is shown in the revised version. Save these files to your directory and run Dots to see how it works. Then modify DotsPanel as follows:
    1. Make the dots that appear alternately red and green. To do this, you will need to make the following changes:
      • Add an instance variable to hold the dot color.
      • Add an instance variable to count the number of dots that have been drawn.
      • When the mouse is clicked, in addition to setting the draw point you need to update the counter and change the dot color to green if the counter is odd, to red if it is even. Then call repaint.
    2. Make the program draw a large dot if the user clicks, and a small dot if the user presses and releases the mouse elsewhere. Continue to alternate between red and green. Remember the difference between a mouse click and a press/release -- a click is released in the same location where it was pressed. Note that a mousePressed event is generated in either case. The current dots have a radius of 6 pixels; modify this to 20 pixels for clicked dots. You'll need to:
      • Change RADIUS from a constant to a variable (also make it lower case).
      • Add code for the mouseClicked event so that a large dot is drawn. Remember that mousePressed will always be called before mouseClicked, so you'll see a small dot briefly until you release the mouse.
    3. Modify your code so that the small dot is drawn where the mouse is released, not where it is pressed. Remember that mousePressed is called on both press/releases and clicks; this is true for mouseReleased as well. From the way your program behaves, you should be able to tell the order in which mousePressed, mouseClicked, and mouseReleased are called on a click.

    Print DotsPanel.java to turn in.

  3. File Circles.java sets up a GUI that creates and draws a circle as defined in Circle.java of random size and color at each mouse click. Each circle replaces the one before it. The code to handle the mouse clicks and do the drawing is in CirclePanel.java. Save these files to your directory, compile them and run them and experiment with the GUI. Then modify these files as described below.
    1. This program creates a new circle each time -- you can tell because each circle is a different color and size. Write a method void move(Point p) for your Circle class that takes a Point and moves the circle so its center is at that point. Now modify your CirclesListener class (defined inside CirclePanel) so that instead of creating a new circle every time the user clicks, it moves the existing circle to the clickpoint if a circle already exists. If no circle exists, a new one should be created at the clickpoint. So now a circle of the same color and size should move around the screen.

    2. Write a method boolean isInside(Point p) for your Circle class that takes a Point and tells whether it is inside the circle. A point is inside the circle if its distance from the center is less than the radius. Recall that the distance between two points (x1,y1) and (x2,y2) is sqrt((x2-x1)2+(y2-y1)2.

    3. Now modify the mousePressed method of CirclesListener so that the GUI behaves as follows:
      • If there is no circle (i.e., it is null) and the user clicks anywhere, a new (random) circle should be drawn at the click point.
      • If there is a circle on the screen and the user clicks inside that circle, the circle should go away. (Hint: To make the circle go away, set it to null and repaint.)
      • If there is a circle on the screen and the user clicks somewhere else, the circle should move to that point (no change from before).
      So the logic for mousePressed should look like this:

        
        if there is currently no circle
           create a new circle at the click point
        else if the click is inside the circle
           make the circle go away
        else
           move the circle to the click point
      
        repaint
      

    4. Add bodies for the mouseEntered and mouseExited methods so that when the mouse enters the applet the background turns white, and when it exits the background turns blue. Remember that you can set the background color with the setBackground method.

    5. Now modify the code in CirclePanel.java so that a circle is drawn when the user presses a mouse, but the user can drag it around as long as the mouse button is depressed. If the mouse button is released and then pressed again, a new circle is created, which can then be dragged around. You will need to make the following changes:
      1. In the CirclePanel constructor, make the CirclesListener object listen for both mouse events and mouse motion events. Don't create two separate listeners -- use the same object for both.

      2. Make the CirclesListener class implement the MouseMotionListener interface in addition to the MouseListener interface. This requires two steps:
        • Note in the header that CirclesListener implements MouseMotionListener.
        • Add bodies for the two MouseMotionListener methods, mouseDragged and mouseMoved. In mouseDragged, simply move the circle to the point returned by the getPoint method of the MouseEvent and repaint. (Remember that you already wrote a move() method for the Circle class.) Provide an empty body for mouseMoved.

      Print CirclesListener.java and Circle.java to turn in.

What to Turn In

Turn in hardcopy of DogTest.java, Dog.java, Labrador.java, Yorkshire.java, Player.java, SortedIntList.java, ListTest.java, DotsPanel.java, CirclesListener.java, and Circle.java. Tar your lab5 directory and e-mail it to me with cpsc170 lab5 in the Subject line.