CS 140- Lab 1 Instructions

Thursday, August 28

Objectives

  1. Familiarize yourself with the DrJava interface.
  2. Experiment with the DrJava interpreter and compiler.

Set Up

Normally I recommend that you work in pairs, but today I'd like each of you to work by yourself so that you get set up and used to the DrJava programming environment. After today if you do work in pairs you should each fill out your own lab sheet to get checked off. Also, you should always take turns driving.

Method

Part I - Copy the Course's Java classes to your P: Drive

In this course we will be using some Java code that allows us to interact with media, such as images, sound. What you'll do in this step is to make your own personal copy of these files and you'll change a setting in DrJava to indicate where these extra files can be found. It's not terribly exciting, but important to get right.

  1. Create a folder on your P: drive named CS140.
  2. Copy the folder
            T:\Harcourt\Fall2008\CS140\downloads\cs140Classes
          
    to the CS140 folder you just created.

Part II - Establishing the DrJava Classpath

  1. Next we need to tell DrJava where to find all the Java library we'll use in this course (the folder you just created).
  2. First, start DrJava by going to the Start->All Programs directory and clicking on DrJava.
  3. Next you should see the main DrJava pane which will look something like this:
  4. Bring up the preferences window by browsing to the Edit->Preferences menu. Find the "Extra Classpath" box.
  5. Click on the "Add" button beneath the "Extra Classpath" box. This will bring you to a file selection window.
  6. Click on the drop down arrow in the "Look In:" box, click on the P: drive, and go to the cs140Classes folder you just copied there.
  7. Finally, single click on the cs140Classes directory to select it as the path you want to add. If you accidentally double clicked on the directory, click on the "Up One Level" button (looks like a folder with an arrow pointing up).
  8. Finally, click on the "Select" button. Your preferences pane should now have an additional path in the classpath.
  9. Click on "OK"
  10. Now we'll run a quick test to make sure the path got added correctly. You should now be looking at the main DrJava pane again. Close DrJava and restart it to pick up the new setting.
  11. Copy and paste the program below into the large window in DrJava (the editor pane).
            /*
             * This is my first CS140 Java program. This is a comment
             * that briefly describes what the program does. 
             * 
             * Author: Ed Harcourt
             * Date: 8/21/08
             * 
             */
            public class Lab1
            {
              // This is the start of the main method. Every Java
              // program must have this.
              public static void main(String [] args) 
              {
                // this is where body of the main method starts 
                StdOut.println("Hello world!"); 
              }  // end main method
            }  // end class Lab1
          
  12. Save the file in your P:/CS140 folder under the name Lab1.java.
  13. Click on the Compile button in the upper part of DrJava. It should say Compilation completed. in the Compiler Output tab at the bottom.
  14. Click on the Run button in the upper part of DrJava.
  15. Click on the Console tab in the lower window and see what happened. You should see Hello World!.

Part III - Your First Program

Next, we'll add to our first java program.

  1. Now answer the following questions by modifying the Lab1 program.

    Question 1: What does the following statement do?

                 StdOut.println("Val:" + 1 + 2 + (3 + 4));
              

    What steps does the computer do to create the result?

    Question 2: What is the difference between the statements StdOut.println(1/2); and StdOut.println(1.0/2.0);? Why do they evaluate to different values? What are the values?

    Question 3: Modify the program to calculate and print how much money you will make if you work 40 hours at $13.00/hr and an additional 10 hours at time and a half wage. Let the program do all the calculations. Don't do any of the math in your head or use a calculator. Save the code on your screen for your instructor check-off.

Instructor Check-off

When you are satisfied that you have successfully followed the instructions above, and answered the questions, raise your hand and either the TA or instructor will check you off.


Rich Sharp and Ed Harcourt