CS100J       Lab 08. Abstract classes     Fall 2003 

Name ___________________       Section time _____________        Section instructor _________________

This lab introduces you to the concepts of an "abstract class" and "abstract method". The topic is covered in Section 4.7 of the class text and on lesson page 4-5 of the ProgramLive CD. Your lab instructor will present the concepts to you (which are really quite simple). You will then obtain a few Java classes from the course website, load them into Java, and change one of the classes from a normal class to an abstract class. You will be asked to look at the classes and modify them a bit. You will need a sheet of paper to write information about this lab. Show it to your instructor when you have finished.

Because you probably don't have text with you, we summarize the material here.

Abstract class:

Problem 1. In some situations, we don't want a programmer to instantiate (create instances of) a class. The class is there only to provide a superclass of other classes. But there is no way to prevent programmers from instantiating the class.

Solution. Change the class to an abstract class, because abstract classes cannot be instantiated. To do this, change the first line of the class, say class C, from

       public class C {
to
      
public abstract class C {

Purpose of making a class abstract. Make a class abstract so that it cannot be instantiated.

Problem 2. In an abstract class C, we may want to place a method meth (say) in it only so that subclasses can override it. We don't want programmers to call this method; they should call only the overriding methods in the subclass. But we can't prohibit them from calling method meth.

Solution. Change meth to an abstract method, because abstract methods cannot be called (but can be overridden). To do this, change meth as shown below. There are two changes: (1) keyword abstract is inserted and (2) the method body is replaced by a semicolon.

       public void meth(...) { ...}
to
      
public abstract meth(...) ;

Purpose of making a method abstract. Make a method in an abstract class) abstracat so that it cannot be called.


Step 1. Open some files in DrJava. Start a new directory on your hard drive. Download these five files into the directory: DemoShapes.java    Shape.java    Parallelogram.java    Rhombus.java  Square.java. You can also obtain them by opening the course web page in a browser and clicking "Labs" in the lefthand column; this opens a page that has links to these files.

Open files DemoShapes and Shape in DrJava and compile the program. In the Interactions pane, execute this call:

DemoShapes.main(null);

A figure like that on the right (above) should appear. The output in the Java console is a description of seven shapes that are drawn in the window.


Step 2. Make class Shape abstract

(a) Open file DemoShapes.java and place the following statement in method paint, just before the declaration (and initialization) of variable h:

Shape s0= new Shape(5,5);

Execute the program; it should still run. On your paper, write what this statement does.

(b) Open file Shape.java and place keyword abstract just before keyword class in the class definition, so that the third line of the file looks like

public abstract class Shape {

You have made the class into an abstract class. Try executing the program again. Do you get an error message? Write down the error message and explain in a few words why it is an error.

Now delete the statement that you placed in file DemoShapes.java in part (a) and run the program again. You should no longer have an error message.


Step 3. Make method drawShape of class Shape abstract

In file Shape.java, change method drawShape to:

public abstract void drawShape(Graphics g);

Note that the body {} is replaced by a semicolon. You have made this method into an abstract method. Execute the program; it should still execute.

Open file Parallelogram.java and comment out method drawShape (put /* before the method and */ after the method). Try to execute the program. Do you get error messages?

Write on your paper the error message that deals with class Parallelogram. Write a few words explaining what the error is.

Remove the comment symbols, so that drawShapes is again defined in Parallelogram. Execute the program again just to be sure that you removed them correctly.


Step 4. Add arms

The shape that appears when the program is executed looks almost like a person. You will give it arms. All the changes you will make will be in class DemoShapes. Read through method paint of DemoShapes.

First, comment out the code that produces the two black lines (in DemoShapes). Hint: look for where the color is set to black.

Each arm is a green rectangle that is 60 pixels long and 20 pixels high. Its leaning factor (the third parameter of the Parallelogram constructor) is 0, which means that it is a rectangle. The leaning factor is defined on Lesson page 4.4 of the ProgramLive CD, but you really don't have to read about it. Later, when you get the program going with leaning factor 0, you can try a different leaning factor, say 15, and see what it looks like.

The arms should be attached at the top right and top left of the square that makes up the body. The tops of the arms should be parallel to the top line of the square.

In writing the code that draws these rectangles, use the variables that are defined at the top of method paint. Also, use variables to contain all the constants that you need, as we did in method paint. You may have to move the whole figure to the right (by changing the value of variable x) so that you can see the whole picture. You must use class Parallelogram; you may not use method drawRect in class Graphics.

Hint: to figure out the coordinates for the arms, look at the positioning of the green square.

When you are finished, write on your paper the sequence of statements that you added to method paint.