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
Point1080
call and a driver class. - Start by right-clicking on your project and performing a
refactor copy to create a new copy of
Point1080
calledPairPoint1080
. 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
Point1080
as the first step of transforming it into an interface. NetBeans will not be happy for a while.- Delete the private fields and constructors of
Point1080
Point1080
. - Delete the
toString
method 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
class
tointerface
.
Point1080
though it will be unhappy about your other classes. - Delete the private fields and constructors of
- Let’s fix up
PairPoint1080
.- Add the phrase
implements Point1080
in the class heading right afterPairPoint1080
. - Add
@Override
before the methods ofPairPoint1080
. These methods are your implementation of thePoint1080
interface.
PairPoint1080
. - Add the phrase
- In the driver class, change every invocation of the
Point1080
“constructor” to an invocation of thePairPoint1080
constructor. 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
PairPoint1080
to create a new class calledShortPairPoint1808
. - Change the declarations of
x
andy
to 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
Point1080
from an interface to an abstract class.- Change
interface
toabstract class
in the class header. - Put the modifier
abstract
in front of every method specification.
- Change
- However, you must go to
PairPoint1080
andShortPairPoint1080
and changeimplements
toextends
. 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 usegetX
andgetY
in 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.Point
to implementPoint1080
.