Lab 1: Getting Started with

BlueJ,   Program Documentation,   Numbers and Strings


BlueJ is a Java development environment; in other words, it is a software application that provides tools for reading, writing, compiling, and running Java programs. In this lab, you will:


Exercise 1: Create a folder for your CS 150 work.

It makes sense to keep your CS 150 programs together in a single place, separate from other documents or materials you may have for other classes.

If you are working on K College computers (any computer that is part of Knet):

  1. Double-click on the "My Computer" icon on the desktop.
  2. Double-click on the M: Drive associated with your Knet ID (e.g., k03xx01).  Note: if there isn't an M: Drive associated with your Knet ID under "My Computer,", talk to your instructor immediately!
  3. Create a new folder named CS150 (or something similar) on your M: Drive. Throughout this course, you will be creating additional sub-folders in your CS150 folder (or whatever you chose to call it).

If you are working on your own computer, you may create the folder wherever you will be able to find it easily.


Exercise 2: Download and modify a trivial Java program.

An object-oriented program, as we learned in class, is made up of a collection of classes.  The Java code for each class is stored in a separate file, where the name of the file is the name of the class with a .java extension.  Thus, the Java code for a class called Aquarium must be in the file Aquarium.java.  In BlueJ, the classes for a program are all stored together in a folder. 

We also learned that a running program consists of objects invoking operations on other objects.  How, though, can we start an application when it doesn't have any objects yet to do any operations?  In Java, we can start an application by invoking a special "start-up" operation called main.  (Java operations are called methods, so this operation is called the main method.)  A main method is not tied to any particular object, so the BlueJ development environment (or Eclipse, or whatever software is running the program) can invoke the main method, even though the program doesn't have any objects yet.  The main method then constructs one or more objects and invokes methods on them.  They, in turn, may construct other objects and invoke other methods, which is the essence of an object-oriented program.

We will start by downloading a very simple program with a single class. To keep things simple, the only method this class has is a main method.

  1. Download the TrivialJavaProgram project into your folder for this class.  To download it, right-click (or control-click on a Mac) on the TrivialJavaProgram project link, select "Save Link As...", and, in the navigation window that appears, save it to your new CS150 folder. (Do not just save it to a Downloads folder.)
  2. Unzip the downloaded project.  Navigate to your CS150 folder. Depending on the type of computer you have, you may be able to double-click (or right-click) on the TrivialJavaProgram.zip file to "unzip" or "uncompress" the file. This will create a new folder, also called TrivialJavaProgram. (On a PC, you may have to double-click or right-click and then click on "Extract all files" (under Folder Tasks) on the left side of the Windows Explorer window.  In the Extraction Wizard, just click on "Next" or "Finish" until it is done.)
  3. Open the TrivialJavaProgram project in BlueJ.
  4. Read the README file.  For now, you can ignore the buttons in the control panel or toolbar on the left side of the BlueJ window.  Concentrate instead on the icons in the central part of the window.  This is known as the Class Diagram.  Double-click on the document icon in the upper-left corner of the diagram to open up a README file with information about the project.  Read this document, especially the "HOW TO START THIS PROJECT" section, which describes how to know whether classes in a project need to be compiled (translated to a format that the computer can execute), how to compile classes that need to be compiled, and how to run the TrivialJavaProgram application.
  5. Compile and run the TrivialJavaProgram project using the instructions in the README document.
  6. Read and modify the TrivialProgramClass class.  Double-click on the TrivialProgramClass icon to view the class's Java code in an editor window.  (Or, right-click or control-click on the icon and select Open Editor.)  Modify the "@author" line in the class comments to include your name, without brackets, rather than "(your name)".  Change the date on the "@version" line to today's date.  Leave the "[with assistance from ...]" and "[working side-by-side with ...]" phrases in place for now. (You could even update them, if you have received or given help during the lab.)
    Note: In Java, the source code for a class is always in a file with the name of the class and a .java suffix.
  7. Compile and run the TrivialJavaProgram project.
  8. Further modify the TrivialProgramClass class.  Again, double-click on the TrivialProgramClass icon to view the class's Java code in an editor window.  Modify the main method to print a simple message, as in the following example:
            public static void main(String[] args)
            {
                System.out.println("Hello, world.");
            }
        
    Compile and run the modified program.  Your message should appear in a new window.
  9. Update documentation.
    1. Edit the TrivialProgramClass Java source code to update the initial description of what the program does.  If you received assistance from anyone, or if you collaborated with a classmate working side-by-side, document that in the appropriate "@author" lines; otherwise, remove those lines. 
      Note: We use comments to distinguish internal documentation from actual code. The comments in this class, enclosed in /** … */, are known as documentation comments or javadoc comments. They are used to provide overview information about the class (at the top of the file) and about each method (immediately preceding each method).
    2. Compile and run your program to make sure that you only changed the program's documentation, not its behavior.  Double-check your documentation changes by selecting Documentation in the pull-down menu in the upper-right corner of the editor window.  Scroll down to see the complete javadoc documentation for your class.
    3. Update the AUTHORS and USER INSTRUCTIONS sections in the project README file, since your program no longer does nothing.  (Optional)
      Note: The README file is an example of external documentation because it is outside the code.


