CISC 3115
Introduction to Modern Programming Techniques
Lab #5
Inheritance

How to Develop and Submit your Labs

Overview

This lab provides practice in create subclasses with somewhat intuitive behavior. It also has the potential for confusion (whose resolution should be helpful) in that it appears to do something you don't think you know how to do, i.e., implement and display a window. The exercises in the lab call for the creation of classes forming a hierarchy of 'window' objects. While these classes will not cause any graphic objects to be displayed, they exhibit some of the behavior of a window.

Important Note: This assignment does NOT use graphics. Printing the window involves printing to System.out a area representing the 'window', e.g. a 7 x 10 window would print 7 lines (rows) of 10 characters (columns)

The Class Hierarchy

Here is the class hierarchy for Labs 5.1-4:
Window
	ColoredWindow
	BorderedWindow
and the interface relationship between Window and GUIComponent
GUIComponent
	Window
i.e., Window implements GUIComponent. Each class or interface in this hierarchy has a specific and distinct purpose: i.e., an (abstract) superclass named Window and three subclasses extending the behavior of the superclass.

Testing at Each Exercise

Each of the exercises in this lab call for the creation of a class or interface, and I will provide applications classes in the first several to give you an idea what to do. These classes will also be used in the Codelab implementation of these exercises. In addition, in the first two exercises, you are being asked to code an interface (5.1) or abstract class (5.2), and if you recall, objectsa of interface or abstract class type can't be created. As such, I provide, in each case, a minimal implementation/concrete subclass, which again you may choose to use in your own testing of those exercises.

Lab 5.1 — A GUI Component Interface (GUIComponent)

Code an interface named GUIComponent specifying three methods: I am providing both an application class (that tests the interface), as well as a minimal class named MinGUIComponent, that implements this interface. This minimal GUI component displays nothing when visibility is off and a simply fixed size box when it's on. Sample Test Run

The output of my application class using MinGUIComponent is:

Creating a MinGUIComponent and displaying -- initial state -- visible=false

Setting visible to true and displaying
...
. .
...

Submitting to CodeLab

This lab is primarily concerned with subclassing and abstract class, but this was an opportunity to get an interface in. It would be more compelling if there was a second class (e.g., a Button) that implemented the interface, but time is pressing.

While the notion of 'display' is indeed amorphous, and thus contained as nothing more than a method specification in an interface, the question arises as to making the visibility a specification in an interface, as opposed to placing it — together with a instance variable in an abstract class. The motivation for the latter is that it would seem that visibility would always be handled by an instance variable (typically of type boolean), so why defer this to an implementing class? While this is a reasonable argument,

Lab 5.2 — A Window Class (Window)

Code the following Window class:

The (abstract) Window class implements the GUIComponent interface, and represents a window with a height and width, as well as the status of the window: normal (also known as 'restored') or minimized. In minimized mode, the window's toString result is displayed in a 1-line collapsed fashion (reminiscent of minimizing a window to the task bar):

[ a 7 x 5 minimal window (minimized) ]
while displaying the window in 'normal' (or 'restored') depends on the type of window, and thus is deferred to the subclasses (by making it an abstract method ofWindow). Specification: I've provided an application class, WindowApp, for testing your class, and, as discussed above, a MinWindow class that provides a minimal implementation of a displayNormal method. This minimal window class has a self-describing toString that also contains the width and height.

Sample Test Run

For example if the file window.data contains:

7 12
the provided WindowApp application class (using the MinWindow) class produces the following output:
Creating a 5x10 minimal window
Displaying:
(Nothing to see here)

Setting visible to true and displaying:
.......................
:a 5x10 minimal window:
.......................

Minimizing and displaying:
[a 5x10 minimal window (minimized)]

Restoring and displaying:
.......................
:a 5x10 minimal window:
.......................

Resizing to 10x5 and displaying:
.......................
:a 10x5 minimal window:
.......................

Minimizing and displaying:
[a 10x5 minimal window (minimized)]

Creating a window from file data and displaying
(Nothing to see here)

Setting visible to true and displaying:
.......................
:a 7x12 minimal window:
.......................

Minimizing and displaying:
[a 7x12 minimal window (minimized)]

Submitting to CodeLab

Lab 5.3 — A Colored Window Class (ColoredWindow)

Code the following ColoredWindow subclass of Window: Specification: I've provided an application class, ColoredWindowApp, for testing your class.

Sample Test Run

If the file window.data contains:

4 9 #
my application class produces the following output:
Creating a 5x10 colored window
Displaying:
(Nothing to see here)

Setting visible to true and displaying:
+++++
+++++
+++++
+++++
+++++
+++++
+++++
+++++
+++++
+++++

Minimizing and displaying:
[a 5x10 window with background color '+' (minimized)]

Restoring and displaying:
+++++
+++++
+++++
+++++
+++++
+++++
+++++
+++++
+++++
+++++

