CSCI 202 inheritance lab

Getting started

Create a NetBeans Java application project with a main class called Driver1080 and a second class called Point1080. Both classes need to be in the edu.unca.cs.csc202 package.

The tasks

The chores in this lab are going to be directed by the instructor. You can try to get ahead, but it might be best just to help the person beside you get through the next point.

  1. Override the toString method in Point1080 with something that prints nicely.
  2. Modify your driver routine to use the the overriden toString method.
  3. Create a new class NamedPoint1080 that does nothing more than extends Point1080.
  4. Return to your driver method. Change one of the invocations of the default Point1080 constructor to an invocation of the default NamedPoint1080 constructor. If you don’t have a default constructor, add one. Do not change any declarations. Your program should still run, but some objects may be printed a bit differently.
  5. Add a constructor to NamedPoint1080 that receives two integer arguments and passes them on to Point1080. Your new constructor should contain nothing more than a call to super. It also should contain a throws IllegalArgumentExecption in its header since the super can do that.
  6. Go to your driver routine and change all the invocations of the Point1080 to use the NamedPoint1080 constructor. You’ll probably discover that you need to add a default NamedPoint1080 constructor to get your program to run. That’s a funny thing about Java.
  7. Return to the NamedPoint1080 class and add a private String field called name. Set up the appropriate accessor and mutator methods for this new field. (I strongly suggest you call them getName and setName.)
  8. Try to use your new setName method to give a point a name. This will not work until you declare your variables to be of type NamedPoint1080. Think a bit about that one.
  9. Now add an NamedPoint1080 constructor that takes three arguments, a String and two integers. Modify your driver to use this new constructor.
  10. Add a toString method to NamedPoint1080. See if your program is now prints a bit friendlier.
  11. Finally, declare lastTest to be of type Point1080 but assign it a value using the default NamedPoint1080 constructor. Print this value with a simple System.out.println. Do you get the name or unnamed print?