CS 117: Introductory Labs

Lab #0: Starting Out
Goal: To get a basic program in BlueJ compiled and running.

1. Logon to the computer.
2. Double-click on the BlueJ icon.
3. Once BlueJ has started, click on "Project", then choose "New Project".
4. In the "File name" box, type "introlab", then click the "Create" button.
5. You should see a project window in front of you. Click on the button "New Class", and enter "HelloWorld" in the box titled "Class Name". Click "OK".
6. You should now see a rectangle in the project window representing the class HelloWorld. Double-click on it to see the Java source code associated with the class.
7. Modify the Java code for HelloWorld so that it looks like the following:

    import javabook.*;
    /*
        This is an introductory first program.
        Hopefully it works just fine!
    */

    class HelloWorld
    {
        public static void main(String[] args)
        {
            System.out.println("Hello world!");
            System.out.println("This is my first Java program!");
        }
    }

8. Click the "Compile" button, and wait for it to display an error message at the bottom or the text "Class compiled - no syntax errors." If there are any errors, fix them and recompile.
9. When your class has compiled correctly, find the project window and right-click on the HelloWorld class rectangle.Chose "void main(args)", which sends a message to the class to run the method called main. When the "BlueJ: Method Call" window pops up, just click "OK" to send no arguments to the method.
10. If all went successfully, the BlueJ Terminal Window should pop up and you should see the text "Hello world!" inside it. Congratulations!
11. What do "/*" and "*/" do?
12. Change the first appearance of the word "println" to "print". What happens?
13. Close the "Terminal Window" and the "Hello World" Java code window. Exit BlueJ by clicking on the "Project" menu, then clicking "Quit."

Lab #1: Using a pre-existing class
Goal: To instantiate an object from a class in the Javabook library.

1. Start up BlueJ again.
2. Click on "Project", then "Open Project...".
3. Click "introlab", then click "Open."
4. Click on the button "New Class", and enter "FunTime". Click "OK".
5. Double click on the class FunTime to pop up the Java source code window.
6. Modify the Java code for FunTime so that it looks like the following:

import javabook.*;

/*
     This program will allow you to draw a picture by dragging a mouse while
     holding the left button. To erase the picture, click the right mouse button.
*/

class FunTime
{
    public static void main(String[] args)
    {
        SketchPad doodleBoard;          // Declare SketchPad object named doodleBoard
        doodleBoard = new SketchPad();  // Create the SketchPad object
        doodleBoard.setVisible(true);   // Make doodleBoard visible
        doodleBoard.toFront();          // Make doodleBoard appear in front
   }
}

7. Compile your program and fix errors until it compiles successfully.
8. Right-click the "FunTime" class in the project window and choose "void main(args)" to run the program.
9. If all goes successfully, you should see a SketchPad window in front of you. Have fun with it! When done, close the window to end the program.
10. What does "//" do? How is it different from "/*" and "*/"?
11. Close the Java source window, if still open, and leave just the Project window remaining.

Lab #2: Creating your own output
Goal: To start designing your own output.

1. Create a new class called "DisplayMessage".
2. Edit the Java code associated with the class so that it looks like the following:
    import javabook.*;
    /*
      This program will pop up my windows for the world!
    */
    class DisplayMessage
    {
        public static void main(String[] args)
        {
            // Declare, create, and display a Window
            MainWindow mainWindow;
            mainWindow = new MainWindow();
            mainWindow.setVisible(true);
            mainWindow.toFront();

            // Declare, create, and display a MessageBox
            MessageBox messageBox;
            messageBox = new MessageBox(mainWindow);
            messageBox.show("I Love Java!!!");
        }
    }

3. Compile and run the program. Click "OK" to the message "I Love Java!!!", then close the frame window.

Lab #3: Some basic arithmetic
Goal: To see how basic arithmetic operations behave.

1. Create a new class called Arithmetic. Copy and paste the code from introlab3.java into the Java source code window.

2. Compile and run the program. Enter integers when the program asks you to. What does the operation "%" do? How about "/"? (Try running the program several times with different input.)

3. Now change the type of a and b from "int" to "double", and change the two "getInteger" method calls to "getDouble".  Recompile.  How does the behavior of "%" change?  Get rid of the "%" line and recompile.
Question 1: How does the behavior of "/" change?
Question 2: The type "double" is badly named--what does it mean in this context?

4. Suppose you want to "comment out" a block of code that's bugging you. That is, you want to make the compiler ignore some code without actually removing the code from your file. The /*...*/ style comments give you a convenient tool for doing this.  Try commenting out everything in main.. Recompile.
Question 3: What happens, and why?  How can you fix the problem?
Question 4: Which style of comments is appropriate for documentation in your code, and which style of comments is appropriate for commenting out blocks of code at a time?

5. Put the answers to Questions 1-4 in documentation at the top of the program. Add the names of all students in your group.

6. Submit this program electronically. To find out how to do so, go to the class home page at http://www.mathcs.carleton.edu/faculty/dmusican/cs117w02 and click on the link "Electronically submitting your work."

Lab #4: Putting it all together

Create a class called "Temperature" with a main method that asks for a temperature in Celsius and prints out the terperature in Fahrenheit. Use InputBox for input and OutputBox for output. The formula to convert Celsius to the equivalent Fahrenheit is

fahrenheit = 1.8 x celsius + 32

Make sure to add the names of all students in your group to documentation at the top of the program, and electronically submit your work.