CISC 3130 — Lab #1

CISC 3130
Data Structures
Lab #6
Building Containers
Relationships Among Classes

How to Develop and Submit your Labs

Make sure you read the sections in the above on how to interpret CodeLab string comparison feedback

Overview

In this lab you will work on the different ways of building classes from each other: independent, composition, inheritnce, and interface inheritance. The first part mirrors what we did in the lab for the counter classes, but for a small shape hierarchy consisting of a shape interface, a rectangle class, and a square class. The second part covers inheritance of interfaces in the context of the JCF in anticipation of our exploring that framework later on.

You will also get a bit more practice programming to an interface.

Lab 6.1 — A Simple, Minimal Shape Hierarchy

The Shape Interface

Code an interface named Shape with the following behavior (method names are in ()'s): All parameters/return types are either int or boolean

The Rectangle Class

Code a Shape-implementing class named Rectangle.

ShapeApp

Code an interface-specific app, ShapeApp with the following output Assume ShapeApp invoked by RectangleApp). Assume ShapeApp has the static method demo that accepts the Shape it will be 'demo'ing, and RectangleApp call demo with a Rectangle object it creates.
--- Shape demo for Rectangle ---

a rectangle with a width of 4 and a height of 3
perimeter: 14
area: 12
isQuadrilateral: true
isEquilateral: false
Notes
  • The first line of output prints out the implementing class for which the interface-specific app is being invoked and thus demo'd (in this case a Rectangle)
    • The method getClass (of class Object and thus available to all classes) returns an object of type Class. Performing a System.out.println on this object (i.e., invoking its toString method) prints out the name of the class prefixed with the word 'class' (e.g., class Rectangle). The getName method of Class returns simply the name of the class (e.g. Rectangle)
  • The next line (a rectangle …) is printing the object (i.e., its toString result).

RectangleApp

Code an app, RectangleApp that invokes ShapeApp with a Rectangle object. Assume ShapeApp invoked by RectangleApp). Assume ShapeApp has the static method demo that accepts the Shape it will be 'demo'ing, and RectangleApp call demo with a Rectangle object it creates.

IntelliJ Notes

As mentioned all along, you should first code up your demos in IntelliJ. At this point, given the above, you have enough to execute something … RectangleApp.

Codelab Notes

These above sections will be presented as individual labs in CodeLab; you will be asked to supply one or maore of the classes/interfaces and the others will be supplied to you.

Lab 6.2-6.4 — Implementations of a Square Class

The following three labs build upon 6.1; each has you coding a different implementation of a Square class: independent, composition, and inheritance. They are independent of each other; feel free to implement in whatever order you wish.

In 6.2 below, you are asked to code a SquareApp that invokes ShapeApp (using dependency injection). This app can then be used in 6.3 and 6.4 as well.

Lab 6.2 — An Independent Square Class

Code an independent implementing class of Shape named Square. The class should not use or inherit from Rectangle.

IntelliJ Notes

Code a SquareApp app that invokes ShapeApp. Here is the output for this (i.e., independent) implementation of Square:
--- Shape demo for Square ---

an independently implemented square with a side of 3
perimeter: 12
area: 9
isQuadrilateral: true
isEquilateral: true
Notes
  • Please note that the toString result for each of the implementations is different

IntelliJ Notes

In IntelliJ since you ar eproviding all the code, you will need SquareApp app so you can execute an app using your Square class. You will use this app in the next two parts as well.

Codelab Notes

The Square and SquareApp classes will be presented as separate labs (6.2.1 and 6.2.2). Labs 6.3-6.4 will ask for the Square class only.

Lab 6.3 — A Composition-Based Square Class

Code an implementing class of Shape named Square. The class should use composition using the Rectangle class.

Here is the output for SquareApp for this implementation of Square:

--- Shape demo for Square ---

a square composed of a rectangle with a width of 3 and a height of 3
perimeter: 12
area: 9
isQuadrilateral: true
isEquilateral: true

Lab 6.4 — An Inherited Square Class

Code an implementing class of Shape named Square that inherits from Rectangle.

Here is the output for SquareApp for this implementation of Square:

--- Shape demo for Square ---

a square inherited from a rectangle with a width of 3 and a height of 3
perimeter: 12
area: 9
isQuadrilateral: true
isEquilateral: true