Changing color to '%' and displaying:
%%%%%
%%%%%
%%%%%
%%%%%
%%%%%
%%%%%
%%%%%
%%%%%
%%%%%
%%%%%

Resizing to 10x5 and displaying:
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%

Minimizing and displaying:
[a 10x5 window with background color '%' (minimized)]

Creating a window from file data and displaying
(Nothing to see here)

Setting visible to true and displaying:
####
####
####
####
####
####
####
####
####

Minimizing and displaying:
[a 4x9 window with background color '#' (minimized)]

Submitting to CodeLab

Lab 5.4 — A Bordered Window Class (BorderedWindow)

Code the following BorderedWindow subclass of Window: Specification:

Sample Test Run

For example if the file window.data contains:

8 3
an application program might produce the following output:
Creating a 5x10 bordered window
Displaying:
(Nothing to see here)

Setting visible to true and displaying:
+-----+
|     |
|     |
|     |
|     |
|     |
|     |
|     |
|     |
|     |
|     |
+-----+

Minimizing and displaying:
[a 5x10 window with a border (minimized)]

Restoring and displaying:
+-----+
|     |
|     |
|     |
|     |
|     |
|     |
|     |
|     |
|     |
|     |
+-----+

Resizing to 10x5 and displaying:
+----------+
|          |
|          |
|          |
|          |
|          |
+----------+

Minimizing and displaying:
[a 10x5 window with a border (minimized)]

Creating a window from file data and displaying
(Nothing to see here)

Setting visible to true and displaying:
+--------+
|        |
|        |
|        |
+--------+

Minimizing and displaying:
[a 8x3 window with a border (minimized)]

Submitting to CodeLab

Lab 5.5 — Playing Polymorphically with Windows (WindowApp)

Using the classes you've developed in Labs 5.1-4, write an app that loads window data from a file, creates the proper window object, loading them up into an array of type Window, and then traversing the array performing Window-level actions (displaying, settingvisibility, resizing).

The Details

The window.data file contains window data for the various (concrete) Window subclasses (ColoredWindow and BorderedWindow). THe problem is detecting what kind of window data is coming up next — the read method of ColoredWindowtakes a width, height, and background color character, while the read method of BorderedWindow takes only a width and height. There are several ways of resolving this; hat we will do is prefix the data for a window with an indicator (or sentinel) that indicates the type of window whose data follows — a C for ColoredWindow, and a B for BorderedWindow.i You can seen a example of this in the sample run below. When your application reads in the objects from the file, it (the application) reads in the indicator, and then —based on its value (C or B) — calls the proper read method and loads the returned (Window) object into the array. Your application should: You are not getting any code from me in this exercise; the interface and classes are those from 5.1-4.

Sample Test Run

For example if the file window.data contains:

C 4 9 O
C 3 4 ;
B 4 6
C 2 3 +
B 5 7
your application should produce:
Creating window data from file data, loading it into an array, and displaying
--- Window 0: a 4x9 window with background color 'O'
(Nothing to see here)

--- Window 1: a 3x4 window with background color ';'
(Nothing to see here)

--- Window 2: a 4x6 window with a border
(Nothing to see here)

--- Window 3: a 2x3 window with background color '+'
(Nothing to see here)

--- Window 4: a 5x7 window with a border
(Nothing to see here)


Turning on visibility
--- Window 0: a 4x9 window with background color 'O'
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO

--- Window 1: a 3x4 window with background color ';'
;;;
;;;
;;;
;;;

--- Window 2: a 4x6 window with a border
+----+
|    |
|    |
|    |
|    |
|    |
|    |
+----+

--- Window 3: a 2x3 window with background color '+'
++
++
++

--- Window 4: a 5x7 window with a border
+-----+
|     |
|     |
|     |
|     |
|     |
|     |
|     |
+-----+


Resizing (flipping width and height) and displaying
--- Window 0: a 9x4 window with background color 'O'
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO

--- Window 1: a 4x3 window with background color ';'
;;;;
;;;;
;;;;

--- Window 2: a 6x4 window with a border
+------+
|      |
|      |
|      |
|      |
+------+

--- Window 3: a 3x2 window with background color '+'
+++
+++

--- Window 4: a 7x5 window with a border
+-------+
|       |
|       |
|       |
|       |
|       |
+-------+


Minimizing and Displaying
--- Window 0: a 9x4 window with background color 'O'
[a 9x4 window with background color 'O' (minimized)]

--- Window 1: a 4x3 window with background color ';'
[a 4x3 window with background color ';' (minimized)]

--- Window 2: a 6x4 window with a border
[a 6x4 window with a border (minimized)]

--- Window 3: a 3x2 window with background color '+'
[a 3x2 window with background color '+' (minimized)]

--- Window 4: a 7x5 window with a border
[a 7x5 window with a border (minimized)]

Submitting to CodeLab

Files used in this Lecture