CMSC 110 (Introduction to Computing)
Fall 2015
Assignment #6
Part A: Due before start of class on Monday, Nov 16, 2015
Part B: Due before start of class on Monday, Nov 23, 2015
Task: Design an underwater creature that will live in a class aquarium.
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 argument passed into the constructor. The location of the object should vary each time the constructor is called. In addition to the constructor, make sure that the class implements all of the methods of the AnimatedObject Interface (shown in Part A, Step 1).
Make sure to name the class as <FirstLastName>Creature. For example, my creature will be defined in a class called DavidCooperCreature. Your class's constructor should take a single float argument that is approximately the creature's size. The value of this argument should be roughly the diameter of a circle that encompasses your creature. In addition to the constructor, make sure that the class includes one method called display(), and one called move() so that one can incorporate your creature into a sketch with other creatures.
Part A (3 steps!)
Step 1: Cut and paste the following code into a file called Assignment06_<FirstLastName>.pde (e.g., my sketch file would be called "Assignment06_DavidCooper.pde"). Save the sketch immediately. This is the main file for testing Assignment 6. The driver code listed below should be used VERBATIM to run your class for part, with ONE exception: in the setup() function, you will need to replace "DavidCooperCreature" with the name of your creature 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 ... ****************************************/ interface AnimatedObject { /** * @return the x position of the center of the object */ float getX(); /** * @return the y position of the center of the object */ float getY(); /** * @return the diameter of the object */ float getSize(); /** * Advances the object's animation by one frame */ void move(); /** * Displays the object */ void display(); /** * Display a signature block with name of creature * and Your name with 0,0 representing the upper left * hand corner of a w x h rectangle * @param w the width of the signature block * @param h the height of the signature block */ void signature(float w, float h); /** * Extra credit: up to 20 points. * Modifies the object based on the other objects. * @param objs an array of AnimatedObject creatures in the environment */ void react(AnimatedObject[] objs); } /** The array of creatures */ AnimatedObject[] creatures = new AnimatedObject[20]; /** Setup the sketch */ void setup() { size(displayWidth,displayHeight); smooth(); // initialize all the objects for (int i=0; i < creatures.length; i++) { creatures[i] = new DavidCooperCreature(random(height/20,width/16)); // change this line to call your constructor } } /** The main draw loop */ void draw() { background(32,178,170); float buffer = 0.2; int numCellsWide = 7; float gridWidth = width/numCellsWide; float cellWidth = gridWidth * (1.0-buffer); float bufferWidth = buffer * gridWidth; float gridHeight = gridWidth; float cellHeight = gridHeight * (1.0-buffer); float bufferHeight = buffer * gridHeight; for (int i=0; i < creatures.length; i++) { pushMatrix(); translate(bufferWidth + gridWidth * (i%numCellsWide), bufferHeight + gridHeight * (i/numCellsWide)); creatures[i].signature(cellWidth, cellHeight); popMatrix(); } // draw and animate each of the objects for (int i=0; i < creatures.length; i++) { creatures[i].react(creatures); creatures[i].display(); creatures[i].move(); } } /************************************** ... TO HERE ***************************************/
Step 2: Create a file called <FirstLastName>Creature.pde (e.g., my file would be called DavidCooperCreature.pde) -- you can start a new file by creating a new tab. Cut and paste the following code into the file. 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 creature object and file appropriately, since we will later combine all creatures together into a single aquarium.
Complete Part A by defining the fields, and implementing the constructor, display(), getX(), getY(), and getSize() of your sea creature in this file. Note: Updated/Corrected text below
// Keep this line as is, except for renaming the class class <FirstLastName>Creature implements AnimatedObject { // Constructor <FirstLastName>Creature(float size) { // ... } // Displays the creature // Implement this for Part A void display() { // ... } // Moves the creature // Implement this for Part B void move() { // ... } // Reacts to other creatures // Implement this for Extra Credit void react(AnimatedObject[] objs) { } float getX() { // put code to return the center x position } float getY() { // put code to return the center y position } float getSize() { // put code to return the size } void signature(float w, float h) { // put code to draw signature } }
The focus should be on the use of classes to define the object factory. You will need to write enough code so that the display() method will display the creature based on its location and size. You can use other features, such as orientation, movement vector, or anything else you think of to help determine how your creature is displayed.. Make sure that your creature fills the circle defined by the location and the size of the creature as close as possible. If your creature has a best default size, then make a comment in your code stating the best size.
Step 3 (submission): Create a folder called A6A. Copy your sketch folder into A6A. zip A6A, and submit it to Moodle.
Part BIn addition to moving the x,y position and/or orientation of your creature over time, define at least one "move behavior" appropriate to your creature. 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.
Note that the final showcase will include one instance of each creature defined by you and your classmates swimming together in a single aquarium sketch.
Hints:
Extra Credit:
For up to 20 points extra credit, make your creature react to nearby creatures in the aquarium. Instead of leaving the react(AnimatedObject[] objs) method blank, implement it:
/** Reacts to other creatures
* Implement this for Extra Credit
* @param objs an array of AnimatedObject objects in the environment
*/
void react( AnimatedObject[] objs ) {
// ...
}
The react() method takes an array of
all AnimatedObject objects, including the current object.
You can compare creature objects using the standard '==' and '!=' operators. For
example, you may want to do the following to ensure that the current object is
different than a given object in the array.
if (this != objs[i]) { ... }
Step 3 (submission): see below
What to include in your class file:
What to hand in: