M/F 2:30-3:20   
in G01 Gates Hall

CS 1130: Transition to OO Programming

Spring 2016

Self-Help Exercise

Drawing Objects of Subclasses

This self-exercise is to draw objects of subclasses. It is important that you know how to draw an instance of a class yourself, following our conventions. Only then can you fully understand how classes and instances of classes work in Java. The three steps of this exercise are given below. After the two steps, we give some notes on mistakes that students often make when doing this exercise. Please read them carefully to make sure that you did not make these mistakes. Here are the answers, in a pdf file.


Exercise

Step 1. Draw an object.

Consider the following classes:

public class Ex {
    
public static final int ZERO= 0;
    
private int h;

    public Ex(int ph) {
        h= ph;
    }

    public int getH() {
    
return h;
    }

    public String toString() { ... }

    public static int what(int x) { ... }
}

public class Sub extends Ex {
    private int k;

    public Sub(int pk) {
        k= pk;
    }

    public String toString() { ... }

}

To the right is an instance of class Sub. Below the horizontal line are all the instance variables and methods that are declared in Sub; above the line are all the instance variables and methods that are declared in superclass Ex. Variables h and k have been given arbitrary values.

On your paper, draw an instance of class Ex and another instance of class Sub.


Step 2. More subclasses

Consider also these two subclasses of class Sub:

public class SubSub1 extends Sub {
    public SubSub1() {
        super(5);
    }

    public int hPlus1() {
    return getH()+1;
    }

    public String toString() { ... }
    }

public class SubSub2 extends Sub {
    private int p;
}

Draw an intance of class SubSub1 and an instance of class SubSub2.


Notes on this exercise

One semester, when we gave a similar homework, some students did miserably. We explained what they did wrong and gave them another chance to learn. So that you don't have this problem, here are notes that we gave to the students who did things wrong. After completing this assignment, read through these notes and see whether you made the same mistakes. If so, correct them.

Note 1

You did not follow directions at all. We do not see, separately, an instance of Ex, an instance of Sub, an instance of SubSub1, etc. You may have put them all together in one picture, but that is not what we asked for and it shows no understanding of drawing folders.

Note 2

In a folder like one for SubSub1, you placed all the components together in one place. You did not put those for Ex on top, then those for Sub next, and finally those for SubSub1 at the bottom.

Note 3

You did not draw field p of SubSub2 as a variable —either with a line after it or a box after it.

Note 4

You left off the part for superclass Ex and/or Sub in your diagrams. You did not follow directions.

Note 5

You did not draw variables correctly. They should be drawn with the name of the variable followed either by an underline with the value on it or by a box with the value in it.

Note 6

Method what and field Zero in class Ex are static. Therefore, they should not be drawn in each folder of class Ex —they belong separately in the file drawer for Ex.

Note 7

In drawing a folder for SubSub2, you put in it a place for SubSub1 components. That is not correct. SubSub2 extends Sub, not SubSub1.