Yep, we are still continuing Lab 9, but we are going to get abstract this week.
Interfaces and Abstract Classes
You are going to have to do a lot of cut-and-paste this week to tranform from inheritance to interface to abstraction. Try to be very careful about keeping your braces balanced. Also, there will be long periods when NetBeans is angry with you.
Getting reading
Either copy the Lab 10 project to a new Lab 11 project or unZIP the following ZIPped project into your home directory. I’d prefer that we all start with the same project. Compile before going on.
We will create an interface and than an abstract class today.
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.
Interface
- Have a copy of a working project with a
Point1080call and a driver class. - Start by right-clicking on your project and performing a
refactor copy to create a new copy of
Point1080calledPairPoint1080. NetBeans should ignore your new class. Make sure that NetBeans does not call your class something silly likePairPoint10801. (It happened to me twice!) - Now we are going to do some massive clearing of
Point1080as the first step of transforming it into an interface. NetBeans will not be happy for a while.- Delete the private fields and constructors of
Point1080Point1080. - Delete the
toStringmethod ofPoint1080. - Replace the bodies, the part between the braces, of each of the remaining four methods with a single semicolon. I m not kidding.
- Change the keyword
classtointerface.
Point1080though it will be unhappy about your other classes. - Delete the private fields and constructors of
- Let’s fix up
PairPoint1080.- Add the phrase
implements Point1080in the class heading right afterPairPoint1080. - Add
@Overridebefore the methods ofPairPoint1080. These methods are your implementation of thePoint1080interface.
PairPoint1080. - Add the phrase
- In the driver class, change every invocation of the
Point1080“constructor” to an invocation of thePairPoint1080constructor. Do not change any of your variable declarations and do not change the array constructor. Run your program and think about what you have done. - Let’s do something silly to save 16 bits in each point.
-
Start by doing a refractor copy of
PairPoint1080to create a new class calledShortPairPoint1808. - Change the declarations of
xandyto beshort’s rather thanint’s. -
Cast some thing special, like
(short), as needed.
Point1080. -
Start by doing a refractor copy of
- Change the initialization of
triangle[1]so that it is ashortPairPoint.
Abstract class
- It’s not hard to change
Point1080from an interface to an abstract class.- Change
interfacetoabstract classin the class header. - Put the modifier
abstractin front of every method specification.
- Change
- However, you must go to
PairPoint1080andShortPairPoint1080and changeimplementstoextends. So far nothing useful has happened. - Return to your new abstract class and add the following
methods to computer the angle and distance to the point.
public double angle() { return Math.sin((double)getX()/getY()) ; } public double distance() { int x = getX() ; int y = getY() ; return Math.sqrt(x*x + y*y) ; }Think about why we had to usegetXandgetYin these new non-abstract methods. -
Add some code to your driver class to take advantage of these
new methods.
System.out.printf("Dist %6.1f, Angl %6.4f\n", p.distance(), p.angle()) ; -
If you have some free time, add a third extension class that uses
a
java.awt.Pointto implementPoint1080.