CS 10: Winter 2016

Short Assignment 0
Due: Wednesday, January 6

This short assignment is a bit wordier than will be typical. It also has a lot of technical detail about installing the course 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 assignments you may use either IDE (or both IDEs, one for developing the code and the other for playing with it and debugging it). I used DrJava when writing the sample solutions for sa-1 and sa-2 and Laboratory 1, and Eclipse for later short assignments and labs. Both will be installed on Sudikoff computers, if you prefer to use them there.

Things to do to get started

  1. Get the textbook from either the publisher or Amazon.

  2. Familiarize yourself with the layout of the CS 1o web site. All materials for the course, other than the textbook, will reside on the website, www.cs.dartmouth.edu/~scot/cs10/syllabus.html or on the course Canvas site.

  3. Install Java 8 and Eclipse on your computer:

    You can also find instructions on the Software tab of the course website.

    Pay careful attention to the instructions. Follow them exactly. Do not skip steps. From here on, we'll assume that you have installed Java 8 and Eclipse correctly.

  4. If Eclipse is not already running, launch it. You should see a window like this:

    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.)

    Here, /Users/scot/Documents/workspace is my workspace 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:

  6. Fill in the project name. I chose the name cs10proj. You should get a screen that looks like this:

    Click the Finish button.

  7. At this point, your screen should look like this:

  8. Click the triangle next to cs10proj. You will see the following:

    "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 (while holding Option on a Mac) and save the file onto your hard drive. 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:

    Click on "OK". You are telling Eclipse to make a copy of the file Mystery.java in the folder you selected as your workspace. For example, in my system, Eclipse has made a copy of Mystery.java within the folder /Users/scot/Documents/workspace/cs10proj/src/.

    At this point, you should see something like this:

    If you don't, then click on the triangle 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." Select "NewJava Application." in the continuation menu. This will cause 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 action 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. In a plain text file, tell us 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). Doing so 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 that 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:

  13. Next, remove Mystery.java from the project. To do so, 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, followed by a blank line, followed by a haiku about programming.

    In case you have forgotten, a haiku is a poem in three lines. The first and third lines have five syllables each, and the second line has seven syllabus. No rhyming is required.

    The haiku you write should be about computer programming. Feel free to make one up, or you can easily find haikus about programming on the Internet. Here's the haiku I made up:

    When in CS 10
    Starting assignments early
    Pays off in the end

    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 cs10proj, you will see that cs10proj/src has already been chosen as a parent folder; that's just what you want:

    You then need to type in the name of your new file next to where it says, "File name:." Choose Haiku.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 (Haiku.java in our example.) Type in the haiku program, with your name, a blank line, and a haiku of your choosing. (See the explanation below under Exercises.) 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 Haiku, then you should type "Haiku" rather than "Haiku.java." Don't worry; Eclipse will create the file Haiku.java for you. If the name of the class is Haiku, the window will look as follows just before you click Finish.

    If you create the Haiku class this way, then a class declaration and a declaration of the main method will be provided, and you must fill in the bodies. You might see that they're in a package named cs10proj; that's OK.

    You might have noticed a subtle difference between the parameter in the main method that I've written and the parameter provided when you choose File -> New -> Class. In the code I write, the parameter is written as String args[], but in the code Eclipse creates, the parameter is written as String[] args. The difference is where the empty square brackets go. It turns out that in this case, it doesn't matter whether they go after the type String or the parameter name args, and it wouldn't matter even if we really were going to use the command-line arguments. Later, we'll see situations in which where you place the empty square brackets does matter.

  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. You've programmed before, and so you've made many an error in the past. If you have an error, check the syntax of your program carefully. Are the semicolons, c urly braces, parentheses, and quotation marks correctly placed? Did you misspell something?

    If you need help, you can contact the TA, a section leader, or me to get help. Remember that if you blitz , 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 an 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 attachment. Your program is found somewhere in the src folder within the cs10proj folder within the folder that you selected as your workspace.

    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. I personally use TextWrangler on the Mac, which is free on the web.) Alternatively, you can take a screenshot. (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.)

  17. Install Dr. Java on your computer. You can get it by going to drjava.org and chosing the appropriate download in the section "Current Stable Release". For Windows it is easy - choose the Windows app and unpack the file. Unpack the file and you have the DrJava application. (You can then throw away the downloaded file.)

    Unfortunately the Mac version of Dr. Java does not work with Java 8 (which you just installed). (If you have Java 6 installed from earlier, you can use the app.) So instead you download the .jar file. Put it in your CS 10 folder (or some useful place) and double-click on it. If it runs, fine. If you have the privacy and security settings that you probably should have, you will get the following window:

    Click "OK" and go to "System Preferences" under the Apple menu. "Show All", and click on "Security & Privacy". You should get a window that looks like:

    Click on "Open Anyway". You may get a warning:

    Click on "Open" and you should be all set. Having done this once, Dr. Java should open whenever you click on its .jar file.

    After you have installed Dr. Java you need to also install the java source and media libraries from the Media Computing site. The files that you want are java-source and media-sources.zip. (These are reduced versions of what is available 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.

    You now need to tell DrJava how to find the code in the java-source directory. Use the Preferences in DrJava to add the java-source directory to the list of locations where DrJava looks for unknown classes. (This list is called the classpath.) Then click on APPLY and OK. (This is a simplified version of what appears on pp. 18-20 of the on-line textbook here. The description there tells you how to install music and instrument files, which we will not use.)

    Quit and re-start DrJava. Then 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-sources directory and choose beach.jpg. Play with the picture if you like. Note that if you click on the picture the pixel location and color (given as a red value R, a green value G, and a blue value B) are given 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.

Reading

Your textbook, Data Structures and Algorithms in Java, 6th edition, by Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser, contains a lot of good reading. Some of it is background information that you will read very lightly. Some is more important. Spend a little time now to see how the book is laid out.

Remember that the schedule page tells you what reading to do for each lecture. I suggest reading it before the lecture, but some people prefer to read it after the lecture.

For Wednesday, read Chapter 1 and Sections 2.1–2.3. Yes, that's 81 pages of reading. You thought you could escape lots of reading by taking a Computer Science course?? Hah!

Exercises

Here's what you should do for Wednesday. Remember that short assignments are due at 10:00 am. You submit them as a .zip file via Canvas.

  1. Run the Java program Mystery.java, and write into a plain text file what you observed.

  2. Write a Java program, Haiku.java, that prints out your name, a blank line, and a haiku about programming. Here's the output of my program:

    Scot Drysdale
    
    When in CS 10
    Starting assignments early
    Pays off in the end.
    

    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("When in CS 10");

    Submit your Haiku.java file and the output of your program.

  3. Submit the screen shot of beach.jpg that you got from running the explore call in Dr. Java.

You should also fill in Questionnaire.doc and section-times.doc and email them to the appropriate addresses.