Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

Lab 4: The Birds of Spring
Due: 4:00 PM, Friday, February 28, 2014

Spring is around the corner, the birds are coming back. So we'll develop some programs with an avian theme.

You may work alone or with a partner on this lab. Only one submission per group is needed.

There are two practice programs and two programming assignments in this lab. Please refer to the "Submission Guidelines" on the course home page and syllabus for the requirements for each of these items.

Getting Set Up

Working solutions to all programs can be downloaded here.

Download the a starter project with partial implementations of some of the programs from here.


First, you'll practice a technique similar to one you'll need in the first programming assignment: making an object grow taller.

Practice Program: Write a program that creates a 50 ×10 black rectangle at (100, 300) when it starts. Each time the mouse is moved inside the rectangle, it grows upward by 5 units (i.e., it gets taller). When it reaches a height of 250, it turns red and stops growing. (8 points)

A few notes on this one:

Irate Ibises

Your first of two programming assignments is to complete a program that draws some birds (we'll think of them as ibises, since they're going to be tall birds, hence the name "irate ibises", which I hear is the next hit app from Rovio).

The canvas initially starts empty. When you press the mouse on the canvas, an ibis starts growing at that point. Initially it will be just a short bird with a body, beak, and legs. The bird's body may be of a color of your choosing. The base of the bird's body should be at the press point. As you drag the mouse around, it grows taller. When it reaches its full height, the bird stops growing and eyes appear. The ibis won't grow any more, but if you press on its eyes after the bird has reached full size, its body should change to a new randomly generated color. When you are happy with the ibis's color, you can grow another one by pressing somewhere else in the window.

Your program should be divided into two classes: a window controller called IrateIbises and an Ibis class. The starter folder provides a complete IrateIbises class, which you should not modify. Your task is to complete the partially-implemented Ibis class so that it works with the provided IrateIbises. In particular, the Ibis constructor takes two parameters: the Location of the bottom of the bird's body and the canvas, and draws the initial ibis. You are welcome to use this code or replace it with one of your own design. The Ibis class defines "stubs" for the following methods which are used by the controller to implement the functionality described above:


A working solution for the program will appear below. Click inside the applet to interact with it.

More Practice

Since your second programming project involves an ActiveObject a bit different from the ones you've seen, we'll start with a practice program to create an ActiveObject that behaves similarly.

In the BallTossAround folder in the starter you downloaded, you will find a window controller class called BallTossAround that creates a FilledOval every time you press the mouse and passes that FilledOval to the constructor of an active object (which you will implement) that is responsible for moving that ball across the screen as if you "tossed" it upward to the right.

Most (all?) of the active objects we have seen so far are responsible for creating the graphical objects they animate. Here, though, the active object takes the object it animates as a parameter to the constructor.

Specifically, the active object's constructor should remember the ball it is passed, remember the canvas (or perhaps just its dimensions) so when the run method animates the ball, it can stop when the ball goes off the bottom or right of the canvas. Its run method should start the ball moving up and to the right, but the ball should be subject to "gravity" - its y speed should increase each time around the animation loop. So you should see the ball trace out a parabolic path. When the ball goes off the bounds of the canvas, the loop should stop and the ball should be removed from the canvas.

Practice Program: Complete the implementation of the BallTosser class. (7 points)

Angry Bird

Your final task is to implement a very simplified version of the popular Angry Birds game, where millions of people spend billions of combined hours launching birds at pigs. (Note that I am not judging or criticizing here, I am a recovering Angry Birds addict.) Since your program will have just one bird and one pig, maybe we'd call it "Angry Bird".

The program stars with a bird sitting on a perch near the bottom left and a pig sitting somewhere near the bottom right of the canvas. To play, you press the mouse and drag around near the bird, resulting in a "sling" that follows your mouse around on one end but which remains anchored to the bird on the other. When you release the mouse, the angle of the sling determines a "launch vector" for the bird. The bird starts moving in the direction you launched him, and flies across the canvas, subject to acceleration due to gravity. The game ends in success if your bird bumps into the pig, or failure if your bird leaves the visible bounds of the canvas before bumping into the pig.

A few more details, hints, and other notes:


A working solution for the program will appear below. Click inside the applet to interact with it.

Bonus Opportunity

You can earn up to 10 points of bonus for enhancements to the two programming assignments. Check with your instructor for details.


Before 4:00 PM, Friday, February 28, 2014, submit your lab for grading. There are four things you need to do to complete the submission: (i) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab4". (ii) Demonstrate the execution of your programs for your instructor. (iii) Hand a printout of the Java files that make up the programming assignment (not practice programs) to your instructor. (2 business day grace period for demos and printouts).


This assignment is worth 100 points, which are distributed as follows:

> FeatureValueScore
Practice Programs
GrowingRectangle initial creation 1
GrowingRectangle rectangle grows upward on move 4
GrowingRectangle rectangle stops growing and turns red 3
BallTosser moves ball 3
BallTosser gravity correct 2
BallTosser stops and is removed from canvas 2
Style, Design, and Efficiency
Appropriate comments 5
Good variable names 3
Appropriate variable declarations 3
Good use of constants 3
Appropriate formatting 1
Does not generate new objects unnecessarily 2
Good overall design and methods 3
IrateIbises Correctness
Ibis grows while mouse is dragged 7
Growth stops at specific height 4
Eyes appear when height is achieved 7
Color changes when most recent ibis's eyes clicked 4
Color changes only when growth is complete and eyes are visible 3
AngryBirds Correctness
Perch drawn 1
Bird placed initially on perch 2
Pig placed randomly in lower right portion of canvas 3
Sling appears when mouse press on bird (only) 3
Sling one end follows mouse, other at center of bird 2
Sling disappears on launch 1
Bird launched on release (only if sling visible) 4
Basic bird animation loop 3
Bird launch vector determined by sling angle 4
Bird animation subject to gravity 4
Bird stops on contact with pig 4
Bird stops when off visible portion of canvas 4
Message updates 4
Using image files for bird and pig 1