CSc 331 - Spring 2018
Lab Exercise #3

Preparation

Additional Information / Guidelines

Exercise 1:

Plan, code and "test" a Shape class hierachy in which Rectangle and Circle are derived from PositionedShape, which is derived from Shape. All Shape's have a colour. All PositionedShapes have a center Point. (Yes Point is another class.)

The Point class will have the following methods:

Circle and Rectangle will have an draw method - that just returns a brief description of the object - as shown below. For example, the Rectangle draw method might return
A black rectangle at (1, 2). Size 4 by 2

Each class has constructors as demonstrated in the main below. Note that all "default" shapes are black with unit dimensions - centered at (0, 0).

For the purposes of this Java programming exercise, the following main method ...


    public static void main(String[] args) {
        List<Shape> pic1 = new ArrayList<>();

        pic1.add(new Circle());
        pic1.add(new Circle("red", 0, 1, 2));
        pic1.add(new Rectangle());
        pic1.add(new Rectangle("blue", 2, 0, 3, 5));

        for (Shape s: pic1) {
            s.draw();
        }
    }

should produce output very much like ...


A black circle at (0, 0). Radius 1
A red circle at (0, 1). Radius 2
A black rectangle at (0, 0). Size 1 by 1
A blue rectangle at (2, 0). Size 3 by 5

Exercise 2:

Complete the code in ReadShapes.java - so that it supports keyboard input for an arbitrary number of Circle's and Rectangle's, stores them in a List and then outputs each of them together with their distance from the Point (1, 0) - for example - as illustrated below.

Assume that all keyboard input is correct. (This is just test code for the Shape hierachy.)


$ java ReadShapes

Your options are:
C for circle
R for Rectangle
q to quit
--> c
Colour? red
x coord for center? 0
y coord for center? 1
Radius? 2
Center is (0, 1)
.
.
.
--> q

The picture contains ...
1. A red circle at (0, 1). Radius 2
   Distance to (1, 0) is 1.41
2. A green circle at (4, 4). Radius 1
   Distance to (1, 0) is 5.00
3. A blue rectangle at (2, 0). Size 5 by 3
   Distance to (1, 0) is 1.00

Exercise 3:

Add a Java interface Measurable. Measurable has 1 method - double area(). The area method will return the area of this object. Add the draw method to the appropriate classes. Add to ReadShapes.java as appropriate so that the output now looks like ...


$ java ReadShapes
.
.
.
The picture contains ...
1. A red circle at (0, 1). Radius 2
   Distance to (1, 0) is 1.41
   Area is 12.57
...

Exercise 4:

Finally add to your code so that your List of Circle's and Rectangle's is output in its original order - and in sorted-by-area order. See out4.txt in the directory ~hohman/331/s18/labs/lab3

Submit:

This assignment is due at the beginning of the Tuesday lab class next week (3:30 p.m.) - and the submit program will enforce this