SA-0, due Sep 18

This Short Assignment is a bit wordier than will be typical. It also has a lot of technical detail about installing the software and using it. It is to get you started off. Future assignments will be more creative and will require less following of picky instructions.

I will be using two Java IDEs (Interactive Development Enviroments) in class. The first is DrJava. It has the advantage of having an Interactions Pane, which allows you to type lines of Java and to have them interpreted interactively. This lets you create objects and play around with them without having to write a main program. For the early lectures this is a great advantage, because we can easily experiment.

The second is Eclipse. This is an industrial-strength IDE. This has advantages. It provides code completion. (You type an object reference followed by a dot, and up pops a list of all of the methods or instance variable that can legally appear after the dot. You pick the one you like.) It provides refactoring. (One example is that you can change a variable name, and it will rename all instances of that variable, but will not change instances of variables with the same name that are declared elsewhere.) It has a powerful debugger and a lot of other useful features. I prefer this for actually developing programs, but it is a lot to learn and deal with.

This assignment asks you to install both IDEs. For later assignments you may use either IDE (or both IDEs, one for developing the code and the other for playing with it and debugging it). Both will be installed on Sudikoff computers, if you prefer to use them.

Things to do to get started

  1. Familiarize yourself with the layout of the CS 10 web site. All materials for the course, other than the textbooks, will reside on the web site. You can access the site through Blackboard, or you can dial direct: http://www.cs.dartmouth.edu/~gevorg/cs10/.

  2. Install Dr. Java on your computer. You can get it by going to drjava.org and chosing the appropriate download (either the Windows app or the Mac OS X App). Use the stable release, not the beta release. Unpack the file (double click on it for a Mac) and you have the DrJava application. (You can then throw away the downloaded file.)

    There appear to be some problems using the download site under some OS-X installations. When you try to run the program you are told that the file is from "an unidentified developer" (The OS does not seem to recognize it as a trusted program when it is decompressed.) If you have this problem, use this simple workaround.

    There is also an alternate way solve the problem. You can enable OSX to run Applications from "Anywhere" in System Preference => Sec & Privacy => Allow Applications downloaded from: Anywhere. Run Dr. Java. Then reset your security settings to the default settings.

    Also install the various media libraries from the Media Computing site. The files that you want are java-source.zip, media-sources.zip, jMusic1.6.3.jar Folder.zip, and inst.zip. (These are from a Georgia Tech educational site, which is here in case you want to know more about them.) Put them in a CS 10 folder somewhere where you can find it. Unpack the .zip files, and change the name of "jMusic1.6.3.jar Folder" to "jMusic1.6.3.jar".

    The way to get all of these files working together is described on pp. 18-20 of the on-line textbook here. Make sure that you update class paths as shown.

    Test your DrJava installation by typing the following code into the Interaction Pane in DrJava:

    String fileName = FileChooser.pickAFile();
    Picture p = new Picture(fileName);
    p.explore();

    The first command will cause a file-chooser window to appear. Navigate to where you put the media-source directory and choose beach.jpg. Play with the picture if you like. Then take a screen shot of that window. (I like using Grab on the Mac. You can use the PrntScrn button on Windows.)

    If you get the error message "Static Error: Undefined name 'FileChooser'" it probably means that you did not set up the class path to java-source correctly.

  3. Install Eclipse on your computer.

    The amount of disk space you will need to do this will vary according to your system. Once you are done installing the components, you can throw away the installers, which will recover a fair bit of space. You can always get the installers back off the file server later, if you need them.

    Pay careful attention to the instructions. Follow them exactly. From here on, we'll assume that you have installed Eclipse correctly, and that you have configured Eclipse per the installation instructions to make look like mine.

  4. Start up Eclipse. You should see a window like this:

    Picture of Empty Workbench

    This image is from Mac OS X. If you're running Windows, you'll see something equivalent. (Mac OS X uses slashes to separate folder names; Windows uses backslashes. Mac OS X uses triangles next to folders that you can expand; Windows uses plus-signs.)

    Here, /Users/thc/ is Tom Cormen's home folder on his Mac. (He is the one who created the images).

  5. From the File menu in Eclipse, select "New" and on the popup menu that appears choose "Java Project." You should get a window like this:

    Picture of New Project dialog

  6. Fill in the project name. Tom Cormen chose the name "cs5proj" when teaching that course. You should probably use "cs10proj", but can choose any name that you like. You should get a screen that looks like this:

    Picture of New Java Project dialog

    (You may select "JavaSE-1.7" instead of 1.6 if you have installed it.) Click the "Finish" button.

  7. At this point your screen should look similar to this:

    Picture Eclipse Workspace with Project

  8. Click the triangle (Mac users) or plus-sign (Windows users) next to "cs5proj." You will see the following:

    Picture Eclipse Workspace with Project Opened

    "src" is short for "source," and it's where you will put programs in order to run them. We call the actual text of a computer program the "source code."

  9. The code that you'll first run is Mystery.java. Click on this link and save the file onto your hard drive. How you do so varies depending on whether you're running Mac OS X or Windows:
    • On OS X, hold the option key while clicking the link, which should save the file your Downloads folder.
    • On Windows running Internet Explorer, right-click the link and choose "Save Target As...".
    Don't save this file into the workspace folder that you made, however. Just save it someplace innocuous, such as your Desktop or leave it in your Downloads folder.

  10. Drag the Mystery.java file from wherever you saved it onto either the word "src" or the icon to its left. You should see a window like this:

    Picture File Operation

    Click on the link that says "Configure Drag and Drop Settings...". You'll see this window:

    Picture Drag and Drop Settings

    Uncheck the box next to "Enable linked resources." Click "OK," and click "OK" again when you get back to the "File Operation" window. You are telling Eclipse to make a copy of the file Mystery.java in the folder you selected as your workspace. For example, in Tom Cormen's system, Eclipse has made a copy of Eclipse.java within the folder /Users/thc/Documents/workspace/cs5proj/src/.

    At this point, you should see something like this:

    Picture Eclipse Workspace with Default Package

    If you don't, then click on the triangle (Mac) or plus-sign (Windows) next to "(default package)."

  11. To run the program Mystery.java, first click on the file name "Mystery.java" in the "Package Explorer" tab. (In other words, click on the file name under "(default package)." The name "Mystery.java" will become highlighted. Then go to the "Run" menu. One of the choices is "Run as." The only choice in the continuation menu should be "1 Java Application." Select it. This causes your program to compile (translate the Java to an internal form called Java byte code), to link (join up with needed libraries), and to run.

    Note that this causes a new tab, "Console," to appear near the bottom of the window. Click on "Console" and you will find a question. Click to position the cursor in the console tab to the right of the question, answer the question, and note what happens. You should see a new window come up with something in it. If you don't see that window, it's probably because your Eclipse is covering it; in that case, move the Eclipse window until you see the new window completely. Write down what you see in that new window and save it in a text file. You do not have to say much. We want to know that you have succeeded.

  12. Double click on the "Mystery.java" name under "(default package)". This will open a listing of the Mystery.java program in the center of the window with "Mystery.java" as a tab above it. When you open a number of different files, each will have its own tab, so you can quickly select any of them. This listing can be edited and saved. This panel is where you will enter and debug your programs. It should look like this:

    Picture of Mystery Listing

  13. Next, remove Mystery.java from the project. To do this select it again by clicking on "Mystery.java" in the "Package Explorer" tab, go into the "Edit" menu, and choose "Delete." When you are asked, "Are you sure you want to delete file 'Mystery.java'"?, click "OK."

  14. Now you will type in a new program that you will write. This program prints your name and some information. In particular, it includes the hours that you could meet on Tuesday or Thursdays for sections and something interesting about you.

    First, click on "src" to highlight it. Then go to the "File" menu and drag down to "New," then choose "File" from the popup menu that appears. You need to select a parent folder. Because you have already selected the default package in src under cs5proj (or cs10proj or whatever you chose), you will see that cs5proj/src has already been chosen as a parent folder; that's just what you want:

    Picture of Parent Folder

    You then need to type in the name of your new file next to where it says, "File name:." Let's assume that you choose "Info.java" as the file name. The ".java" is essential, because the name of the file has to be the same as the name of the class inside it. Then click "Finish."

    The middle of the window will be a tab labeled with the name of your program ("Info.java" in our example.) Type in your program, giving the required information. (See the explanation below under Exercises.) You can model it after the class examples. Note that Eclipse automatically indents your program as you type it, and does things like putting closing braces and parentheses when you type the opening ones. Also, if a line has an error in it, a red error mark will appear in the left margin in front of it.

    You can have Eclipse help you even more by selecting File -> New -> Class instead of File -> New -> File. A screen will pop up. Type in the name and click the box next to "public static void main(String[] args)". When you type in the name, bear in mind that it's the name of a class, not the name of a file. For example, if you want your class to be named "Info", then you should type "Info" rather than "Info.java." Don't worry; Eclipse will create the file Info.java for you! If the name of the class is Info, the window will look as follows just before you click "Finish."

    Picture of Info.java

    If you do this some comments (in a special form called JavaDoc), a class declaration, and a declaration of the main method will be provided, and you must fill in the bodies.

  15. Type in your program.

  16. Run your program by highlighting the file name in the Package Explorer tab and going to the Run menu, as you did with Mystery.java. If Eclipse asks you to "Select the resources to save," just click "OK." Either your program will run correctly (writing to the Console panel) or Eclipse will describe the errors in the program in the Problems panel.

    If you need help, you can contact a Section Leader, a TA, or me to get help. Remember that if you post on Piazza or blitz cs10help@cs.dartmouth.edu, the first available person will respond. If you ask for help by Blitz, please remember to enclose a copy of your program: without it, we cannot tell you what's wrong with it. Remember that when you Blitz a program it must be a Blitzmail enclosure (attachment). Please do not copy and paste your program into the message; we will have a hard time running it if you copy and paste, but it's easy for us to run it if you make it an enclosure. Your program is found in the src folder within the cs10proj folder within the folder that you selected as your workspace. (For example, on my Mac, it's in /Users/gevorg/Documents/workspace/cs10proj/src.)

    When your program execution completes, you'll be left with the Console panel containing the output of your program. We have not yet found a way to print the Console panel. (You'd think that you could click in the Console panel and then choose "Print..." from the File menu, but the good people who wrote Eclipse disabled printing when the Console panel is active. Go figure.) So what you need to do is to copy the output and paste it into a word processor's window (Notepad on Windows or TextEdit on the Mac are easy ones to use, although Word will also work.) Make sure that you use a monospaced font in your word processor; otherwise, the printout will not have same spacing that you see on the screen. (A monospaced font is one in which all characters have the same width, such as Courier.) Then save the output into a file that you will turn in. Alternatively, you can take a screenshot and turn in that. (On Windows, use the PrntScrn button. On the Mac, you can take a shot of just the window you want by using the Grab utility.)

An important reminder

Keep up to date. Do your studying for each class—not just once a week. Do lots of simple exercises on your computer, as suggested in the text. Even when you think you understand how to do something, you will discover that it is still important to actually do it with your own fingers, on your own computer. Programming is not a spectator sport.

Exercises: (these are the "official" things to do and turn in Wednesday)

  1. Install DrJava and the multimedia libraries, and take a screen shot of beach.jpg.
  2. Run the Java program Mystery.java, and write down what you observe in a text editor. Save this in a text file to turn in.

  3. Write a Java program that prints out:
    • Your name.
    • The hours on Tuesdays and Thursdays when you could attend section. Note I am not asking for your favorite time. I want all times that you can reasonably make. The options are: Tue: 8, 9, 10, 11, 1, 2, 3, 4, 5, 6 and Thr: 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 7
    • Something interesting about you.
    The output from my program is: Gevorg Grigoryan OK hours for section: Tue: 8, 9, 10, 11, 3, 4, 5, 6, 7, 8; Thr: 9, 10, 11, 3, 4, 5, 6, 7 Semi-interesting fact about me: I speak three languages. The program should be very simple, essentially consisting of just a few System.out.println() statements within your main method.

Note

You should put the screen shot of the beach picture, your Java program, a file with your description of what Mystery does, and a file with the output of your program into a folder and then compress that folder into a .zip file. (On a Mac you can choose "Compress" under the File menu in the Finder.) Turn them in electronically via Blackboard by 2 am on Wednesday.

Please remember in all assignments that editing the output of the program before printing it is a violation of the Academic Honor Principle. Make sure that the output you turn in comes from the program that you turn in. If you make any change to the code, no matter how insignificant you think it might be, rerun your program to produce new output!!