CPSC 170 Lab 4: Inheritance

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

Introduction

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 declartions for a 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:18: cannot resolve symbol
    symbol  : constructor Dog  ()
    location: class Dog
    
    But if you look at line 18 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?)
      =>



    2. 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. What error do you get? Why?

    =>



    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, Labrador.java, and Yorkshire.java.

A Sorted List Class

File IntList.java contains code for a 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 what it does.

Now write a class SortedIntList that extends IntList. SortedIntList should be just like IntList except that its elements should always be sorted. You can either insert the elements in the right place in the array or insert them at the end and then call a sort routine -- you choose. 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.

Graphical User Interfaces (GUIs)

GUI objects in Java are called components, and any class that defines a GUI object must be a subclass of the Component class. The standard Component classes include Button, TextField, Label, and Container. Container is an abstract class, so it can't be instantiated, but its subclasses are Components that can have other things added to them. An Applet is a Container, which means that we can add other components to an applet. For more information on the classes Java Class Library, see the online Java documentation. Scroll through the classes in the box on the left until you find Component; click on this and you will see a description of the Component class and its methods in the center window, including a list of its subclasses. Follow the links to find descriptions of the subclasses.

  1. File Greeting.java contains a simple GUI that asks the user to enter his or her name, then prints a greeting when the user presses Enter. Save this program to your directory along with its html file. Compile the program and run it with the appletviewer to see how it works. Now modify it as follows:
    1. In init, the greeting label is initialized to be a long string of spaces. Change this so that it is the empty string ("") and recompile and run the program. You should see that the message is truncated -- only the H shows. This is because the size of a Label is fixed when the label is created and it doesn't change. This makes sense; the layout manager can't figure out where to put the components unless it knows how big they are, and you don't want them shifting around every time you change a label! So if you are using a label to display a result, you will have to anticipate how big the result could be and initialize the label to a string that size. Set the greeting label initialization to its original value.

    2. Modify the applet so that it includes a button that says "Show Greeting". Instead of printing the greeting when the user presses Enter in the textfield, it should be printed when the button is pressed. You will need to do the following:
      • Declare a Button with the other component declarations.
      • Create the Button object in init. The Button constructor takes one argument, a string containing the text that will be displayed on the button (its label).
      • Add the button to the applet.
      • Make the applet listen for ActionEvents from the button, not the textfield.
      • Do you need to change the actionPerformed method?

  2. Although a button's label (the text on the button) usually remains fixed, it can be changed with the void setLabel(String label) method of the Button class. The current label on a button can be examined with the String getLabel() method. You will need these methods for this exercise.

    File BGChanger.java contains the skeleton for an applet that should behave as follows:

    Following the comments in the file, fill in the code necessary to make this happen. Use the setBackground method of the Applet class to make the applet appear a certain color.

    In testing this you'll find that the button is a little hard to see, since it is the same color as the background. Modify your code to make the button always be white. Hint: The setBackground method we use in Applet is actually inherited from Component, and Button is also a Component.

HAND IN: