| 6.170 | Laboratory in Software Engineering Spring 2004 Problem Set 0: Introduction to 6.170 Due: Friday, February 6, 2004 at 9pm |
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.)
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.
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.javaSimple.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
This may take a while. It will produce a file called
Simple.class which is
the compiled binary form of the Simple class.
javac Simple.java
To run Simple, type:
java SimpleYou should see a welcome message.
For more documentation on compiling and running code from the command line, see the Problem Set Procedure page.
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.
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.



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.
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:
Find the documentation for nextInt(int n) and read it. You don't have to understand all the details of its behavior specification, only that it returns a random number from 0 to n-1. You should use this method to choose your greeting.
To handle choosing which random greeting to display, we suggest that you could either use an array of greetings or use a switch statement. The array of greetings approach might look something like:
String[] greetings = new String[5];You would then need to print to System.out the contents of a randomly chosen index from greetings[].
greetings[0] = "Hello World";
greetings[1] = "Ola Mundo";
greetings[2] = "Bonjour Monde";
greetings[3] = "Hallo Welt";
greetings[4] = "Ciao Mondo";
Alternatively, if you were using a switch statement, it might look like:
switch (greetingNumber) {
case 0: System.out.println("Hello World"); break;
case 1: System.out.println("Ola Mundo"); break;
case 2: System.out.println("Bonjour Monde"); break;
case 3: System.out.println("Hallo Welt"); break;
case 4: System.out.println("Ciao Mondo"); break;
}
If you haven't used Java before, or are having trouble with the language or syntax, a good reference is The Java Tutorial from Sun's website.
When you are finished writing your code and it compiles, run it several times to ensure that all 5 greetings can be displayed.
Congratulations – you have written, documented, and compiled
your first Java class!
Any file that you introduce, you must explicity add to your CVS repository. Just adding the file to the repository is not enough; you must also commit it. Furthermore, after making changes to a file, you must commit it so that we, the staff, can collect your most up-to-date version.
Since RandomHello.java and problem4.txt are not in your CVS repository, you must add them. You also want to commit the changes you made to HolaWorld. Instructions for doing so follow:


validate6170 ps0
We strongly encourage you to use the 6.170
Zephyr Instance. The instance
can be used to get in touch with classmates and any lab assistants on
duty.
$Id: ps0.html,v 1.56 2004/02/04 23:12:18 jaz Exp $