CS 117: Introductory Labs

Lab #0: Using a pre-existing class
Goal: To construct an object and run a few methods.

  1. Log on to the computer.
  2. 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. In the menu bar at the top of the screen, click "Edit", then "Add Class from File...".
  6. Click on the button next to "Look In" that has your user name on it, and choose "Accounts" from the drop down box. Then double click on the folder next to "courses", then "cs117", then "dmusican". Finally, click on the file titled Canvas.java. (In short: your goal is to find the file /Accounts/courses/cs117/dmusican/Canvas.java.)
  7. You should now see a rectangle representing the class Canvas in your project window. Right click on it, and choose "Compile."
  8. Construct a Canvas object by right clicking on the class Canvas and choosing "new Canvas()".
  9. In the window that pops up, give your instance a name by typing myCanvas for the "Name of Instance", and click OK.
  10. You should now see a an object called myCanvas on the object workbench. Right click it to see all the methods that you can run. One of these methods is "setVisible". Choose the method setVisible, then type the word true in the method call box. Click OK. You should now see a Canvas window appear on the screen.
  11. Experiment with the other methods that you can get by right clicking, and see what they do.
  12. Create a second Canvas object, named anotherCanvas. Make it visible, and draw some pictures.

Lab #1: Creating your own class
Goal: To get a basic program in BlueJ compiled and running.

  1. Continue working in the same BlueJ project as earlier. Click on the button "New Class", and enter "HelloWorld" in the box titled "Class Name". Click "OK".
  2. 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.
  3. 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 void display()
        {
            System.out.println("Hello world!");
            System.out.println("This is my first Java program!");
        }
    }

  1. 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.
  2. When your class has compiled correctly, find the project window and right-click on the HelloWorld class rectangle. Choose "new HelloWorld()". Give it a name of your choosing for "Name of Instance", or just use the default. You should see a new object from the HelloWorld class on the object workbench.
  3. Right click the object and choose "void display()", which will run the method you just wrote. If all went successfully, the BlueJ Terminal Window should pop up and you should see the text "Hello world!" inside it. Congratulations!
  4. Question to consider: What do "/*" and "*/" do? 
  5. Change the first appearance of the word "println" to "print". What happens? 

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

1. As in Lab #0, click on "Edit", "Add Class From File...". Choose the file /Accounts/courses/cs117/dmusican/Arithmetic.java. Double click the class rectangle that appears and examine the Java program inside.

2. Compile, create an Arithmetic object, and and run the method doSomeArithmetic. Enter integers when the program asks you to.

3. What does the "int a, int b" in parentheses after "doSomeArithmetic" do?

4. What does the operation "%" do? How about "/"? (Try running the program several times with different input.)

5. Now change the types of a and b from "int" to "double".  Recompile and rerun.
Question 1: How does the behavior of "/" change?
Question 2: The type "double" is badly named--what does it mean in this context?

6. 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 doSomeArithmetic. Recompile and rerun.
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?

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

Lab #3: Putting it all together

Create a class called "Temperature" with a method called convert that takes a temperature in Celsius and prints out the temperature in Fahrenheit. 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,

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