CS30 -- Introduction to Computer Science II
Fall, 2004
Assignment 7
(updated 10/16/2004)


Exercises:
[none]

Programming Project:
This week, you will begin building a general framework for puzzles and simulated worlds.  The core of this framework will consist of the grid and the grid-object .  The grid will be the world on which puzzle pieces can be placed and/or moved, or on which critters "live" and move about.  The grid-objects will be puzzle pieces or critters.  To start this extended project, you should create the Grid class and a sample GridObject class that can be used to populate a grid instance with block-like objects; these classes should display a grid with any such blocks that it contains in their appropriate locations.

Note: your assignment this week requires more design thought than your previous assignments.  You will need to think about what specific methods to provide.  In future assignments, I will provide more detailed specifications to which you will need to adapt your code.  The design process is part of this assignment. 

[a. the Grid]  The grid should be rectangular and take width and height arguments (in terms of number of cells) to the constructor.  The cells of a grid may contain zero or more grid-objects (such as blocks).  You will, at a minimum, need to provide methods to add and remove objects from specific locations on the grid.

[b. the GridObject class]  The objects that populate a grid may have widely different characteristics and behaviors.  Thus, as you create this general GridObject class, you will want to be thinking abstractly and generally so that future assignments can create classes that extend the GridObject class.  For the purposes of this assignment, you may assume that a grid-object covers or lives within a single cell.  (However, keep in mind during your design that this will definitely not be the case for all of your future assignments.)  Your first GridObject class should provide the functionality of a block.  That is, it just sits there but has a color property.  That is, it does not move after being placed.

[c. display the Grid and its contents]  Create a graphical display of a grid and the objects (blocks in this case) that it contains.  Here, you will probably want to create your Grid as extension of Frame.  You will need to override the paint method inherited from Frame; your method should take care of painting all the grid's objects.  Using the Asteroid game as an example, your grid-objects will have their own paint methods that get called by your Grid's overridden paint method.  The "Graphics" argument is provided internally for you by the repaint() method of your Grid.   That is, repaint() calls the paint method with the appropriate graphics arg.  Then your paint method can pass that Graphics object along to your grid-objects' paint methods.  You should display the grid such that cells are visible whether or not they contain GridObjects.  In other words, you should draw grid-lines.  Note: you will need to address scaling issues such that a grid that is n rows by m columns is displayed appropriately on a window that is x by y pixels as given in the setSize method.

To run your program, provide a main method that creates a Grid 15 rows high and 30 rows wide.  Add GridObjects of assorted colors at locations [3,17], [5,7], [5,8], [6,8], and [11,24].  Keep in mind that even though you're not required to exercise multiple-object-per-cell functionality this week, your design must support this.  In general, your grade will reflect how completely you understand, design for, and implement the requirements given above.


Submission Instructions:
On your machine where you are doing your homework, create a folder called <your email name> followed by "A7".  For example, someone with email address "cjones" would create a folder called "cjonesA7".  Inside that folder, place plain text file(s) containing your answers to any exercises.  Also, place whatever Java files are necessary for your Programming Projects in the same folder.  Finally, either tar or zip the folder so that when I extract it, the folder "<emailname>A7" will be created.  (Note: the '<' and '>' should not be part of your folder name.)  If you choose to use zip instead of tar, change the extension of the resulting zip file from "<something>.zip" to "<something>.foo".  This way, our webmail will allow you to attach the file.  If I get an attachment with extension .foo, I will change it back to .zip and all should be well.