home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
lab13a
using eclipse
BlueJ and Java are two separate things:
- BlueJ is an “integrated development environment” -- a
program designed to help you write Java programs.
- Java is the language -- it's the code you write inside the various windows of BlueJ.
1
BlueJ helps edit and indent your program,
and it calls Java to create individual objects and put them on the bench,
and it also converts the code pad into a small Java program, and then has Java run that
program every time you type something there.
Other IDEs
Beside BlueJ, there are other Integrated Development Environments
which you can use to write your program.
While BlueJ strives for a simple, uncluttered interface (at the expense
of not including every bell and whistle),
IDEs such as JBuilder and eclipse and have more complicated interfaces, which contain
more advanced features.
Note that in nearly every IDE, there are two initial tasks:
Create a project, and then create a file within that project.
Note that we've recently started using multiple classes in the
same project
(BankAccounts and Banks,
e.g..)
Eclipse
Eclipse is an IDE which is geared more towards industrial programmers,
and so its plethora of menu options can be a bit overwhelming.
But its help with indenting and typing alone is a huge draw.
Note that Eclipse is not geared towards Java alone; it can
be used for almost all programming languages.
In ITEC120, ITEC220, and later classes,
no particular IDE is required,
but many students prefer to use eclipse.
Eclipse is free, and can be downloaded from
eclipse.org.
- Start eclipse: From Windows,
Start > Programs > Radford University Course Software > College of Science and Technology > open-source Java IDEs > Eclipse > Eclipse (whew!)
- Before we can write a class file, we need to
create a project (just like we did in BlueJ).
From within eclipse, New > Project… > Create Java Project,
and enter a project name like “lab13a-eclipse”.
- From the main window, click on the arrow, “go to workbench”.
- We'll write a brand new class over the next several steps;
here's a quick preview:
Fields and Methods for class Dog:
Every Dog will have two fields:
an age, and a sound they make (like "woof" or "arf").
What behaviors (methods) will a dog have?
In the steps below, we'll write (with help from eclipse):
- A constructor.
-
getters and setters for our fields.
-
A method speak, which
has a dog return its sound repeated twice, with a comma in between:
for example "woof, woof"
if the dog's sound is currently "woof",
or "arf, arf" if the dog's sound
is currently "arf".
-
Finally, a method getAgeInDogYears,
which returns its age multiplied by DOG_YEARS_PER_YEAR
(a named constant which is whose value is 7).
Choose New… > Class, and type in the class name,
“Dog”.
You can leave the other options as-is.
Ignore the message “use of the default package is discouraged”;
messages that make no sense to us as intermediate Java programmers is
the price
we're paying for the use of eclipse.
-
Declare two fields — sound and age.
Purposefully mis-indent these lines.
-
Type control-A (to select-all — this is a keyboard shortcut
common to all Windows programs, not just eclipse) (mac: command-A).
Then, choose Source >Correct Indentation.
Cheer!
- It gets better:
choose Source > Generate getters and setters….
Click the check-boxes for both fields, and make sure the
box Generate method comments is also selected.
-
Generate Constructor from fields…, and accept the default suggestions.
If you look at the generated code, it looks like what we expect,
except that the first line is super();.
We'll cover what that means next week. (You can delete that one line,
if it annoys you.)
-
While your cursor is inside that method,
choose Source > Generate element comment.
You still need to fill in the meaning of the parameter names, but
eclipse helps do those bits of the comments which are automatic.
-
Start typing the code for speak,
(which returns “arf, arf.” or “woof, woof.” or
whatever, based on the current value of the field this.sound).
Notice how eclipse actually helps you finish your method-names;
you can hit return to accept its selected guess for finishing the method name.
(Ctrl-space can also triggers auto completion;
you can hit Return to choose the top suggestion.)
-
Hitting TAB in eclipse will move you to the next part of code that needs
to be filled out.
-
If you start writing a method which requires inputs -- like
setAge -- then after reaching the open-paren of the method call,
eclipse reminds (via a tooltip) which argument
you're currently in the middle of typing.
-
Note the list of methods/fields/etc on the right -- just click to go see that
definition.
- Try clicking on the little round minus-signs circles, in the left margin
of your text. Neat!
- Note anything which is underlines -- that's a compiler error message.
(Eclipse re-compiles the program, with every keystrock you make!)
Watch the lower-left corner of the window, with the text of the error message.
You can also hover over the red-underline to see the message,
and if you see a light-bulb in the left hand margin, you can see what eclipse's ideas
are, for fixing your mistake.
-
Eclipse does have version of the Code Pad, although it's a bit clunky.
New > Other… > Java > Java Run/Debug > Scrapbook Page.
You can type in expressions into this "scrapbook page".
Then, select the entire text (all lines),
and click on the magnifying-glass button ("evaluate expressions")
found in to the upper right of the scratch book.
For more information on using eclipse's Scrapbook pages,
you can download
tutorial #3 (video).
Eclipse rocks!
Other suggested features to play around with, in eclipse:
-
refactor > rename;
-
To comment or uncomment large sections at once,
select several lines, and choose Source > Toggle comment.
-
As you're typing the code for getAgeInDogYears(),
I suddenly wanted to refer to the constant DOG_YEARS_PER_YEAR.
I typed it in, it was immediately underlined as an error (since I hadn't actually
yet introduced that name).
The light-bulb in the left margin appeared, and clicking on it
yields the first option “Create constant DOG_YEARS_PER_YEAR”.
Choosing that option creates the named constant!
(It is prefixed with modifiers “private static final”,
none of whose meanings we understand yet; you can rip those out or leave them
there, as you like.)
You can also find other
eclipse tutorials
on the web.
EBEHET: Experimental BlueJ/eclipse Hybrid Editing Technique
We'll still need to use BlueJ for another few weeks, to make our test cases,
and to call specific methods.
If you want to leverage all of eclipse's great features, you can try using both.
The following approach is a bit experimental;
please report on the discussion boards,
what works well and what doesn't, when trying to switch back and forth
between the two.
- Start your project in eclipse,
to put down your fields, getters/setters,
and constructors.
Once you've started a project in BlueJ, it's difficult to
get eclipse to use it.
-
Write stubs for all your methods in eclipse,
and generate their javadoc. Write the comments.
-
Start up blueJ, choose Project… > Open NonBlueJ…,
and select the folder you used as your eclipse-workspace.
Note: if you keep both BlueJ and eclipse running,
then at this point,
both BlueJ and eclipse think they own the java file!
This actually works surprisingly well; both programs notice when the file changes
on the disk, and often update themselves to include the change!
To be safe, when switching IDEs, be sure to save the file in one,
then revert-from-disk (or, close and re-open) the file in the other IDE.
Still, this is risky overall.
I recommend, every few minutes, Save as... your file to a backup name,
so if something goes horribly awry, you won't lose more than 5min or work.
- In BlueJ, create a test class (as we've seen).
(Oddly, you might need to compile this test class, to have it turn green.)
You can create objects, move them to the test fixture, record test cases
and assert what the correct answers should be.
-
Go back and write your methods in eclipse;
then open your BlueJ project (it should see the changes made)
and run your tests.
What is Programming?
A note on philosophy of teaching:
So far, in BlueJ,
you have (hopefully) understood every word in your programs.
Programming is not about getting arcane incantations just-right;
it's about decomposing a problem into smaller bits.
Writing programs that require all sorts of keywords gives the wrong impression
about what programming is.
BlueJ has helped keep things on track.
Eclipse, on the other hand, is aimed at professionals, not at beginners.
It's a great tool, which does a lot of great things.
It also injects many new java keywords into our program.
They are keywords which actually should be there, once we understand them.
You may leave them or delete them as you like.
Just don't be confused about what's difficult about programming:
it's not learning all the keywords;
the truly difficult part is learning how to take a big problem,
and decompose it into small stand-alone tasks, which all cooperate to
give the big result.2
comparing IDEs
Uncommon, Good features of BlueJ:
- You can call any method, and create any object.
Most IDEs only let you call a class's public static void main(String[] args), and you can't make objects or call methods without writing a
test driver.
- Code Pad.
This rules! Otherwise, if you wanted to see what
"hello".comparesTo("Hello") returns, you'd have
to write an entire function for that.
- Inspect, from an object's pop-up menu.
To be able to have several objects and see what is inside each one
and how the innards change as you call methods is a great, novel feature.
- When learning,
it provides a visual reminder of the difference between classes and
methods, and the fact that static methods/variables
are accessed through the class, not through individual instances.
-
By offering only a stripped-down set of features, it avoids
overwhelming beginners with clutter.
Uncommonly poor features of BlueJ:
- It can't indent worth a dang.
- If there's a compiler error,
it does highlight the offending line, but doesn't
tell you which character is where the error is first noticed.
- Not much editor support for typing code (see strengths of
other IDEs, below).
- Slightly buggy.
I occasionally have to re-start BlueJ to get something to work;
sometimes javadoc-generation fails.
Features of BlueJ that are okay, compared to other IDEs:
- Compiler error messages are often extremely uninformative.
These messages are actually generated by the underlying compiler,
and all the different IDEs tend to use the same compiler,
so they all share this flaw equally.
(And the solution is, compilers should give more human-intuitive
error messages! This is an HCI (Human-Computer Interaction) problem.)
- javadoc generation.
I don't like how I can't view the program simultaneously as looking
at its javadoc3,
but I like the emphasis that there are two fundamental views of
a class: as an implementor, and as a user
(where the interface is everything).
- The debugger is fine, although it doesn't allow more sophisticated
behaviors like
“stop at line 20, only if numItems > 20”
or
“stop at line 20 the fiftieth time you reach it.”.
These are rarely needed features, but can be important when you do need them.
Some strong features of other IDEs, like eclipse:
- Always compiling your program in the background,
to quickly catch mistakes.
- better auto-indenting (e.g. in Eclipse, an correct indentation
menu item; you want to select your entire program text before
choosing this item, and I think it should be automatic;
maybe there is some way to set that?)
- When you type an open-brace or open-paren, it will
immediately generate the matching close-brace/paren.
- Automatically generating getters and setters for fields.
- Automatically generating javadoc for getters and setters
- When starting a javadoc comment,
Eclipse will auto-fill in the names of any existing parameters.
- You can customize the font-coloring of keywords, constants, etc..
- pop-up menus for the names of methods and class libraries.
On the other hand,
I know of no other IDEs with BlueJ's above-listed strengths.
All in all the strengths outweigh the weaknesses, especially when learning Java.
However, that doesn't mean you should ignore its weaknesses.
It helps to know other IDEs in addition to BlueJ.
1
Actually, there are two parts to writing a Java program:
-
writing the actual java source code for your program, and
-
When you press the compile button, your Java program
is translated to an intermediate language: Java byte code
(which is closer to the ones and zeroes the computer hardware actually uses,
but byte code is actually hardware-independent).
-
Finally, you run your java byte code on a “java virtual machine” --
a program which looks at successive lines of java byte code,
and quickly translates each line into instructions which your specific computer
platform can run.
BlueJ sits inbetween parts (a) and (b).
↩2Writing an English essay is the same skill:
taking a large point,
and separating it into different sections and paragraphs which each
stand on their own, yet contribute to the overall effect.
It's all logical, structured thinking, which is one of several main
modes of thought learned in college, and taught in nearly all departments.
↩
3Actually, I just realized this is no problem:
If you choose BlueJ's “document project”, it pulls
up the documentation in a separate web browser. ↩
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive