Sie sind auf Seite 1von 3

Computer

Science 152
Assignment 1
General Instructions:


There are two distinct, but related, parts in this assignment, each requiring a Java
program. You may do the assignment in pairs, but both partners must work together
on both parts (you must not simply assign one part to each partner). Your commentary
and README file must show BOTH student names.

Part 1:

The basic idea is to write a tool that captures polygons from mouse clicks and saves
them in a file. A single file can hold many polygons.

The points for each polygon will be captured from mouse clicks. As each point is
entered, draw the polyline for the points captured so far (yes a polyline not a polygon
yet). When the last point is captured, the user will press c (or C) to complete the
last line segment (joining the last point to the first point). At this time, create a polygon
(Javas Polygon class would be useful here see the API), draw the polygon, and write
the polygon to the file. (If you wish, you can extend the Polygon class to add some
features of your own, like writing to a file).

When the user types a q (or Q) to quit, close the file and terminate the program.
Youll probably need to keep track of whether or not the last polygon was completed (if
not, complete it, draw it and write it to the file before quitting).

There is no limit to the number of polygons that can be created or stored in the file.
However, assume that no single polygon has more than 100 points. Also, make the
panel 1000 x 1000 pixels, therefore restricting x and y values to the range 0999. The
format of the data file is quite simple. Heres an example:

100 100


Each line of the file represents a single
200 100
points x and y value separated by
200 200
whitespace. A -1 -1 line indicates the
100 200
end of a polygon. So the file to the left
-1
-1


contains two polygons a square and a
300 300


triangle.
350 200
400 300


-1
-1


Part 2:

The second tool reads all of the polygons from the file and draws them. Then it allows
the user to repeatedly select a polygon which the program then fills with a random
color.

This data for Part 2 should be loaded into an ArrayList of polygon objects. Again, use
Javas Polygon class (or your own subclass of Polygon).

Once the polygons are loaded and drawn, when the mouse is clicked, youll have to
search through the ArrayList to see which (if any) polygon(s) currently contain the
mouse pointer. Look at the methods in the Polygon class to see if theres one that
might be of some help. Note that It is possible that polygons overlap and therefore
more that one might contain the mouse pointer fill all that match with random
colors1.

I will post a complex data file that can be used to test your Part 2 program.

Discussion:


You can save yourself a lot of time and frustration by being intentional and methodical.
Think hard about the organization and design of your program before you start typing
anything. Build and test your code incrementally (in phases). Verify what you know to
be true at each step. For example:

Are you sure the points are captured and stored correctly?
Is the polygon built correctly?
Does the polygon drawn match the point data?
Is the file correct? Does it contain the right number of polygons, right number of
points for each polygon, proper delimiters, etc.?
Is the file being read completely?
Etc.

Test with simple polygons first. Start with a simple rectangle. Then test with a rectangle
and a triangle. Then try something more complex. Use System.out.println when you
need to, for example:

verify whether some piece of code gets executed
1

A random colour can be generated with the constructor new Color(r,g,b) where r, g and b are int values
from 0 to 255 inclusive; you could also use new Color(v) where v is an int value from 0 to 16777215
same concept, but with a single 24-bit value.

confirm the content of a variable, array, or other construct at a given point in the
program


Unlike tools that may exist in a given IDE, you always have access to System.out.println
to check things out. Use it wisely.

Instead of writing all the methods at once, write one or two at a time and figure out a
way to verify that each works. That way, you can compile and run early and often. For
the other methods, you write a stub (just enough so the code compiles and runs).

Throw-away or disposable code can sometimes be useful to help answer questions
like those above. For example, you might want to add a little for loop to confirm that
the coordinates in a polygon are accurate. This code can be removed (or commented
out) as you get further along (make sure you remove this code before passing in your
assignment).

To write text data to a file you can do something like this:


PrintWriter myFile = new PrintWriter(new FileWriter(stuff.dat));

myFile.println(Hello there);

Try to pay attention to the design and elegance of your code. That includes the usual
stuff like comments, indentation, good names for things but it also includes: logical
program structure ("divide and conquer"), using the right type of loop, using the best
data types/data structures, good use of helper methods and program efficiency.


What to submit:


Submit a single zip file containing a single BlueJ project FOR EACH PART to moodle using
the appropriate link. Each BlueJ project folder should include all source files, class files,
data files, etc. needed to run that parts program. It should also contain a README file in
which you should indicate how to run the program, note any deficiencies or special
features, etc. as well as clearly identify both students (if you choose to work in a pair.)

Assignments are due as shown in your class moodle. There are no extensions. START
ALL ASSIGNMENTS THE DAY YOU GET THEM. WORK ON THEM EVERY DAY. DEADLINES
ARE CLOSER THAN THEY APPEAR. If you fall behind, youll be doomed.

Das könnte Ihnen auch gefallen