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.
  1. 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:

  2. Implement the pileOfCards class according to the specifications in its JavaDoc.

  3. 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.

  4. 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.

  5. 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).

  6. 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:

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:

  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: 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.

  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.

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:

  1. Declare three objects small, medium, and large of type JRadioButton.

  2. 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.

  3. Instantiate a button group object and add the buttons to it.

  4. 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.

  5. In StyleGUI() instantiate a SizeListener and add it to each button. Also add each button to the panel.

  6. 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: