CS 117 Lab
Getting started with Java and BlueJ

Follow the instructions below. If you have questions along the way, ask. If you want to try something, go ahead. Whenever there are questions in the lab, answer them to your own satisfaction--you do not need to hand in your answers.

The only thing you need to hand in is in section V below.

I. Hello, World

Goal: To get a simple program in BlueJ compiled and running.

  1. Once you have logged on to the computer, launch BlueJ by double-clicking on its icon on the desktop.

  2. Once BlueJ has started, click on "Project", then choose "New Project".

  3. In the "File name" box, type "introlab", then click the "Create" button.

  4. You should see a project window in front of you, with a single piece-of-paper icon in it. Double-click on the paper icon to see what's in it. We will talk later about how to use this document profitably. For now, just close it.

  5. Click on the button "New Class", and enter "HelloWorld" in the box titled "Class Name". (Note that "HelloWorld" has no spaces in it.) Click "OK".

  6. You should now see a rectangle in the project window representing the class HelloWorld. Double-click on it to see that BlueJ has already written a small amount of Java code for you. This code will give you a starting point for each new class you create with BlueJ.

  7. Modify the Java code for HelloWorld so that it looks like the following:

    
    import javabook.*;
    
    /*
    	The traditional first program.  Once you learn more
    	about Java and objects, you should read the Preface
    	in Wu to see what he has to say about "hello, world"
    	programs.
    */
    
    class HelloWorld
    {
    	public static void main( String[] args )
    	{
    		System.out.println( "Hello, world." );
    	}
    }
    

  8. Click the "Compile" button, and wait for it to display either an error message or the text "Class compiled - no syntax errors" at the bottom of the HelloWorld window. 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. Choose "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."

II. 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. Select "introlab". Click "Open."

  4. Click on the button "New Class", and name the new class "FunTime".

  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. Try it out. When you are done, close the window to end the program.

  10. What does "//" do? How is it different from "/*" and "*/"?

  11. Close the Java source window, leaving only the Project window visible.

III. Output

Goal: To use the MainWindow and MessageBox for 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.*; 
    
    /* 
    	The main method for this class will display a message
    	in a pretty window.
    */ 
    
    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( "Wingardium leviosa." ); 
    	} 
    }
    

  3. 3. Compile and run the program. Once you see the message, close the message box and the frame window.

IV. Arithmetic

Goal: To see how basic arithmetic operations behave, and, as an added bonus, to see how to accept input from the person using the program.

  1. Create a new class called "Arithmetic". Copy and paste the code from introlab.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.

  4. Your a and b variables now have type double. When you run the program, how is the behavior of "/" different from when a and b had type int? The type "double" is badly named--what does it mean in this context?

  5. 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. What happens, and why? How can you fix the problem? Which style of comments is appropriate for documentation in your code, and which style of comments is appropriate for commenting out several lines of code at once?

V. Putting it all together

  1. Create a class called "Temperature" with a main method that asks for a temperature in Celsius and prints out the temperature 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
  2. Put the names of all students who worked on your Temperature class in the comment at the top of your source file. Then, submit the program electronically. To find out how to do so, go to http://www.mathcs.carleton.edu/faculty/jondich/cs117/submission.html.