Exercise 3: Zip and Submit Your Program.

  1. In preparation for submitting your program, rename the folder containing it to YourName_Lab1. (Do this from the Mac Finder or Windows Explorer, not from within BlueJ.) This will help whoever grades it when they receive a dozen or more projects with similar names.
  2. Zip up the folder. This will create a single, compressed version of your project that can be submitted to Kit. On a Mac, you can do this by right-clicking or control-clicking on the folder and choosing Compress. You can do something similar on Windows, or you can use 7-Zip, an easy-to-use, open source application available from sourceforge.net.
  3. Submit your zipped up folder to Kit.  (If you have not already followed the link from the Kit email to set up your account, you should do so now.)
    Side-Note: If you ever need to print a version of your files, you can do so in BlueJ. You will find the Print option under the Class menu.  Remember that you can go back and forth between the Java source code and the class documentation for a class using the pull-down menu in the upper-right corner of the editor window.


Exercise 4: Experiment with Numbers and Strings.

In this exercise you will be experimenting with numbers and strings.

  1. Download and unzip the Numbers and Strings project.
  2. Compile and run your program to test that it runs and does something.
  3. After you have run the program, clear the output in the BlueJ output window by going to the Options menu and choosing Options Clear. This will give you a clean slate for the next run.
  4. Experimenting with String Concatenation

    1. Open up the source code for the NumbersAndStrings class. Notice that all but the first four lines of code are "commented out" (appearing in comments so they won't run). Look over the four lines of code that are not commented out, and make sure that you understand the connection between those lines and the output you saw when you ran the program.
      Note: The javadoc commenting style we saw before (enclosed in /**…*/) provides overview information about the class and each method. Here we see a second commenting style. The double-slashes (//) introduce a short comment (from the // to the end of the line), usually providing an explanation for the code that immediately follows. We can also use a //-style comment to "turn off" a line of code, so that it will not get executed, as is done here.
    2. In the next "paragraph" (group of statements, also called a stanza), notice that the first line is a true comment
         // Experimenting with string concatenation...
      but the other two lines are commented-out code. Uncomment the two code lines by removing the // symbols at the beginning of the line. (There will still be a comment to the right of the statement, at the end of the line.) What do you think the code will do now? For each line, put what you think the output will be in the comment at the end, similar to the expected output provided for the last line of the previous stanza.
      Tip: In BlueJ, you can easily comment or uncomment multiple lines at once, which is very convenient when developing and testing programs. Drag your mouse or cursor over the lines you want to comment or uncommendt, go to the Edit menu, and choose Edit --> Comment or Edit --> Uncomment.
    3. Run the program, and compare your actual output with your expected output. Did they match? If not, do you understand why not?
      Good Practice: This is good testing technique, as practiced by experts: Always think about what you expect the results to be before running a test, so that any actual differences will stand out more clearly. Whenever actual results differ from expected results, analyze the difference: was the problem in the code or in the original expectation?
    4. After each run of the program, you may want to clear the output in the BlueJ output window (Options --> Clear) to give you a clean slate for the next run.
  5. Experimenting with the + Operator in Strings and Numbers

    Building output with a mixture of strings and numbers is frequently very useful. The five statements in the next stanza will let you experiment with what the + operator does inside and outside strings, and when given two numbers, two strings, or a number and a string.

    1. For each of these statements, indicate what you think the output will be in the comment at the end of the line.
    2. Then uncomment the statements, either one-by-one or all at once, and compile and run the tests. (If you want, you can comment out the earlier System.out.println statements, to lessen the amount of output in each run.
    3. Compare the actual results to the expected results, and analyze any differences to understand how Java handles the + operator in different contexts.
  6. Experimenting with Combinations of Multiple Arithmetic Operators

    The final set of statements in this exercise will allow you to experiment with what happens when addition and multiplication operations are combined in the same statement.

    1. Starting with just the first two statements, think about what you think the output will be, document the expected output at the end of each line, uncomment the statements, and run the experiment.
    2. What about the third statement in the block? Do you expect its behavior to be the same as the first statement or the second statement, or neither? Test it.
    3. Then go on and do the same for the final two statements.
You do not need to submit anything for this exercise.  


Note: When you create new projects and new classes in BlueJ, the development environment will automatically provide you with some sample code and documentation in the classes and in the project README file.  The documentation conventions for this course, however, are slightly different from the documentation that is provided automatically.  In future projects, you will be asked to copy and paste the contents of our CS template classes into new classes as starting points.


When you complete the lab, you can start work on Programming Project #1.