In many way this is yet another continuation of Lab 9. We are going to do is to look at other ways of making a point.
Inheritance
It would be best to start by copying your old Point1080
project in NetBeans.
Be sure to compile it before you go on.
If you don’t have a working Point1080, you can copy
my Point1080
along with its driver.
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.
- Have a copy of a working project for lab 9 or lab 10.
- Override the
toStringmethod inPoint1080with something that prints nicely. - Modify your driver routine to use the the overriden
toStringmethod. - Create a new class
NamedPoint1080that does nothing more thanextendsPoint1080. - Return to your driver method. Change one of the
invocations of the default
Point1080constructor to an invocation of the defaultNamedPoint1080constructor. 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. -
Add a constructor to
NamedPoint1080that receives two integer arguments and passes them on toPoint1080. Your new constructor should contain nothing more than a call tosuper. It also should contain athrows IllegalArgumentExecptionin its header since thesupercan do that. -
Go to your driver routine and change all the
invocations of the
Point1080to use theNamedPoint1080constructor. You’ll probably discover that you need to add a defaultNamedPoint1080constructor to get your program to run. That’s a funny thing about Java. -
Return to the
NamedPoint1080class and add aprivateStringfield calledname. Set up the appropriate accessor and mutator methods for this new field. (I strongly suggest you call themgetNameandsetName.) -
Try to use your new
setNamemethod to give a point a name. This will not work until you declare your variables to be of typeNamedPoint1080. Think a bit about that one. -
Now add an
NamedPoint1080constructor that takes three arguments, aStringand two integers. Modify your driver to use this new constructor. -
Add a
toStringmethod toNamedPoint1080. See if your program is now prints a bit friendlier. -
Finally, declare
lastTestto be of typePoint1080but assign it a value using the defaultNamedPoint1080constructor. Print this value with a simpleSystem.out.println. Do you get the name or unnamed print?