# CS 231 Lab 7: Sub-classes and inheritance (circle overlap)

## Introduction

One important broad point lesson to understand about computers is that they do not have the human capability to view situations as a gestalt (or unified whole) and to use this view to come up with certain kinds of information "all at once" (or seemingly so). For the computer to display such behavior, we must program it in terms of much smaller steps, steps which seem especially awkward or stilted to human intuition.

Consider for example that we draw a set of circles on a page and then ask someone whether the circles overlap, and which ones do: if the page is not huge (i.e., if we can see it all at once), the answer seems immediate to us. But to program a computer to do this, we must have it consider each possible pair of circles in turn, and have it "compute" whether or not the circles overlap. In essence, the computer does not "see" the circles so much as it knows their coordinates and sizes, and so must use this information to recover in many steps what is so immediate to us. (Of course, if we give the computer "eyes" by connecting a camera to it, we can imagine that it will "see" the answer as well as we do, but in fact this will require at least as much programming to achieve.) To be fair, of course, we are not ourselves conscious of the considerable low-level neural and physiological "work" that is done when we intuitively grasp such knowledge, but that is a topic for another course (if you are interested in such issues, consider courses in perception offered through the Psychology Department).

## Overall plan

Your job, then, is to write an interactive graphics program which draws circles: the user will enter commands to draw a new circle of a specified color at a specified position on the screen. Your program will then draw the circle and compare it to other circles drawn previously. Any two circles which overlap should be modified to have a red color, and the whole scene should be re-drawn.

Your overall main method will therefore consist of a loop which:

1. reads in a "command" string via a `Scanner`; each command should consist of three numbers (integers) and a color code (you can use the initial letters for orange, yellow, green, blue and purple, saving red for the overlapping circles). The three numbers represent the center coordinates and radius of the new circle. You should set up the Scanner itself inside the main method, but before the loop;

2. You should then add the newly-requested circle to an array of circles in the scene (you can assume there will never be more than a dozen circles); remember to keep track (with an integer) of how many circles you currently have, as opposed to the total capacity of the array;

3. After adding the new circle to the array, you should check every pair of circles to see if they overlap: if they do, set the color of each circle to red. To check every pair of circles, use a nested loop, similar to the one you used for the multi-verse song lab: note that you don't need to cmpare circle A to B and circle B to A, just check each pair once (you also don't need to, or want to, check to see if circles overlap themselves).

4. Once you have checked and modified (if necessary) the circles, you should re-draw all the circles in the array (up to the actual fill point, not the total capacity). Then go back around the loop and do it all again. (If you want to, you could allow some special command, such as "Q", to act as a quitting command.)

## Class structure

In addition to your `Main` class with it's `main()` method, you are required to structure your circles in terms of a set of classes and sub-classes. More specifically, you should have three related classes:

• a `Point` class which contains just a pair of integers for the coordinates of the point, a constructor method and a method to determine the distance between one point (on which the method is called) and another, second point (passed as a parameter to the method). The `distance` method should return a Java `double` value, and should be computed according to the usual formula (use the library function `Math.sqrt(...)` to find the square root of a number).

• a `Circle` class which adds an (integer) radius variable and a method to determine overlap between circles. The `overlap(...)` method will be called on one Circle and take another as a parameter; it will return a boolean value (true or false). You can compute whether or not two circles overlap by comparing the distance between their center points to the sum of their radii (remember that you can still call a method on an object even if its class is a sub-class of the one that actually holds the method definition).

• a `ColoredCircle` class which adds a color variable (remember to import `java.awt.*`) and a method for painting the circle in a `Graphics` object and a method for setting its color. Once again, this class will extend the last one (`Circle`), so you will still be able to call the `distance(...)` and `overlap(...)` methods on it.

Given these three classes and the "architectural" sketch from above, you should be able to put together a program which will satisfy this specification and nicely illustrate some principles of class- and inheritance-based programming.