CPSC 170 Lab 3
 
Array Lists, GUIs and Inheritance
As usual, create a lab3 subdirectory for today's lab, open this
document in Firefox, and start emacs.
 ArrayLists 
The file  card.java  conatains a basic representation 
of a playing card. Note that some of the features discussed in class are 
missing (notably: getValue and a way to compare cards).  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 manage 
the data for you. 
-   The code has been commented in Javadoc style.   Run javadoc on the pileOfCards 
code to generate the html description of the class.   There are a couple of things 
to note:
-  Notice in the HTML 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).
-  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 create a CardPanel 
class with the 
following components: 
 
-  a pile of cards (preferably containing one of each of the cards that can be created).
-  a JLabel that displays the image of card most recently removed from the pile.  The 
file  GPLcards.tgz  contains the images used by the card class.
You will need to untar the file in your lab3 directory (tar xzf GPLcards.tgz).
-  a button that, when pressed, removes a card from the pile and updates the icon on 
the JLabel.
 
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 
(passing on the same arguments) 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.  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:
- 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.
- What's going on? Hint: Think about what happened in the previous activity
regarding constructors and subclasses.  Fix the problem (which really is in Labrador) so that DogTest.java 
creates and makes 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.
Checkboxes and Radio Buttons
Up until now, you hav been creating simple GUIs with panels, textboxes and buttons.  
Recall that these programs used ActionListeners to carry out
certain actions when a button was pressed.
In today's exercise you will see two new GUI components (checkboxes and radio buttons)
along with their listeners (ItemListeners and ActionListeners respectively).
The files StyleOptions.java and 
StyleOptionsPanel.java are adapted from Listings 5.22
and 5.23 of the text.  (A variable 
fontSize is used rather than an integer literal in various instances and the
variable style is an instance variable rather than local to the 
itemStateChanged method).  Save these files to your directory and compile and
run StyleOptions.java to see how it works.  This is the driver; StyleOptionsPanel.java contains
the code for the GUI.
Now you will add a set 
of 3 radio buttons to let the user choose among three font sizes.  The 
method of adding the radio buttons will be very similar to that in the
QuoteOptionPanel class (Listing 5.25 of the text), so study this example before you 
continue.
Do the following to add the radio buttons to the GUI: 
- Declare three objects small, medium, and
large of type JRadioButton.
 
- Instantiate the button objects labeling them "Small Font," "Medium Font,"
"Large Font." Initialize the large font button to true.  Set the
background color of the buttons to cyan.
 
- Instantiate a button group object and add the buttons to
it.
 
- Radio buttons produce action events so you need to add an inner
class (name it SizeListener) to implement ActionListener and
listen for radio button clicks.  The code for actionPerformed
can be similar to that in the QuoteListener in Listing 5.25. (Or if you prefer,
you can use the isSelected method to see which button was selected
instead of getting and checking the source.)  
You need to set the fontSize variable (use 12 for small,
24 for medium, and 36 for large) in the if statement, then call the
setFont method to set the font for the saying object.
 
- In StyleGUI() instantiate a SizeListener and add it
to each button.  Also add each button to the panel.
 
- Compile and run the program.  Note that as the font size changes
the checkboxes and buttons re-arrange themselves in the panel.  We will 
revisit the issue of controlling layout later in the course.
 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 lab3 directory with the command 
 
tar czf lab3.tgz . 
 
-  send the tar file to me via e-mail.