CMSC 110 (Introduction to Computing)

Spring 2011

Assignment#5

Due before start of class on Thursday, March 24, 2011



Task:
Design an underwater creature. The creature should be designed so that it can be instantiated as an object. Make sure you name the class as <YourFirstInitialLastName>Obj. For example Eric's creature will be defined in a class called EEatonObj. 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().  Your implementation should look like the following:

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.

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

The driver code listed below should be used EXACTLY as is to run your class.  You can write the drawTankBackground() function above to modify the default aquarium background, if you wish.


/***************************************
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
***************************************/




What to Hand in: Hand in the entire sketch folder in your Dropbox folder. 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 above, 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.