CPSC 170 Lab 2
Inheritance, Ragged Arrays, Polygons and GUIs

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

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.

Non-Rectangular ("Ragged") Arrays

We usually think of a two dimensional array as a table or matrix, but in Java it is also possible to create non-rectangular two dimensional arrays. There are two ways to do this:

File TwoD.java contains a shell for a program that reads in and prints out information about variable sized groups of people. This information will be stored in a two dimensional array, with each row representing one group. The program should ask the user for the number of groups and use this information to create the appropriate number of rows in the array. For each group it should then ask how many people are in the group and use this information to make that row of the array just the right length. It should then ask for the names for that group and store them in the array. After all the data has been entered, the names in each group should be listed. For example, a run of your program might look like this;

Enter number of groups: 3

Enter number of people in group 0: 2
Name 0: rachel
Name 1: elly

Enter number of people in group 1: 3
Name 0: spud
Name 1: madison
Name 2: floyd

Enter number of people in group 2: 1
Name 0: beauty

** GROUP SUMMARY **
GROUP 0: rachel elly 
GROUP 1: spud madison floyd 
GROUP 2: beauty 
In testing your program, try a group of size 0 (among others) to see what happens.

Print your program to turn in.

Polygons

A polygon is a multisided closed figure; a polyline is a line with an arbitrary number of segments. Both polygons and polylines are defined by a set of points, and Java provides graphics methods for both that are based on arrays. Carefully read pages 407-411 in the book about polygons and polylines, and study the Rocket example.

File DrawPerson.java contains an applet that creates a Person object and draws it. File Person.java contains the Person class, where a Person currently consists only of a blue shirt. Copy these files and the associated html file to your directory, compile DrawPerson.java, and run it using the appletviewer to see what it does. Now modify Person.java so that it draws a whole person as follows:

  1. Add pants to go with the shirt (they should be a different color). You will need to declare pantsX and pantsY arrays like the shirtX and shirtY arrays and figure out what should go in them. (Note that these are instance variables so they are visible to all of the methods. This may not seem necessary now but will be useful later.) Create a drawPants method and make the draw method call it as well as the existing drawShirt method.

  2. Add a head. This can just be a circle (or oval), so you won't need to use the Polygon methods. Declare variables headX and headY to hold the position of the head (its upper lefthand corner), and use them in the drawHead method.

  3. Add hair to the head. This is probably best done with a polygon, so again you'll need two arrays to hold the points.

  4. Add a zigzag across the front of the shirt. (You can draw this in the drawShirt method if you want.) Use a polyline.

  5. Write a method move(int x, int y) that moves the person by the given number of pixels in the x and y direction. To keep it clean, write separate methods moveShirt, movePants, etc, that go through the appropriate arrays (or in the case of the head, the x and y coords) and increment all of the coordinates by the x or y value as appropriate.

  6. Now modify your applet (in the DrawPerson class) so that it draws the person three times, moving him/her in both the x and y directions each time (you decide how far). Hint: remember the repaint() method.

Print DrawPerson.java and Person.java to turn in.

Checkboxes and Radio Buttons

Last semester you worked with GUIs in lab 11, where you wrote event-driven programs that did things when the user pushed a button (remember the voting program?). 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.