Lab 2

Introduction

In order to successfully design and built large software systems we need to manage their complexity. We will do this by designing systems so that they are composed of small, independent parts with well-defined responsibilities which interact with each other. These small simple parts can be developed and tested independently of each other. The complexity of the software system as a whole arises from the interconnections and interactions amongst the component pieces.

In lecture we discussed how to model systems of interacting entities in terms of their properties (or characteristics) and their behaviors (or the services they can offer to other entities). Behaviors are sometimes called capabilities. The behaviors/capabilities of objects determine how they can communicate with each other. The relationships which a given object has with other objects determines which objects it can communicate with.

In lecture we explored some of the relationships which objects can have with each other. In this lab we focus in particular on the dependency relationship. The dependency relationship exists between two objects when one object uses another object in some way. It is for this reason that the relationship is sometimes called the uses a relationship. This relationship can be expressed in Java by simply having one object create a new instance of the other object.


Objectives

The high-level objectives of this lab are to have you, The following are the new concepts covered in this lab:

Assignment Specifications

Being a comic isn't easy. Whenever you tell a joke you hope your audience will laugh, or at least not fall asleep. In this lab you will write a Java program that models an audience in a comedy club (or maybe it's a class in a CSE115 lecture - they're pretty similar). The program will display some faces with funny behaviors/capabilities.

You will write some Java code on your own but not all of it. We have written the Java code (the classes) for the faces; you will write the code to create instances of these classes. When you run your finished program and click on the faces they will demonstrate their behaviors/capabilities to you.

In fact, in practice you rarely write all the code that is required to solve a problem. Instead, you use existing code as part of your solution. This is called code reuse, an important software engineering concept. In Java (and other object-oriented programming languages) code reuse is accomplished by making use of existing class definitions. Thus, instead of writing everything from scratch your solution will consist at least in part of existing components arranged in the proper fashion.

The components you will be working with are classes whose objects have fairly sophisticated behaviors/capabilities. For instance (no pun intended) one of the classes, called CSE115.Faces.YawningFace, models a face that yawns when you click on it. This class has a constructor method, CSE115.Faces.YawningFace(), which you will need to use to create a yawning face object. The constructor method takes care of lots of work for us - it not only ensures that the face is drawn, but also that it is positioned correctly with respect to the rest of the faces. When you click on this face, it will yawn.

If you are now thinking that there is not all that much which you need to specify in the course of this lab, you are right. This lab is a "warm-up" lab to get you used to writing a relatively simple Java program (an applet in this case), compiling and running it.

As mentioned in the Objectives section above, in this lab we want to model the uses a relationship between objects. This means that you need to create instances of each of the classes that the Audience object depends on. The objects that Audience is dependent on are instances of the following three types of face classes:


Helpful Hints

This is a "warmup" lab, and is therefore not intended to be difficult. There are a lot of things you need to do, but the lab description explains what you need to do. Your goal is to create an Audience object and use an Applet to display it. The main challenges of this assignment will be getting used to the environment: Unix, Eclipse, and Java.

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 designing the Audience object, look over the list of predefined objects (see the Class Diagrams section), and decide how you want to use each one. Note that the Audience's only behavior/capability is to construct itself so it contains all its dependent parts.

When you are confident about your 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 therefore harder to debug, so getting into a good habit now will save you a great deal of time in the future.


Reading

Make sure you have read chapters 1 and 2 of the Brown notes before coming to lab. Also make sure you have reviewed your lecture notes.

Lab set-up tasks

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. Before you start work on the lab itself, you must carry out each of the following set-up tasks. Your teaching assistant will guide you through this process.

Step 1: Log in

The first thing you must do when you are in the lab is log in at the SunRay in front of you. This will log you in to a server called
everest.cse.buffalo.edu
Next, open a window on a machine called,
coldplay.cse.buffalo.edu
by typing (at the Unix prompt in a terminal window),
xterm -e ssh -X coldplay &

Note that you don't need to specify the full hostname, coldplay.cse.buffalo.edu, when you are working from a CSE lab. This is because the machine you are connecting from is in the cse.buffalo.edu domain. If you connect to coldplay from home you will need to refer to it with its full hostname.

We will use coldplay to run our software development environment, Eclipse, because it is a faster machine than everest and it has the lastest version of Eclipse and Java installed. coldplay is file-served from the same place as everest, so you will see the same files from both of these computers.

You added the following alias to your .alias file in the last lab:
alias coldplay 'xterm -e ssh -X coldplay &'
From now on, all you have to do to log onto coldplay is type coldplay at the Unix prompt and a new window on coldplay will pop open.

Next, you will need to create a directory/folder in which to keep you work for this course. Create a subdirectory in your home directory called cse115; within that directory create another subdirectory called workspace. Here is one way to do this:

mkdir ~/cse115
cd ~/cse115
mkdir workspace
(At Home) Step 1: Turn on computer

If you are at home you do not need to establish a connection to everest or coldplay or have a prompt open while doing this lab.

 

Step 2: Start Eclipse

Eclipse is a powerful and extensible software development environment. You will use Eclipse to develop your code not only in this course but also in your subsequent coursework in the department. Start Eclipse. You do this by typing eclipse & at the Unix prompt (make sure you do this in the coldplay window). Note that your Unix prompt may look different from mine.

At home: Simply double-click the icon for eclipse.

Eclispe will show a splash screen:

Eclipse will ask if it can start up with a default workspace location. The screenshot below shows the default location for me. Your default location should be ~/cse115/workspace (recall that '~' refers to your home directory: Eclipse will write out the full path). If this isn't the path shown in your window, click "Browse" and browse to that location.

Be sure to check the box "Use as default and don't ask me again", and then click "OK".

Eclipse should now show you a progress bar as it finishes starting up:

At home: You will need to select a location for your workspace. Make a directory using MyComputer in a location that makes sense to you. Once you have started Eclipse, navigate over to that location and use that as your permanent workspace location for this semester.

Once the Eclipse window has opened up completely, you will see the Eclipse welcome screen.

There is a lot of on-line help available in Eclipse, but for now you should close the welcome screen by clicking on the arrow labelled "Workbench". This brings you to the "Resources" perspective:

Step 3: Import preferences file

Next you need do tell Eclipse a few things about how you want it to behave. This is a one-time process. In other words, once you have successfully carried out these steps you will NOT need to repeat them (unless you install Eclipse on your home computer, for example).

If you are using Eclipse at home for the first time, you will need to download this file to your machine. You will use a program like FileZilla to transfer the file from the CSE systems to your home machine. On the CSE systems, the file is located in /projects/CSE115 and is called cse115.epf. On your machine, you can store your file wherever you would like, but you should keep the same name. You will need to remember where you put the file so that you can input it's location below.

First you will need to open the Import dialog. Do this by selecting "Import..." from the "File" menu. It is common practice to abbreviate menu selections that you need to make in instructions like these. For example, we can abbreviate

select "Import..." from the "File" menu
by simply writing "File" -> "Import". This time we'll show the screen shot of the menu item being selected, but from now on we'll simply write menu selections using this abbreviated method. Your teaching assistant can help you if you have trouble locating the correct menu selections.

An "Import" dialog window should appear. Make sure you select "Preferences" and then click "Next".

This opens up an "Import Preferences" dialog window. Enter "/projects/CSE115/cse115.epf" in the field labelled "From preference file". (or the location on your home machine if you are at home. If you are at home, you can download the preferences file here.)

The rest of the options should be OK as they are (they should be as in the screenshot above - if not, make them the same). Click "Finish".

 

Step 4: Set up connections to code repositories

The code that we do in lecture will be distibuted via a CVS code repository. This may sound complicated, but it makes it very easy for you to get the code examples we go over in lecture into your Eclipse workspace, no matter where you are (on-campus or off-campus).

To set things up in Eclipse so that you can pull code out of the repository directly into your Eclipse workspace, first switch to the CVS Repository Exploring perspective. To do this, select "Window" -> "Open Perspective" -> "Other".

Select "CVS Repository Exploring" and click "OK". After this your Eclipse window will look as shown below. Note that you have two tabs in a window on the left of your main Eclipse window, labelled "CVS Repositories" and "CVS Annotate". Make sure "CVS Repositories" is elected (as in the screen shot):

Right-click in the white part of the "CVS Repositories" window; select "New" -> "Repository Location..."

This pulls up "Add CVS Repository" dialog.

Enter information as follows:

Host: hadar.cse.buffalo.edu
For "Repository path", enter the path that corresponds to your lecture section:
/projects/CSE115/Repositories/Spring2006/LectureCode/A
/projects/CSE115/Repositories/Spring2006/LectureCode/B
User: enter your hadar username
Password: enter your hadar password
Connection type: scroll down and select "extssh"
Click "Save password" so you don't need to type your password each time you connec to the repository.
Click "Finish".


You should now have a repository location showing up in the CVS repositories tab:


Do the same thing again to add a repository location for "lab skeleton" repository, using all the same information except for the repository path, which must be:
/projects/CSE115/Repositories/Spring2006/LabSkeletons

Step 5: Check out Lab2 project from LabSkeletons repository

Open the LabSkeletons repository (click on the "+" or triangle to the left of its name to "spin" it open). Open "Head", and then select Lab2:

Now right-click on Lab2, and select "Check out".

Step 6: Switch to the Java perspective

Switch to the Java perspective ("Window" -> "Open Perspective" -> "Other...", then pick "Java"); the Java perspective shows you what is in your local workspace:

Step 7: Open the Tasks view

Open up the "Tasks" view by selecting "Window" -> "Show View" -> "Other...", which brings up a "Show View" window, and then selecting "Tasks" from the "Basics" folder:

Click "OK".


Lab Design

Now that you've completed the set-up of Eclipse, you're ready to start working on the lab itself!

It is important to come up with a design for your program prior to writing it. The UML class diagrams introduced in lecture will help in this endeavor and will be central to discussions throughout the class. Right now we want you to understand how to take a UML class diagram that includes the dependency (uses a) relationship and generate Java code for that. Since it is your first programming lab, the TAs will develop the appropriate class diagram in recitation. Your submitted lab must implement the UML class diagram discussed in recitation.

Open the Applet class in the Java editor

Open up the Lab2 project, and then the edu.buffalo.cse115.lab2 package. Double-click on the Applet class in the Package Explorer. The Java editor will open on the Applet.java file in the center view. Notice that the Applet has no statements in its constructor.

Define the Audience class

Create an Audience class in edu.buffalo.cse115.lab2 package. Your class must implement the design shown in recitation.

Right click on the edu.buffalo.cse115.lab2 package name and select the "New" option, and from the sub-menu that appears, select "Class". A "New Java Class" window will open. In this window you need to enter the name of the new class your are creating, in the field labelled "Name:". You should type: Audience

The remaining default items are all fine. Click "Finish" when you've entered the class name.

 

Create the Dependency Relationship between Applet and Audience

Now you will create your first class relationship! Inside the constructor for Applet, type the code that will create an instance of an Audience.

You must now add comments at the start of both the Applet.java and the Audience.java files, specifing your name, your cse e-mail, and lab section. Also, be sure to save Applet.java, either by clicking on the diskette icon on the toolbar, by selecting "File" -> "Save", or by typing "Ctrl"-"s" (i.e. pushing and holding the "Control" key while at the same time typing the lower-case "s" key).

You will notice that as you make changes to a file the name on the Java editor tab is *Applet.java, whereas on the later one it is Applet.java. The "*" in front of the name indicates that there are unsaved changes.

Fix errors

Eclipse automagically compiles your code as you type and reports problems it finds. Syntax errors are shown with little red X's or yellow and red X's in the left margin of the Java editor window. If you hover over the X Eclipse will tell you what the error is (you can see the errors in the "Problems" view as well). If you click over the X Eclipse will offer ways to fix the error. Usually one of the suggestions is what you'd like to do, but not always (so do choose carefully). If you do not understand any of the syntax errors reported by the compiler, ask your TA for an explanation.

