CSE115 Fall 2007
 Fall 2007 CSE115 Introduction to Computer Science For Majors I  
CSE115 Fall 2007 - Navigation Menus
CSE115 Fall 2007 - Lab #5

CSE115 Fall 2007 - Lab #5


Working from home

If you are working from home you must download a current copy of the Classlibs.jar file
WINDOWS: download and place in your C:\projects\CSE115\Classlibs\Fall2007 directory
OS X/Linux/Unix/etc: download and place in your /projects/CSE115/Classlibs/Fall2007 directory


Introduction

One way that we can manage complexity is to organize a system into subsystems. In this lab you will learn how objects can be organized into subsystems.

In this lab you will create two subsystems: a flock of birds adn a swarm of bees. You will be using classes which are similar to the bouncing shape classes familiar to you from earlier labs. This time around you will use the following classes:

  • lab5lib.Bird
  • lab5lib.Bee
You will also use the
  • lab5lib.Window
class, which has the drawing canvas on which the lab5lib.Bird lab5lib.Bee appear.

From these pieces you will build two classes: a Swarm class which has at least two lab5lib.Bee objects, and a Flock class which has at least two lab5lib.Bird objects.

The Swarm class must define the startBuzzing and stopBuzzing methods. These methods must be defined to call the methods of the same name on each of the class's instance variables - so that the subsystem acts as a unit.

The Flock class must define the startFlying and stopFlying methods. These methods must be defined to call the methods of the same name on each of the class's instance variables - so that the subsystem acts as a unit.

More details are given below.


Objectives

The high-level objectives of this lab are to have you:
  • define a class
  • declare and initialize instance variables
  • define constructors and methods
  • delegate method calls

Assignment Specifications


Helpful Hints

Read through the entire lab before you start working, so that you know what to expect. Make sure you save your work often, and keep track of what you are expected to submit.

When you are confident about the design, start writing your program. As you work, add just a small part to your program at a time, testing each one to make sure that it works as you expect them to. Working incrementally is an important technique for managing large problems.


Preparatory work

In order to be able to carry out the tasks required during your lab session, you must come prepared. Do the following before coming to your lab session:

Reading

Make sure you have read and understood chapters 1 through 4 of the Alphonce-Decker notes. Also make sure you have reviewed your lecture notes.


Lab tasks

At your lab session your teaching assistant will briefly discuss how to carry out each of the tasks below. She or he will also be available to answer questions you might have. You must carry out each of the following tasks.

Log in

Start Eclipse

Get project skeleton

Retrieve the Lab5 project skeleton from the LabSkeletons respository. When you do this, you will notice lots of errors in the code (i.e. lots of red "X"s). Don't panic - this is actually exactly right. The classes we are providing to you refer to the classes which you haven't yet written, which is why the compiler is unhappy. As you write the missing classes, the errors will go away.

Disconnect the project from the repository

Make sure you disconnect your local copy of the Lab 5 project skeleton from the repository.

Look at what you have...

In the Lab5 project you will see two packages: lab5 and lab5support. You do not need to worry about the code in the lab5support package.

In the lab5 package there is one class, Driver, which is essentially empty. You must complete the definition of this class (which includes inserting proper javadocs) so that it creates a flock and a swarm, creates some buttons in the graphical user interface (the button classes have already been defined - you simply need to instantiate them), and hooks the flock and swarm up to the appropriate buttons.

...think ahead...

The basic design you want implement is shown in class diagram. I recommend you open this in a new tab. If you're using Netspape or Firefox, right click on the link, and select "Open link in new tab".

Class diagram

Now, before you feel that this is overwhelming, remember how we deal with complexity: we deal with it one small step at a time. We'll get you started in this lab description, and your lab TAs will help you out along the way too.

Step 1 - Start a new UML diagram

For this lab, let us use Green to build as much of the project as possible. To start, right click on the lab5.Driver in the package explorer, and select "Green UML"->"Add to New UML Diagram". Once lab5Driver is open in the diagram, move it to the center of the diagram (click, drag, and drop).

Step 2 - Create a Window

The Driver class is the part of our overall system that takes reposibility for creating all the (major) components of the system, and making sure they are related in the right ways so they can communicate with each other.

