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 110 work.

It makes sense to keep your CS 110 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 CS110 (or something similar) on your M: Drive. Throughout this course, you will be creating additional sub-folders in your CS110 folder (or whatever you chose to call it).

If you are working on your own computer, you may create the folder wherever it makes sense on your computer.


Exercise 2: Download and modify a trivial Java program.

An object-oriented program is, as we learned in class, 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 CS110 folder.
  2. Unzip the downloaded project.  Navigate to your CS110 folder and double-click on the TrivialJavaProgram.zip file. On a PC, you will then have to 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. Start up BlueJ.  (If you are on a College computer, click on the "Start" menu at the lower left-hand corner of your screen.  Move the mouse up to the "All Programs" menu name, then move the mouse to the right until a pop-up submenu appears.  In the list of alphabetized application folders in this submenu, move the mouse over the word "BlueJ", then move the mouse right again and select the "BlueJ" application from the second pop-up submenu.)
  4. Open the TrivialJavaProgram project.  In the BlueJ "Project" menu, select "Open Project."  In the navigation window that appears, navigate to your CS110 folder, choose TrivialJavaProgram, and click on the Open button.  (The TrivialJavaProgram project may be in a TrivialJavaProgram folder, in which case you will have to double-click on the folder first and then on the project of the same name within the folder.)
  5. 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.  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.
  6. Compile and run the TrivialJavaProgram project using the instructions in the README document.
  7. 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 parentheses, rather than "(your name)".  Leave the "(with assistance from)" phrase there for now.  Change the date on the "@version" line to today's date.
  8. Compile and run the TrivialJavaProgram project.
  9. Further modify the TrivialProgramClass class.  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.
  10. Update and view the class documentation.
    1. Update the AUTHORS and USER INSTRUCTIONS sections in the project README file, since your program no longer does nothing. 
      Note: The README file is an example of external documentation because it's outside the code.
    2. Edit the TrivialProgramClass Java source code to update the initial description of what the program does and to document your changes in the Modifications section of the class documentation.  If you received assistance from anyone, document that in the "@author" line; otherwise, remove the "(with assistance from)" clause. 
      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).
    3. 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.
  11.  

    Submit your modified files for this exercise to Moodle.  When you are done making your modifications for this exercise, submit the project README file and the Java source code for the TrivialProgramClass class (2 files) on Moodle.  (If you have not already joined the class on Moodle, you should do so now.) You will submit a third file after finishing the last exercise in this lab.
    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 3: 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.  


Exercise 4: Download and save class templates for future use.

When you create new projects and new classes in BlueJ, the development environment will provide you with some sample code and documentation in the project README file and in the classes.  The documentation conventions for this course, however, are slightly different from the documentation that is provided automatically.  The JavaTemplates project contains a project README file and two sample classes with documentation corresponding to the conventions for this course.  One class (MainClassTemplate.java) contains a main method, while the other is a "normal class" (NormalClassTemplate.java) for creating objects. In future projects, you will be asked to copy and paste the contents of these classes into new classes as starting points.

  1. Download the JavaTemplates project to your CS110 folder for future use and unzip it in the same way that you downloaded and unzipped the TrivialJavaProgram and NumbersAndStrings project above.
You do not need to submit anything for this exercise.  


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