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:
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!<First Initial Last Name> class <First Initial Last Name>Obj extends AnimatedObject { // keep this line as is, except for renaming the class
/** Constructor */... } Obj(float size) { ... }
void display() {... }
void move() {/** If you want to enhance the tank background
int getX() { ... }
int getY() { ... }
}
* 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() { }
/***************************************
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
***************************************/
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.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.
* 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) { ... }