The first thing we want to ensure is that the Driver class creates an instance of the lab5lib.Window class. This points to a dependency relationship between the Driver class and the lab5lib.Window class (we'll make it a local variable dependency). To draw this relationship in the diagram you will first need to open lab5lib.Window in the diagram. To do this, make sure the lab5.Driver class box is not selected.

When a class box is selected, its name compartment is highlighted in green. If it isn't selected, its name compartment is the same tan color as the rest of the box's compartments.
Now right-click on an empty part of the diagram canvas, and select "Open Type in Editor". This brings up a winow; in the "Select entries" box type "lab5lib.Window" and click "OK". A classbox for lab5lib.Window should appear.

Draw a local variable dependency) between the Driver class and the lab5lib.Window class. The cardinality should be 1 and the relationship should be created in the constructor of Driver.

Note: Green seems to be a little cranky here. When I draw the relationship the correct code is generated, but the relationship line does not show up. If this is the case for you, don't worry, but do make sure that the correct code is generated in the Driver class.
Step 3 - Create a Swarm

Use Green to create a new class. The class we'll create first is the Swarm class. Select the "Class" icon in Green's tool palette. Click somewhere in the diagram. This will bring up a window which will prompt you for the following information (some may be filled in for you already):

Source folder: Lab5
Package: lab5
Name: Swarm

This Swarm class is very important, because it is going to represent a group of Bees. For now, let us have the Driver create a Swarm.

Draw a local variable dependency) between the Driver class and the Swarm class. The cardinality should be 1 and the relationship should be created in the constructor of Driver.

Don't forget to write javadocs for classes and methods as you go along! It is much easier to write the javadocs as you're writing the code, rather than as an afterthough. Get into the habit of writing javadocs as you write the code. In fact, you can write javadocs for classes and methods before you complete their definitions as a guide to help you keep in mind what the classes and methods are supposed to be doing.
Step 4 - Set up a "Start Swarm" button

There support code for this lab has some buttons already defined. There are four pre-defined buttons. When clicked, one will cause a swarm of bees to start buzzing around, while another will (when clicked) cause the swarm of bees to stop moving altogether.

Open the lab5.support.StartSwarmBuzzingButton in the current diagram by opening the lab5.support package, right-clicking on the lab5.support.StartSwarmBuzzingButton class, and selecting "Green UML"->"Open in Last Viewed Class diagram". When this opens in the diagram you will notice that there already is a relationship between the lab5.support.StartSwarmBuzzingButton class and the Swarm class.

Now set up an instantiation dependency between Driver and lab5.support.StartSwarmBuzzingButton.

Now edit the Driver class. Organize the generated code so that the window is created first, the swarm second. Finally, make sure that the swarm is passed as an argument to the button constructor, and that the button reference is used as an argument in an addButton method call on the window. Calling this method with the button reference will ensure that the button appears visually in the program.

Step 5 Add startBuzzing to Swarm
Define a startBuzzing method in the Swarm class - for now it is fine if its method body is empty.
Step 6 - Test!

Great! Now you're at a point where you can test your program. Even though it won't do much, by testing it now you can verify that you have correctly connected the pieces you are using. After resetting the DrJava interactions pane, use DrJava to instantiate the lab5.Driver class. A window should open that looks like this:

If you think there may be a problem at this point, ask your TA for help. Otherwise, continue on!

Step 7 - Add a "Stop Swarm" button

You can now add the second button that controls the swarm. Do that by setting up an instantiation dependency between Driver and lab5.support.StopSwarmBuzzingButton. Once you've done that, define a stopBuzzing method in the Swarm class - for now it is fine if its method body is empty.

Step 8 - Test!

Great! Now you're at a point where you can test your program. Even though it won't do much, by testing it now you can verify that you have correctly connected the pieces you are using. After resetting the DrJava interactions pane, use DrJava to instantiate the lab5.Driver class. A window should open that looks like this:

If you think there may be a problem at this point, ask your TA for help. Otherwise, continue on!

Step 9 - The Swarm

