CMSC 110 (Introduction to Computing)
Assignment#5
Due before start of class on
Wednesday, November 7, 2012
Due before start of class on Monday, November 12, 2012
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 we will later combine all
creatures together into a single aquarium.
Complete the assignment by implementing your sea creature in this
file.
<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() { }
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.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) { ... }