CSCI 202 in-class lab 12

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.


  1. Have a copy of a working project with a Point1080 call and a driver class.
  2. Start by right-clicking on your project and performing a refactor copy to create a new copy of Point1080 called PairPoint1080. NetBeans should ignore your new class. Make sure that NetBeans does not call your class something silly like PairPoint10801. (It happened to me twice!)
  3. 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.
    1. Delete the private fields and constructors of Point1080Point1080.
    2. Delete the toString method of Point1080.
    3. Replace the bodies, the part between the braces, of each of the remaining four methods with a single semicolon. I m not kidding.
    4. Change the keyword class to interface.
    NetBeans should stop complaining about Point1080 though it will be unhappy about your other classes.
  4. Let’s fix up PairPoint1080.
    • Add the phrase implements Point1080 in the class heading right after PairPoint1080.
    • Add @Override before the methods of PairPoint1080. These methods are your implementation of the Point1080 interface.
    NetBeans should now accept PairPoint1080.
  5. In the driver class, change every invocation of the Point1080 “constructor” to an invocation of the PairPoint1080 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.
  6. 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 called ShortPairPoint1808.
    • Change the declarations of x and y to be short’s rather than int’s.
    • Cast some thing special, like (short), as needed.
    You should now have a second implementation of Point1080.
  7. Change the initialization of triangle[1] so that it is a shortPairPoint.

Abstract class

  1. It’s not hard to change Point1080 from an interface to an abstract class.
    • Change interface to abstract class in the class header.
    • Put the modifier abstract in front of every method specification.
    There is little difference between an interface and a class with only abstract methods.
  2. However, you must go to PairPoint1080 and ShortPairPoint1080 and change implements to extends. So far nothing useful has happened.
  3. 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 use getX and getY in these new non-abstract methods.
  4. 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()) ;
  5. If you have some free time, add a third extension class that uses a java.awt.Point to implement Point1080.