Now you're in a position to add bees to the swarm. Doing this will let you see the effect of pushing the buttons! Remember that we want starting or stopping the swarm to start or stop each bee in the swarm. We do this by ensuring that when the startBuzzing or stopBuzzing of the Swarm is called, the corresponding method is called on each bee in the swarm.

Again, we'll do this incrementally. First let's add one Bee to the Swarm. Use Green to create the appropriate (i.e. going in the right direction) composition relationship of cardinality 1.

Next you need to edit the code in the definition of the Swarm class. Just like the shapes from lab 3, like lab2lib.BouncingBall, had to be added to the lab3lib.Window by calling the addShape method on the window, you have to add the Bee to the lab3lib.Window by calling the addShape method. But wait, where do we get a lab5lib.Window from? Remember that the Driver created a lab5lib.Window. Edit your Swarm class so that its constructor takes a lab5lib.Window as a parameter. Then edit the instantiation of the Swarm in the Driver so that the lab5lib.Window reference is passed as an argument.

Step 10 - Test!

Now, if you test your program, you should get a Bee buzzing around in the window. But pressing the buttons still doesn't appear to do anything. That's OK - you should be all set to carry on.

Step 11 - Make the Swarm stop its Bee

In fact, pressing the buttons does do something: pressing the "Stop Swarm" button causes the stopBuzzing method of the Swarm class to be called! So what? Well, if you want your Bee to stop moving around, you need to define the stopBuzzing method so that it calls the stopBuzzing method of the Bee object whose reference you're storing in the instance variable in Swarm.

Step 12 - Test!

Now, if you test your program, you should get a Bee buzzing around in the window. Pressing the "Stop Swarm" button should now make the Bee stop moving around. Pressing the "Start Swarm" button doesn't yet appear to do anything.

Step 13 - Make the Swarm start its Bee

Now you want your Bee to start moving around, when the "Start Swarm" button is pressed. Define the startBuzzing method so that it calls the startBuzzing method of the Bee object whose reference you're storing in the instance variable in Swarm.

Step 14 - Test!

Now, if you test your program, you should get a Bee buzzing around in the window. Pressing the "Stop Swarm" button should now make the Bee stop moving around. Pressing the "Start Swarm" button should now make the Bee start moving around.

Step 15 - Add more Bees!

You're almost done with the Swarm! All you need to do is add at least one more Bee. How many more you add is up to you. To add more Bees, you need to set up the appropriate composition relationships, and make sure the new Bees are added to the lab5lib.Window, and edit the startBuzzing and stopBuzzing methods so that the new Bees also react when the buttons are pressed.

Step 16 - Test!

Now, if you test your program, you should get a swarm of Bees buzzing around in the window. Pressing the "Stop Swarm" button should now make all the Bees stop moving around. Pressing the "Start Swarm" button should now make all Bees start moving around.

Steps 17 - Do the Flock

Now you're halfway done! Just follow the same steps to create a Flock of Birds. The only constraint is that we'd like your Flock and Swarm differ in how many Birds and Bees they have. Don't forget to work incrementally, testing as you go along.

Grading (rough, and subject to change)

As a rough guideline, you can expect 30 points to come from your JavaDocs, 60 points to come from coding, and 10 points to come from proper submission.

What you hand in

Once you are ready to submit, export your solution as a jar file. Name your jar file Lab5.jar

Use the electronic submission command that corresponds to your recitation. For instance students in section A1 will submit by typing,

submit_cse115a1 Lab5.jar
at the Linux prompt.

Due dates

You have one week two weeks from the meeting of your lab to submit your solution. For example, if your recitation meets on Wednesday, October 3nd then you must submit the lab by 11:59 PM on Tuesday, October 9th Tuesday, October 16th. The due dates are summarized in the table below.  If you submit more than once, the later submission will simply overwrite the previous one.

Date of lab Due date for electronic submission
Tuesday, October 2 Monday, October 15
Wednesday, October 3 Tuesday, October 16
Thursday, October 4 Wednesday, October 17
Friday, October 5 Thursday, October 18


Notice

This lab was designed and written by Carl Alphonce.

CSE115 Fall 2007

 

Page maintained by Carl Alphonce
tel: (716) 645-3180 x 115 • fax: (716) 645-3464 • e-mail: alphonce (at) cse dot buffalo dot edu