CS 5 Fall 2009
Short Assignment #1
Due Friday, September 25

This Short Assignment is a bit wordier than will be typical. It also has a lot of technical detail about installing the Eclipse 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.

Things to do to get started

  1. If you choose to purchase the textbook, get it from either of the bookstores.

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

    If you are using one of the Macs in 005 Sudikoff, skip to item 4.

  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 my home folder on my Mac.

  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. I chose the name "cs5proj." You should get a screen that looks like this:

    Picture of New Java Project dialog

    Click the "Finish" button.

  7. At this point your screen should look like:

    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: 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 get a green (or black and white) "+" in a circle, meaning that the code will be copied to the src part of your cs5proj folder. Make sure that the green (or black and white) "+" is showing before you drop the file (release the mouse button). If you click on the triangle (Mac) or plus-sign (Windows) next to "src" you will see the following:

    Picture Eclipse Workspace with Default Package

    Click on the triangle (Mac) or plus-sign (Windows) next to "(default package)" and you'll see

    Picture of Eclipse after Mystery.java added to cs5proj

    You may remove Mystery.java from your Desktop, or wherever you saved it to, if you like.

  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. 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 with a simple text-based logo.

    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, 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 "Logo.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 ("Logo.java" in our example.) Type in the logo program, with a logo of your choosing. (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 "Logo", then you should type "Logo" rather than "Logo.java." Don't worry; Eclipse will create the file Logo.java for you! If the name of the class is Logo, the window will look as follows just before you click "Finish."

    Picture of Logo.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.

    Your program may not run correctly the first time. If that's the case, then you have had an early initiation into the world of programming. Errors are common in programming. We all make them. Even me. Fixing mistakes is a significant part of developing programs. If you have an error, check the syntax of your program carefully. Are the semicolons, curly braces, parentheses, and quotation marks correctly placed? Did you misspell something?

    If you need help, you can contact a Section Leader, a TA, or me to get help. Remember that if you blitz cs5help@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. 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 cs5proj folder within the folder that you selected as your workspace. (For example, on my Mac, it's in /Users/thc/Documents/workspace/cs5proj/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 logo won't print with the 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 print that window. Alternatively, you can take a screenshot and print 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.)

Reading

Your textbook, Java Software Solutions: Foundations of Program Design, Sixth Edition, by John Lewis and William Loftus, contains a lot of good reading. Some of it is background information that you will read very lightly. Some is more important. The book contains self-review questions, which you might want to try as you read it. There are also brief chapter summaries at the ends of chapters; you will find these summaries very helpful, both for deciding what is really important, and for subsequent review as exams appraoch. Spend a little time now to see how the book is laid out.

Remember that the schedule page tells you what reading to do before each lecture. We expect you to do the reading.

Read Chapter 1 and Sections 2.1-2.3 for Thursday. Yes, it's a lot of reading, but Chapter 1 doesn't have much detail.

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. Unlike mixed martial arts, programming is not a spectator sport.

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

  1. Run the Java program Mystery.java, and write down what you observe.

  2. Write a Java program that prints out a logo containing your name. I chose the following: ********* * * * * * * * * * * ******* *** Thomas * * ** * H. * * * Cormen * * * * * * *** Please feel free to create your own design. (As I often say, "I don't know what I like, but I know art.")

    The program should be very simple, essentially consisting of just a few System.out.println() statements within your main method. In this example, the third statement might be

    System.out.println(" * * *");

Note

"Turning in" Exercise 1 consists of writing down what the Mystery.java program did. For Exercise 2, you should write the program, get it working, and print out both the program and its output. You can print the program by first making source that the panel with your source code is active, and then using the "Print..." command in the File menu to print the central panel. Print the output in the Console window by copying the output, pasting it into a word processor as described above, and printing the word processor window.

You will not turn in this short assignment electronically. In fact, you will not turn in any short assignments electronically. Short assignments are always turned in as hardcopy. It is only the lab assignments that you will turn in electronically (and on hardcopy as well).

Collect your printouts, and put them in an envelope that is at least 8.5 x 11 inches, and write your name on the outside of that envelope as well. We will not accept your assignment if it is not in an envelope. If you cannot afford an envelope, please see me or a TA, and we will get you one. Leave the envelope in the CS 5 HW HAND IN boxes in the lobby outside 008 Kemeny by the start of class on Friday. (Since you won't know which section you're in at that time, you can put them in any one of the CS 5 HW HAND IN boxes that has a Section Leader's name below it.) And have fun.

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!!


Back to Short Assignments
Thomas H. Cormen <thc@cs.dartmouth.edu>
Last modified: Mon Sep 14 11:02:44 2009