CPSC 124 Introduction to Programming Spring 2008

Lab 10: Inheritance

Introduction

In this lab, you will begin using class inheritance when writing your programs. You will also write your own collections and wrappers.

Setup

Create a lab10 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/s08/cs124/labs/lab10/ to your lab10 directory.

Classes and Objects

This lab covers Chapter 5 of the book. If you don't have your book with you, the book can be found online at http://math.hws.edu/javanotes5/.

Exercises

Here are the exercises for this week's lab, due next week at the start of lab.

  1. Modify the file DMV.java (in the lab10 directory) to extend the Vehicle class. You should create three new classes that extend vehicle: Car, Truck, and Motorcycle. The Car class should contain a variable that indicates the number of doors, the Truck class should contain a variable that contains the number of axels, and the motorcycle should contain a boolean that indicates whether it has a sidecar. You should also write a constructor for each class, which uses parameters to set all of the Car, Truck, and Motorcycle variables (including inherited variables). You also must write a print method in each of the three classes (since this method is declared abstract in the Vehicle class) that prints out all the variables in that class, including inherited variables as well as the name and license number of the owner of the vehicle. Finally, in the main() method in the main class DMV, create at least one Car, Truck, and Motorcycle object and print each one of them out. You should also call the method transferOwnership() at least once. You will need to use at least two instances of the Person object to represent the owners of the various vehicles that you create.

  2. In this exercise, you will create some classes for representing shapes that can be drawn on a window using Paint.java. In the classes directory, there is a generic class Shape for modeling a generic shape. You will extend this class to create one other shape: a rectangle. You will also extend your rectangle class and a create square class (since a square is just a special kind of rectangle where the width and height are the same). There is also a class Point in your classes directory that extends Shape and models a single point. You should look at both classes before you start writing your own classes.

    There is one big difference between this and previous exercises that involve Paint. Paint can only be used to draw one type of shape, a point. The following would draw a red point at (50, 50):

    Paint.setColor(Color.RED);
    Paint.drawPoint(50, 50);
    

    The methods Paint.drawRect, Paint.fillRect, etc. are not in the version of Paint.java that you will be using in this exercise. If you want to draw a rectangle or a square you will need to do it with multiple calls to Paint.drawPoint (e.g., in some kind of loop).

    The two classes that you must implement are described below:

  3. Finally, write a class MyPicture.java that builds a window and creates some shapes (a point, rectangle, and square) and draws them.

  4. In this exercise, you will write you own Vector class (with less functionality) called NewVector and your own Integer class (with less functionality) called NewInteger. Put all of your classes in a file called PrintReverse.java (you will see why it's called PrintReverse below).

    Your NewVector class should contain the following:

    Your NewInteger class should contain the following:

    Finally, write a class PrintReverse, which contains a main() method. The main() method should read in a sequence of numbers and print them out in reverse using the NewVector and NewInteger classes defined above. First, main() should create a NewVector object for storing the numbers. Then, main() should prompt for and read in numbers until the user enters a negative number. Each number should be used to create a NewInteger object, which is stored on the NewVector object. Finally, the numbers should be removed from the NewVector in reverse order and printed.

  5. For extra credit, add additional methods or constructors from the Vector class to your NewVector class. Look at the online interface for Vector (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html) to find some methods or constructors. The amount of extra credit will vary greatly with what you decide to implement. More challenging methods and/or constructors will gain you more extra credit points. In addition, the more methods and/or constructors you implement, the more credit you will receive. Put your class in a separate file called ExtraCredit.java (leave your solution from the previous solution intact). Add another class ExtraCredit, which contains a main() method that tests your new Vector functionality.

Handin

Verify that your lab10 folder contains all of the files you created or modified for this lab, then copy your entire lab10 folder to the handin directory ~mcorliss/handin/cs124/username (where username is replaced with your username).