In labs 2 & 3 you learned how to implement the dependency (or uses-a) relationship, the composition (or has-a) relationship, and how to call methods. In this lab you will use these relationships along with the association relationship (also called the knows-a relationship), and you will actually write your own methods.
If you are working from home you must download a current copy of the Classlibs.jar file and place it in your
c:\projects\CSE115\Classlibs\Spring2006\
folder on your home computer. You must do this even if you already did it for Lab 3. This directory structure will probably not be created on your home computer already, so you will need to create it. Remember to follow spelling and capitalization exactly.
Please note, if you have not set up your home computer with Eclipse and Green that this is the last opportunity for you to complete that process and ask questions about it. After Lab 4, technical support questions about working from home will receive lowest priority. Please make sure to ask any leftover questions about set-up issues this week.
In this lab you will define a collection of Java classes which, together with some classes we provide to you, create an applet. The applet will once again display an area (called a "drawing panel"), where we will create some awesome bouncing triangles.
This applet will also have an area with buttons on it. The type of button we will see is called a radio button. A radio button allows us to give a user choices to select from for a particular action. There will be three sets of these buttons, one set controls how fast the triangles move across the screen, one set controls how fast the triangles rotate, and the last set controls how large the triangles are on the screen. Selecting one of the speeds, selecting a rotation speed, and selecting one of the sizes, and then selecting a triangle will allow the triangle to take on the properties (velocity, speed of rotation and size) selected.
You will be writing the code that creates the triangles on the screen. You must create at least two triangles, but you can create as many as you would like in your program. Remember that red triangles are boring - pick different colors! For testing purposes, it might be best to create your triangles initally moving very slow and with large size. You will also be writing the code that allows the triangles to get the information from the button selections.
We give you the skeletal structure of these classes' definitions. Your task is to set up the relationships indicated in the class diagrams by defining instance variables, initializing them as appropriate, and completing constructor definitions. The TAs will help you with some of the design of this lab. Some of the lab you will have to design yourself.
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 your applet 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.
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 Brown notes. Also make sure you have reviewed your lecture notes.
If you are working in the lab in Baldy 21, you won't need any additional set-up to work with the lab.
However, if you are working from home, you will need to download and install the Green add-on, located here. This should be unzipped to C:\Program Files just as all the other Eclipse/Green setup stuff was.
At your lab session your teaching assistant will briefly discuss how to carry out each of the set-up tasks below. She or he will also be available to answer questions you might have. If you have forgotten how to do any of these tasks, please refer to earlier labs.
Step 1: Log in
Step 2: Open a terminal window on coldplay
Step 3: Start Eclipse
Step 4: Get project skeleton
Retrieve the Lab4 project skeleton from the LabSkeletons respository.
Step 5: Disconnect the project from the repository
Make sure you disconnect your local copy of the Lab 4 project skeleton from the repository. (See the instructions from Lab 2 if you've forgotten how to do this).
Once you've done all these things - you're all set to begin the lab.
The TAs will be helping you to design the relationships between the triangles
and the buttons on the screen, but what you need to design is the relationship
between the lab4.Applet
, your top-level object (last time it was
named BouncingShapes
and you could use that name again), and the
triangles. Note that because the triangles in this lab have some slightly different
capabilities, you will actually be modifying the triangle class itself. You
will also be creating instances of this class (lab4.ReactingBouncingTriangle
)
as opposed to the CSE115.ShapeWorld.BouncyShapes.BouncingTriangle
as we did last time.
You should begin in Green by opening up the Applet class in the diagram
and creating your top-level object. Create the relationship between them.
Get a class box for the lab4.ReactingBouncingTriangle
to
appear and create a relationship between your top-level object and the triangles.
Recall that you need at least two, but you can have as many as you want.
Now, you should go into the code generated by Green. Make sure that the code
generated
is
what
you
expected for each of the relationships you selected.
You should also add code to change the color (size and speed perhaps) of the
triangles you created. Recall that there was a method that could be called on
the triangles to change their color. If you don't remember how to do this, you
should refer back to Lab 3. This method is also available on the lab4.ReactingBouncingTriangle
.
You will need to remember how to have an object call a method on itself.
Run your Applet and see that you have the correct number of triangles and in the correct colors.
Notice that in your Lab 4 skeleton, you also have classes for each of the buttons on the screen. Notice also that none of the buttons appear on the screen when you run your Applet. You will need to create instances of each of these buttons to get them to appear on the screen. Decide what relationship these buttons will need with your top-level object and create it either in Green or by typing it in. Make sure that you have created one instance of each of the nine buttons.
Now, when you run the program, the buttons will appear on the screen. However,
notice that each button is instructing you to change its name by calling the
setText
method. You should now go into each button class in the
lab4
package and give each a name that makes sense. Run the program
again to see your names for the buttons.
Now, you should see what happens when the buttons are clicked on. Pick one
of the buttons and inside the button's public void select()
method, place
the following line of code:
System.out.println("I've been clicked! I've been clicked!");
When you run the program again and click on the button, you will notice that
message appearing on the Console tab. This happens because the select
method is called auto-magically whenever the user selects the button
on the Applet.
This happens with the triangle's react
method as well. Put the
line of code from above in the triangle's react
method. Run the
program, click on the triangle, and you will see the message appear again.
We now see that the buttons and the triangle react when they are clicked on. The only piece left is to create a way for the triangle to know about what values the user has selected from the push buttons.
Your TA will explain more about this in lab this week, but you will basically need an entirely new object that the triangle and the buttons communicate with. This object will hold on to the information from the buttons and provide the information to the triangle about what values the user selected.
You will need this kind of holder object for each property the user can select from (velocity, size, rotation speed). Then you have the buttons tell the holder when they have been selected and tell the triangle to ask the holder what speed, size, rotation speed has been selected when it gets clicked on and you're all set!
Be sure to generate a Lab4.dia file in your Lab4 project before you export it. You need to show all of the classes in the lab 4 package in the diagram as well as
Once you are ready to submit, create a jar file named Lab4.jar
Use the electronic submission program that corresponds to your recitation to submit your solution for this lab.
You have two weeks from the meeting of your lab to submit your solution. 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, February 14 | Monday, February 27 |
Wednesday, February 15 | Tuesday, February 28 |
Thursday, February 16 | Wednesday, March 1 |
Friday, February 17 | Thursday, March 2 |