CISC 3115
Introduction to Modern Programming Techniques
Lab #4
Interfaces

How to Develop and Submit your Labs

Lab 4 — A Shape Interface-and-Implementing-Classes Hierarchy

Overview

This lab has you defining a hierarchy with an interface at the top and implementing classes below.

		Shape
		/   \
	Circle	Triangle
	
You will also be creating demo/app clases to 'demo' the facilities of the classes.

Lab 4.1 — Shape

Define a Shape interface with the following: I am supplying you with the code (ShapeTest.java). I am using to test your interface (all supplied code can be accessed via the link at the bottom of the page). You might find it informative to look at the testing and other supplementary code; it may also give you ideas for the code you have to write.

Lab 4.2 — ShapeDemo

Define a ShapeDemo class that contains a static method named demo accepting a parameter that is a reference variable of type Shape. This method should 'demo' those Shape methods implemented by the implementing class whose object was passed to the Shape parameter. (This is like the other 'Demo' classes with demo methods I presented in class.)

I have supplied you with the class file of a Shape-implementing class named Rectangle whose objects are thus eligible to be passed to ShapeDemo.demo, as well as the source and class files of a corresponding RactangleApp class. This class creates an object of type Rectangle. It then passes it to ShapeDemo.demo which invokes the various methods of the Shape interface on the passed object. When control returns from demo to RectangleApp's main method, the methods specific to Rectangle; i.e., those methods in Rectangle that are not in the Shape interface are invoked showing their operation. Here are the files and here is is the output of RectangleApp:

stdout
=== Rectangle App
--- Shape demo for a Rectangle ---
A rectangle with sides 3, 4
Perimeter:  14.00
Area     :  12.00
... Rectangle-specific demo
Square: false

--- Shape demo for a Rectangle ---
A rectangle with sides 1, 1
Perimeter:   4.00
Area     :   1.00
... Rectangle-specific demo
Square: true

--- Shape demo for a Rectangle ---
A rectangle with sides 4, 7
Perimeter:  22.00
Area     :  28.00
... Rectangle-specific demo
Square: false

the portion of the output generated by ShapeDemo (applied to a Rectangle) is in green. You can use RectangleApp as a guide to writing your own xxxApp classes (below).

Lab 4.3 — Circle & CircleApp

The next couple of labs consists of a Shape-implementing clas and its xxxApp class.

Lab 4.3.1 — Circle

Define a Circle class that implements Shape and also contains the following additional (i.e., Circle-specific) methods: Circle (and the rest of the classes that follow) should also provide:

Lab 4.3.2 — CircleApp

Define a CircleApp class that reads in Circle objects from the file circles.data and illustrates the facilities of the Circle class. It does this by first invoking ShapeDemo.demo and then 'demo'ing' the Circle-specific methods.
circles.data
5
0
2
stdout
=== Circle App
--- Shape demo for a Circle ---
A circle with diameter 5.0
Perimeter:  15.71
Area     :  19.63
--- Circle-specific demo
radius: 2.5

--- Shape demo for a Circle ---
A circle with diameter 0.0
Perimeter:   0.00
Area     :   0.00
--- Circle-specific demo
radius: 0.0

--- Shape demo for a Circle ---
A circle with diameter 2.0
Perimeter:   6.28
Area     :   3.14
--- Circle-specific demo
radius: 1.0
As mentioned above, CircleApp is the RectangleApp analog.

Lab 4.4 — Triangle & TriangleApp

Lab 4.4.1 — Triangle

This is the same as Lab 4.3 but for a Triangle class. Define a Triangle class that implements Shape and also contains the following additional (i.e., Triangle-specific) methods: Again, Triangle should also provide:

Lab 4.4.2 — TriangleApp

Define a TriangleApp class that reads in Triangle objects from the file triangles.data and illustrates the facilities of the Triangle class. It does this by first invoking ShapeDemo.demo and then 'demo'ing' the Triangle-specific methods.
3 4 5
1 1 2
3 3 3
stdout
=== Triangle App
--- Shape demo for a Triangle ---
A triangle with sides 3, 4, 5
Perimeter:  12.00
Area     :   6.00
--- Triangle-specific demo
Equilateral: false
Isoceles: false

--- Shape demo for a Triangle ---
A triangle with sides 6, 6, 3
Perimeter:  15.00
Area     :   8.71
--- Triangle-specific demo
Equilateral: false
Isoceles: true

--- Shape demo for a Triangle ---
A triangle with sides 3, 3, 3
Perimeter:   9.00
Area     :   3.90
--- Triangle-specific demo
Equilateral: true
Isoceles: true

Lab 4.5 — ShapesApp

Define a ShapesApp class that: Here is a sample run:
shapes.data
C 12
T 3 4 5
T 10 15 4
T 1 2 3
C 2
C 9
T 3 4 5
stdout
--- Shape demo for a Circle ---
A circle with diameter 12.0
Perimeter:  37.70
Area     : 113.10
--- Shape demo for a Triangle ---
A triangle with sides 3, 4, 5
Perimeter:  12.00
Area     :   6.00
--- Shape demo for a Triangle ---
A triangle with sides 10, 15, 4
Perimeter:  29.00
Area     :    NaN
--- Shape demo for a Triangle ---
A triangle with sides 1, 2, 3
Perimeter:   6.00
Area     :   2.45
--- Shape demo for a Circle ---
A circle with diameter 2.0
Perimeter:   6.28
Area     :   3.14
--- Shape demo for a Circle ---
A circle with diameter 9.0
Perimeter:  28.27
Area     :  63.62
--- Shape demo for a Triangle ---
A triangle with sides 3, 4, 5
Perimeter:  12.00
Area     :   6.00

A circle with diameter 12.0 (Perimeter:  37.70)
A triangle with sides 3, 4, 5 (Perimeter:  12.00)
A triangle with sides 10, 15, 4 (Perimeter:  29.00)
A triangle with sides 1, 2, 3 (Perimeter:   6.00)
A circle with diameter 2.0 (Perimeter:   6.28)
A circle with diameter 9.0 (Perimeter:  28.27)
A triangle with sides 3, 4, 5 (Perimeter:  12.00)

A triangle with sides 1, 2, 3 (Perimeter:   6.00)
A circle with diameter 2.0 (Perimeter:   6.28)
A triangle with sides 3, 4, 5 (Perimeter:  12.00)
A triangle with sides 3, 4, 5 (Perimeter:  12.00)
A circle with diameter 9.0 (Perimeter:  28.27)
A triangle with sides 10, 15, 4 (Perimeter:  29.00)
A circle with diameter 12.0 (Perimeter:  37.70)

Submitting to Codelab