CMSC 110 (Introduction to Computing)

Spring 2011

Assignment#5

Due before start of class on Thursday, November 3, 2011



Task:
Design an underwater creature. The creature should be designed so that it can be instantiated as an object. You should be able to create instances of the creature with varying sizes through a single float passed into the constructor. In addition to the constructor, make sure that the class includes methods called display(), move(), getX(), and getY(). 


Step 1:  Cut and paste the following code into a file called Assignment5_<First Initial Last Name>.pde (e.g., my sketch file would be called "Assignment5_EEaton.pde").  Save the sketch immediately.  This is the main file for running assignment 5.  The driver code listed below should be used VERBATIM to run your class, with ONE exception:  in the setup() function, you will need to replace "EEatonObj" with the name of your object, which you will create in step 2.  This is the ONLY change you should make to the code below.


/***************************************
DO NOT MODIFY FROM HERE ...
****************************************/

/** The array of objects */
AnimatedObject[] objs = new AnimatedObject[5];

/** Constant for the sandHeight */
int SAND_HEIGHT = 40;


/** Setup the sketch */
void setup() {
size(800,600);
smooth();
// initialize all the objects
for (int i=0; i < objs.length; i++) {
objs[i] = new EEatonObj(random(30,50)); // the one change you'll need to make is changing this line to call your constructor
}
}


/** The main draw loop */
void draw() {

// draw the tank background
background(50,50,255);

// draw the sandy bottom of the tank
fill(168,168,50);
rect(0,height-SAND_HEIGHT, width, SAND_HEIGHT);

// draw the enhanced tank background, if necessary
drawTankBackground();

// create the vector of all object's locations
PVector[] allObjectsLocations = new PVector[objs.length];
for (int i=0; i<objs.length; i++) {
allObjectsLocations[i] = new PVector(objs[i].getX(), objs[i].getY());
}

// draw and animate each of the objects
for (int i=0; i<objs.length; i++) {
objs[i].display();
objs[i].move(allObjectsLocations);
}
}



/** An abstract class for animated objects */
abstract class AnimatedObject {

/** Constructor
* Note that your constructor should accept a single float specifying the size
*/

/** Displays the object */
abstract void display();

/** Advances the object's animation by one frame
* Note: Implement only one of the move() functions, but NOT both.
*/
void move() { }

/** Advances the object's animation by one frame.
* Note: Implement only one of the move() functions, but NOT both.
* @param allObjectsLocations an array of the locations of ALL objects in the environment
*/
void move(PVector[] allObjectsLocations) { move(); }

/** Getter for the object's x position
* @return the object's x position (as measured from the center of the object)
*/
abstract int getX();

/** Getter for the object's y position
* @return the object's y position (as measured from the center of the object)
*/
abstract int getY();
}


/**************************************
... TO HERE
***************************************/



Step 2:  Create a file called <First Initial Last Name>Obj.pde (e.g., my file would be called EEatonObj.pde) then cut and paste the following code into the file.  This code must be placed in a different file from the one you created above, and this file should be saved to the same sketch directory as the one you created in step 1.  For ease of running the sketch, keep both files open as tabs in Processing.  It is ESSENTIAL that you name your object and file appropriately, since I will later combine all creatures together into a single aquarium.   

Complete the assignment by implementing your sea creature in this file.

class <First Initial Last Name>Obj extends AnimatedObject { // keep this line as is, except for renaming the class

/** Constructor */
<First Initial Last Name>Obj(float size) { ... }

void display() {
... }

void move() {
... }

int getX() { ... }

int getY() { ... }
}



/** If you want to enhance the tank background
* to showcase your own project, you can put
* drawing code in this function, which is called
* near the start of the draw() function below.
*/
void drawTankBackground() { }



You will need to write the constructor, and various operations as described above.  Make sure that your creature is not too small, nor too large, as it will have to live in an aqauarium with two dozen other creatures!

Depending upon the creature you design, define appropriate move behavior. Some creatures will swim like a fish, or a submarine, some will wiggle about, some might just stay in one place and rotate, bubble, etc.

You can write the drawTankBackground() function above to modify the default aquarium background, if you wish. 
Note that if you choose to use the default background, you must still include this function in your file with an empty function body (as defined above).

In your overall sketch pay special attention to the aesthetic aspects of your design.




What to Hand in:
Hand in the entire sketch folder in your Dropbox folder, which will include the two files you created for this project. In addition to the sketch/programs also include; (1) a gif/jpg/png image of your finished sketch. (2) A formatted write-up with Page#1 showing your sketch, followed by a title, your name, a short 1-2 line description (as discussed in class) on page#1, and a short 1-2 paragraph more detailed description of the sketch and your personal experiences working on this sketch.


Hints:



Extra Credit:

For up to 20 points extra credit, you can make your object react to nearby objects in the aquarium.  Instead of using the move() function defined in step 2, use the following in its place:

/** Advances the object's animation by one frame.
* Note: Implement only one of the move() functions, but NOT both.
* @param allObjectsLocations an array of the locations of ALL objects in the environment
*/
void move(PVector[] allObjectsLocations) { ... }

This version of the move() function takes in an array of PVectors where each element contains the position of one object in the aquarium (including the current object).  You can access the coordinates by allObjectsLocations[i].x and allObjectsLocations[i].y.  To figure out which element refers to the current object, simply test to see if the x and y coordinates of the element are exactly equal to the x and y coordinates of the object.

For example, you could make a fish that puffs up or blows bubbles whenever some other object comes close, or maybe you could track which elements change position over time and only puff at objects that move.  Or, you could make a fish that runs away from all other fish and tries to hide near stationary objects.

If you do the extra credit, be certain to clearly mark in your submission that you're including the extra credit and include a description of your object's behavior.