CPSC 170 Lab 2
Array Lists, Inheritance, and Mouse Listeners
As usual, create a lab2 subdirectory for today's lab, open this
document in Firefox, and start Eclipse.
ArrayLists
The file Card.java conatains a basic
representation of a playing card. The file PileOfCards.java contains the stub code
for a class that manages collection of cards. This is very similar to
last week's CDCollection. The major difference is that you will be
implementing pileofCards using an ArrayList instead of an array. This
means that you don't have to worry (too much) about managing the way
the collection is stored (are you ever going to go "outOfBounds"?).
Instead you are just going to use the predefined interface of the
arraylist to help manage the data for you.
- The code has been commented in Javadoc style. Generate the javadoc
for the PileOfCards code to generate the html description of the class.
Study the HTML file and make not of the following:
- There are multiple places that identify the lineage of
pileofCards. In this case, it is pretty simple - it is a child of
java.lang.Object. However, in more complicated objects, this document
would tell you the entire list of what other classes this class is
decended from.
- Along the same lines, notice that there is a listing of the
methods that this object has inherited (from object). Remember in
class, we talked about how the Object class has a toString method?
Why does pileOfCards not inherit the toString Method from Object?
- Some of the comments have questions - you should answer them
before writing any code. A good practice to get into is to understand
the design of a class and how it works as much as possible before you
try to implement it. (It helps to have the comments and the skeleton
fleshed out like this). Doing this not only clarifies the tasks ahead
for you, it also makes it easier to verify that your code will work
properly, and saves you from recoding portions of your program as you
redesign on the fly.
- Implement the pileOfCards class according to the specifications
in its JavaDoc.
- If you have thought about it correctly, you should be confident
that pileOfCards is free of logic and run-time errors. If you are not
confident, take a few moments to think about where you may have gone
wrong and convince yourself that it is correct. Make any changes that
you feel are necessary.
- If you skipped over step 3, because there was nothing to type,
you'll regret it in the long run - I'm serious - stop and think about
your code for a second.
- When you are confident that your code is correct, write a
text-based driver program to verifies that your pileOfCards code works
correctly. (Make sure that your this program will convince me!
P.S. there is nothing "random" about this program). The output of your
program should include statements or what condition is being checked, the
anticipated outcome of this statement and the actual outcome of this statement.
Observe: It is not acceptable for the PileOfCards class to generate any
run-time errors - all "bad" conditions should be gracefully dealt with.
- Write a graphical driver program that interacts with a pile of
cards. The file CardGame.java contains
a simple stub for a JFrame that contains a
CardPanel (which is-a
JPanel). You will need to update the CardPanel class with the following
changes:
Inheritance
When one class is derived from another, we know that the child class can refer
to the parent class using a special identifier ________________________;
In essence we are saying,
"do what the parent does.... plus a little more".
We often see this used with constructors - the initial configuration is the
same as the parent, but we also are concerned about initializing data that is
unique to the child class. Since this is such a common occurrence,
the constructor for a child class implicitly invokes the parent's constructor
if there is not an explicit call to the
constructor.. To see this in action, download
ClassA.java,
ClassB.java and
ABInherit.java
After looking at the code, compile and execute ABInherit.java - Does it do what you expected?
Inheritance with methods
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.
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 then run it to confirm
what it does.
The file Yorkshire.java
contain declarations for a class that extend Dog.
Save and study this files as well. Modify DogTest.java to add statements to
add and print a Yorkshire (also make it speak).
The file Labrador.java also extends the Dog
class. However, note that the Labrador constructor takes two parameters:
the name and color of the labrador, both strings. Notice that this class
contains an error:
Implicit super constructor Dog() is undefined
What's going on? Hint: Think about what happened in the previous activity
regarding constructors and subclasses.
Fix the problem and then modify DogTest.java to create and make
the Dog, Labrador, and Yorkshire all speak.
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.
Print DogTest.java, Dog.java, Labrador.java, and Yorkshire.java to turn in.
Mouse Events
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 events 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
in section 7.9 of the text.
- 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,
open them in Eclipse, and
run MousePlay to see how it works.
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.
- Files Dots.java and
DotsPanel.java contain revised
versions of the code in listings
7.18 and 7.19 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:
- 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 pressed, 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.
- Modify your code so that the dot is drawn where the mouse is
released, not where it is pressed.
Print DotsPanel.java to turn in.
HAND IN:
- Printouts of the files that you made changes to.
Make sure that your name is included in the header comments (not just written on)
- Tar the files in your lab2 directory with the command
tar czf lab2.tgz .
- send the tar file to me via e-mail.