RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lab13a
using eclipse

BlueJ and Java are two separate things:

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.

  1. Start eclipse: From Windows, Start > Programs > Radford University Course Software > College of Science and Technology > open-source Java IDEs > Eclipse > Eclipse (whew!)
  2. 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”.
  3. From the main window, click on the arrow, “go to workbench”.
  4. 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.
  5. Declare two fields — sound and age. Purposefully mis-indent these lines.
  6. 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!
  7. 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.
  8. 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.)
  9. 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.
  10. 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.)
  11. Hitting TAB in eclipse will move you to the next part of code that needs to be filled out.
  12. 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.
  13. Note the list of methods/fields/etc on the right -- just click to go see that definition.
  14. Try clicking on the little round minus-signs circles, in the left margin of your text. Neat!
  15. 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.
  16. 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:

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.

  1. 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.
  2. Write stubs for all your methods in eclipse, and generate their javadoc. Write the comments.
  3. 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.
  4. 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.
  5. 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:

Uncommonly poor features of BlueJ: Features of BlueJ that are okay, compared to other IDEs:

Some strong features of other IDEs, like eclipse:

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:

  1. writing the actual java source code for your program, and
  2. 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).
  3. 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.      

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Dec.03 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme