6.170 Laboratory in Software Engineering
Spring 2004
Problem Set 0: Introduction to 6.170
Due: Friday, February 6, 2004 at 9pm

Handout PS0

Contents:


Initial Setup

Before doing any work (problem sets, labs, etc.) for 6.170, you need to create a ~/6.170 directory and set up your environment. We have provided a script for this purpose. Type the following commands at your athena% prompt:
     add 6.170
     student-setup.pl
If you see the message "6.170 setup complete", you are done. You must logout and login again for the changes to take effect. If you see an error message, contact the course staff for assistance. (The best way to get help is to visit an LA during office hours. Alternatively, see the staff list for email information, or ask for help on the 6.170 zephyr instance.)

Introduction

In this problem set, you will be introduced to some of the tools that you will be using this semester. These tools will be useful (if not imperative) to you both for the purposes of this class and as a software developer in the real world. This problem set will also have you edit and create some simple Java code.

One of the tools that we introduce in this problem set is Eclipse which is an Integrated Development Environment (IDE). Technically, Eclipse is an "extensible IDE for anything and nothing in particular," but it is best known as being a premier IDE for Java. You are encouraged to edit your Java code in Eclipse, but you are not required to. In the past, 6.170 encouraged students to use Emacs to edit their Java code. We will continue to support and include instructions for Emacs users, but be aware that Emacs is a text editor as opposed to an IDE and therefore is not as powerful as Eclipse. You will begin to see some of the advantages of using an IDE over a text editor in the examples in this problem set.

Instructions are provided for doing this problem set on Athena workstations. We suggest that you use the Athena Linux workstations, as both Java and Eclipse tend to run faster on the Linux workstations than they do on the Athena Solaris boxes. If you wish to use Eclipse from your home computer, you are free to do so, but the instructions will not be as detailed, and the LAs may not be able to help you with configuration issues that arise. See Configuring Eclipse on your home machine for tips.


Problem 1: Compiling and Running Java Programs At the Command Line

First, we're going to see how to compile and run Java programs directly from the Athena command line.  If you plan to use Emacs to edit your code, this is how you'll be running your code in future problem sets.  If you use Eclipse, however, you won't have to do it this way in the future, since Eclipse automatically compiles your code and has menu options for running it.  But you should see how this works now, so that you understand what Eclipse is doing later.

Once again, it is worth noting that the Athena Linux workstations tend to run Java and Eclipse much faster than the Athena Solaris boxes.

Copy Simple.java into your athena locker, by typing at your athena% prompt:

     cp /mit/6.170/www/psets/ps0/Simple.java ~/6.170/workspace/Simple.java  
Simple.java is a Java source file a text file of Java code with a .java extension. To run Simple.java, you must compile it first.

From the command line, you compile a Java class with the javac command. To compile Simple from your athena% prompt, run:

     cd ~/6.170/workspace
javac Simple.java
This may take a while. It will produce a file called Simple.class which is the compiled binary form of the Simple class.

To run Simple, type:

     java Simple
You should see a welcome message.

For more documentation on compiling and running code from the command line, see the Problem Set Procedure page.

Problem 2: Checking Out From CVS

This semester, you will manage versions of your code with a program called CVS which stands for Concurrent Versions System. This system keeps track of previous versions of your code so that you have a backup copy that you can revert back to, if necessary. More importantly, CVS enables multiple developers to work on a codebase at the same time by synchronizing different versions of files checked in by multiple developers. Although you will be the only person working on the code for your problem sets, you may want to switch back and forth between working in Athena and at home, so you can still take advantage of CVS's support for synchronizing multiple copies of code. Further, it is good practice to start using CVS now so that you'll be familiar with it by the time you start working on your team final project. More importantly, you will encounter CVS (or some other type of versioning-control software) in industry and elsewhere, so it is an important tool to learn how to use.

So how does it work? On Athena, we have created an individual CVS repository for you that has the code you need to get started for each problem set. Each problem set is its own module in CVS which you must checkout to your local machine. When you create a new file for the problem set, you must add it to CVS. Every time you make a change to a file worth that is worth recording, you commit that file to CVS. If someone else makes a change to the repository, you must update your local copy to get the other person's changes. CVS will try to merge the copy in the repository with your local copy, but if it can't, then it will notify you that there is a conflict which you must address.

The location of your CVS repository for 6.170 is on Athena at:

/mit/6.170/students/$USER

where $USER is your Athena username. Each 6.170 student correspondingly has his own CVS repository. Since we encourage you to use Eclipse to edit your code, we will give an example of how to checkout from your CVS repository directly into Eclipse.

On the first day of class, we will be creating student repositories throughout the day while students sign up for the course (if you haven't completed the on-line signup form, please do so NOW!). Before going any further, you should make sure your repository has been created by typing ls /mit/6.170/students/$USER on the athena prompt. If you get a "No such file or directory" error, please send e-mail to 6.170-tas and your account will be created ASAP. You can then go on with the rest of this assignment.

Using CVS From Eclipse on Athena

The staff has created a module named ps0 for you in your CVS repository. We will now show you how to use Eclipse to checkout this module from the repository. We will also show you how to edit and compile Java files with Eclipse. Once you have completed the problem set, you will turn in your answers by committing ps0 back into your repository.

Using CVS From Other Environments

We encourage you to try Eclipse for this problem set.  In the future, if you prefer to use Emacs on Athena, we also provide instructions for retrieving problem sets from the command line.
Eclipse is particularly well-suited for working on a home computer running Windows or Linux.  If you prefer to work at home, here are instructions for retrieving problem sets with Eclipse and CVS:

Problem 3: Working with Java in Eclipse

HolaWorld has errors and does not compile. Find these simple errors and fix them so that HolaWorld builds and runs correctly. Eclipse automatically compiles your Java files for you, so you do not have to think about running javac as you did in Problem 1.

Notice that the sections that cause compilation problems are underlined in red. Try using CTRL-Space, which can be used for completion of variable names. Place the cursor at the right of spanishGree and then press CTRL-Space.

You can also find a list of the compile errors by clicking on the Tasks tab, at the bottom of the window.

When you have fixed the errors, save the file (i.e. with File >> Save ), and the errors should disappear.

Once you have fixed the broken HolaWorld, run it using Run >> Run As >> Java Application.  You should see both English and Spanish greetings in the Console window.

Problem 4: Answering Questions About the Code

In this problem, you will create a text file in your Eclipse project and write the answers to some questions in it.  In future problem sets, you will use this technique to provide documentation for your programs.

Select from the top menu File >> New >> File. A window will pop up, asking for the name and location of the file. Select ps0/doc as the parent folder, and name the file problem4.txt.



In problem4.txt, write the answers to the following questions:
  1. How many objects are created by HolaWorld's main method, and what are their types?
  2. What does the keyword void mean in the method signature of the main method?

Problem 5: RandomHello

For this problem, you will write your first Java class from scratch. The class's main method will randomly choose, and then print to the console, one of five possible greetings. The text of the 5 possible greetings is up to you.

First create a new class. Select from the top menu File >> New >> Class. A window will pop up, asking you details about the class.  Use ps0/src as the Source Folder. Your class shold be in the ps0 package, and you should call it RandomHello.

In order to run RandomHello, you need to give it a main method whose signature is public static void main(String[] argv) (which is identical to the signature of the main method in HelloWorld and HolaWorld). When creating RandomHello, you can check the appropriate checkbox and have Eclipse create a stub of the main method for you.

You may use the following as a skeleton for your class RandomHello.java:

package ps0;

/**
* RandomHello selects a random greeting to display to the user.
*
* @author Ben Bitdiddle
*/
public class RandomHello {

/**
* @effects uses a RandomHello object to print
* a random greeting to the console
*/
public static void main(String[] argv) {
RandomHello randomHello = new RandomHello();
randomHello.sayHello();
}

/**
* @effects prints a random greeting to the console
*/
public void sayHello() {

// YOUR CODE GOES HERE

}
}

Instead of writing your own random number generator to decide which greeting will be written to the console, you should take advantage of Java's Random class. (This is a good example of the adage "Know and Use the Libraries" as described in Chapter 7 of Joshua Bloch's Effective Java).

Type the following into the body of your sayHello() method:

     Random randomGenerator = new Random();
This line creates a random number generator.  In Eclipse, your code may be marked with a red underline, indicating an error. This is because the Random class lies in a package that has not yet been imported (java.lang and ps0 are the only packages that are implicitly imported). To explicitly import java.util.Random to avoid the compilation error, you can add the following line under the package ps0; line at the top of your file:

     import java.util.Random;
The above line will import the Random class into your file. However, if you are using Eclipse, you can hit CTRL-SHIFT-O to organize your imports. Because there is only one class named Random, Eclipse can figure out that you meant to import java.util.Random and will add the above line of code automatically. (If the name of the class that needs to be imported is ambiguous – for example, there is a java.util.List as well as a java.awt.List – then Eclipse will prompt you to choose which one to import.)

You can use Eclipse to easily view the documentation of any class you use. The first time you do this, however, you need to tell Eclipse where to look for the external Javadoc. To configure, do the following: