CSE115 Fall 2007 - Lab #4
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
In this lab you will practice instantiating classes, calling
methods, and using local variables to swap the properties of two
objects.
Objectives
The high-level objectives of this lab are to have you,
- edit some Java source code files using Eclipse, and
- compile, debug and run a small Java program.
The following are the new concepts covered in this lab:
- defining non-constructor methods
- declaring local variables
- assigning values to variables
- calling a method (sending a message) with arguments
- getting a value from a method call (receiving a value in reply to a message)
- writing javadoc comments
Assignment Specifications
For this lab we have built a framework which creates a graphical user
interface (GUI, pronounced "gooey") for which you are going to provide
the functionality. The program will display two separate areas
(called "drawing canvases"), each of which will contain an object of a
certain color, represented on-screen using a particular graphic
(either an ellipse or a rectangle), which behaves in a particular way.
The GUI has three buttons. Pressing the "swap colors"
button will interchange (swap) the colors of the two objects. Pressing
the "swap behaviors" button will interchange (swap) the behaviors of
the two objects. Finally, "swap graphics" will interchange (swap) the
graphic used to draw the two objects.
Each time you click on one of the three buttons, exactly one of the
three properties (color, behavior, graphic) is exchanged between the
two objects, and the other two stay the same.
Your task is just to complete the definitions of the three methods
which are to carry out the swap of the property values.
In the code, the objects whose properties you must swap are
instances of the class lab4lib.ChangeableShape.
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.
Begin by just getting the GUI window to appear. Then add small
parts to your program, making sure that they work as you expect them
to. This idea of writing your program incrementally will be very
important as your programs get larger and harder to debug, so getting
into a good habit now will save you a great deal of time in the
future.
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, 2 and 3 of the
Alphonce-Decker notes. Also make sure you have reviewed your lecture
notes.
Try the demo
Make sure you have tried the demo, so that you understand what the
behavior of your program should be in the end. If the applet does not
load then you need to install the Java plug-in for your
browser.
Note: Not all department browsers have the plug-in installed.
Note: Decompiling the demo is a
flagrant violation of the policy on academic integrity and will
minimally result in immediate failure in the course.
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 Lab4 project skeleton from the LabSkeletons
respository.
Disconnect the project from the repository
Make sure you disconnect your local copy of the Lab 4 project skeleton
from the repository.
Look at what you have...
In the Lab4 project you will see two packages:
lab4 and lab4support. The only file you
need to worry about is the PropertySwapper class defined
in the lab4 package.
This class has three methods defined with empty bodies. Your job
is to complete their definitions so that they swap the relevant
property of the two lab4lib.ChangeableShape objects (or
shapes, for short) in the left and right drawing canvases. These
methods are called when the corresponding button is clicked in
GUI.
Next, see what's already available to you: run the program by
creating an instance of lab4.Driver in DrJava's
interactions pane. When the GUI comes up, notice that clicking the
buttons has no effect.
...think ahead...
You need to write code for each of the following methods:
public void swapBehaviors() {
//TODO Complete the body of this method to swap behaviors of the shapes.
}
public void swapColors() {
//TODO Complete the body of this method to swap colors of the shapes.
}
public void swapGraphics() {
//TODO Complete the body of this method to swap graphics of the shapes.
}
If you reflect a bit on the problem, you should realize that although
a different property value is being swapped between the two shapes in
each method, the steps required to swap a property value should be
essentially the same in each case.
...but start with just one method
Suppose you start with the swapColors method. This
method is called
automagically
whenever the button labelled "swap colors" is pressed. Any code
you put in the body of this method will be executed whenever the
button is pressed.
Since you want to swap the colors of the two shapes when the "swap
colors" button is pressed, you need to add to the body of this method
the Java code which will do exactly that: swap the colors of the two
shapes.
But how can we do this? First, we need to be able to communicate
with the two shapes: we need to get references to them! To avoid
using any Java languages features that you have not been exposed to
yet, you will need to do a little bit of extra work here, but only
using things you already know about.
Instances of the lab4lib.ShapeHolder class provide
two methods you will need; their method headers are:
public lab4lib.ChangeableShape getLeftShape()
public lab4lib.ChangeableShape getRightShape()
They return references to the shapes in the left and right drawing
canvases, respectively. The references are to objects of type
lab4lib.ChangeableShape.
Each lab4lib.ChangeableShape provides methods which
you can use to both get and set the values of its properties; the
headers of these methods are:
public void setColor(java.awt.Color color)
public java.awt.Color getColor()
public void setBehavior(Behavior behavior)
public Behavior getBehavior()
public void setGraphic(Graphic graphic)
public Graphic getGraphic()
Once you have written some code to swap colors, test your solution
by running the program and make sure that the color swaps when you
exchange the shapes' colors.
If your method doesn't do what it should, go back and fix it. If
you need help, talk to your TA.
Once you have your color-swapping method all finished, write some
nice JavaDoc comment for the method explaining what the method
does (not how). Your TAs will show you how to do this in more
detail.
Move on
Now try one of the other methods, like the behavior-swapping method.
Once you have your behavior-swapping method all finished, write some
nice JavaDoc comment for the method explaining what the method
does (not how).
Move on to the last method. JavaDoc it appropriately.
Finally, add JavaDoc comments for the class itself.
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 of the three metohds, 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 Lab4.jar
Use the electronic submission command that corresponds to your
recitation. For instance students in section A1 will submit by
typing,
submit_cse115a1 Lab4.jar
at the Linux prompt.
Due dates
You have one week from the meeting of your lab to submit your
solution. For example, if your recitation meets on Wednesday,
September 26
st then you must submit the lab by 11:59 PM on
Tuesday, October 2
nd. 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, September 25 |
Monday, October 1 |
| Wednesday, September 26 |
Tuesday, October 2 |
| Thursday, September 27 |
Wednesday, October 3 |
| Friday, September 28 |
Thursday, October 4 |
Notice
This lab was designed and written by Carl Alphonce.
It is inspired by a lab originally designed and written by Stephen Wong.