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:
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):
k03xx01
). Note:
if there isn't an M: Drive associated with your Knet ID under
"My Computer,", talk to your instructor immediately!
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.
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.
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.)
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.)
TrivialJavaProgram
folder, double-click on the
package.bluej
file.
Note:
In Java, the source code for a class is always in a file
with the name of the class and a .java
suffix.
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.
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).
Note: The README file is an example of external documentation because it is outside the code.
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.
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.
In this exercise you will be experimenting with numbers and strings.
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.
// 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.
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?
+
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.
System.out.println
statements, to lessen the amount of
output in each run.
+
operator
in different contexts.
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.
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.