Run

Once your applet compiles, run it! Right click on Applet.java in the "Package Explorer" tab. Select "Run As" -> "Java Applet". (Or possibly: Run... and fill in)

You should see a blank AppletViewer window appear:

The window is blank because you have not filled in the definition of the Audience class: there is nobody in our audience yet! Notice that the AppletViewer window is pretty small. We will need it to be a bit bigger to see all three faces that will eventually appear. Exit the AppletViewer by selecting "Applet" -> "Quit" from the AppletViewer menu. Next, to make the default size for this AppletViewer window a bit bigger, right-click on Applet.java and select "Run As" -> "Run..." This brings up a window labelled "Run" through which you can manage run configurations.

Change the "Name:" of the run configuration from "Applet" to "Lab2 Applet runner". Select the "Parameters" tab:

Set the width to 800, and the height to 600. Click "Apply":

If you now click "Run" you will see a large (but still blank) AppletViewer window.

Exit from the AppletViewer again ("Applet" -> "Quit").

Modify the Audience class

OK, we've got the basics down. Now we need to populate our Audience class. Let's start with creating a CSE115.Faces.YawningFace instance.

Now you need to add a dependency relationship between Audience and the CSE115.Faces.YawningFace classes. To do this, switch back to the Audience.java file, and add the code so that the Audience uses a CSE115.Faces.YawningFace. Fix any syntax errors. Save your work!

Now run the applet again. Eclipse remembers what run configuration you used last, and let's you run that one again simply by pressing the "F11" key. Press the "F11" key on your keyboard now. You should see a face from your audience appear on the screen. Click on the face that appears and you will see one of its capabilities.

Build up the Code Incrementally

Complete the Audience.java file by adding the Java code for the two other faces you will have in your Audience. You should follow the same steps as described in the paragraph above. You may need to resize the Applet window by streching it to see all the faces as you create more of them. Then, you should try clicking on the picture to see what capabilities each object has. You will see the Audience built up until you are finished. Building a little of the code and testing it as you go is an important habit to get into that will help you as the projects get larger throughout the semester.


What you hand in

When you are finished, you need to export your solution from the Eclipse environment so that you can submit it.

You should now disconnect your completed project from the CVS repository from which you checked out the lab skeleton. To do this, right click on the project name, and select "Team" -> "Disconnect". In the "Confirm Disconnect from CVS" window that pops open select "Also delete the CVS meta information from the file system.", then click "Yes":

Now it is time to package up your completed work to get it ready for submission. Right click on the project name, and select "Export...". An "Export" window will open. Select "JAR file":

Click "Next>". This brings up a new screen:


Check Lab2 box
Check: Export java source files and resources
Options:
Check: Compress the contents of the jar file
Check: add directory entries
To pick a name for the file you are exporting to, click the "Browse" button next to the field labelled "JAR file:".


Click "Finish".

Now you can submit the resulting jar file, Lab2.jar. This file should be in your home directory. To submit, you use the electronic submission program that corresponds to your recitation. For instance students in section A1 will submit by typing

submit_cse115a1 Lab2.jar

at the Unix prompt.


Due dates

You have one week from the meeting of your lab to submit your solution. For example, if your recitation meets on Tuesday, January 31st then you must submit the lab by 11:59 pm on Monday, February 6th. The due dates are summarized in the table below. You are always welcome to submit early. If you submit more than once, the later submission will simply overwrite the previous one.

To check that your lab was submitted, you can always refer back to the Submit Inspector on the Resources page of the website. After you have entered your user name, your submissions will be shown. Clicking on the name of a file that is a zip file will show you the contents of the zip file so you can verify that you indeed zipped up all the correct files.

Date of lab Due date for electronic submission
Tuesday, January 31 Monday, February 6
Wednesday, February 1 Tuesday, February 7
Thursday, February 2 Wednesday, February 8
Friday, February 3 Thursday, February 9