Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs

lab14: implementing Comparable; other IDEs

Nov.22 (Wed):
You can work on today's lab individually, or in pairs, as you like.

  1. Make a copy of your class Treasure. (Or, you can make a copy of the posted solution (.jar).) This week's lab won't be directly used in hw07, although you'll need the precise concept (implementing an interface) worked on in this lab.
  2. Have your Treasure class implement Comparable. We'll say that one Treasure is “less” than another if it weighs less. Test your compareTo method. (See lecture notes for our example of making class Dog implements Comparable.
  3. Refine your compareTo so that in the case of two equally-heavy Treasures, the Treasure whose name comes first alphabetically is considered “less”. (Remember, Strings themselves already implement Comparable.) If they have the same weight and same name, then compareTo returns 0.
  4. Optional: Implement .equals to jibe with this: two Treasures are .equals if they have the same weight and same name (regardless of their description). Doing this has a good side and a bad side: Note that often we're in this undesirable situation: we'd like to be able to sort a list of Treasures by weight (or by name, or by some other criterion — or by different criteria depending on the context!). On the other hand, implementing compareTo defines a single natural ordering on Treasures, which may put us add odds with .equals. The solution: Don't have Treasures compare themselves, but instead call sort and pass it (as an input) an entirely separate method which (“statically”) takes two Treasures and compares them.
  5. Modify your test-method — which already contains a list-of-Treasures — so that it calls the pre-defined (static) method java.util.Collections.sort, passing it your list. Display the list before and after sorting it. (Be sure to test a list which includes two items with the same weight but different names, Treasures with an empty-string as a name, etc.1.)

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs


Nov.24 (Fri):

Other IDEs

Contents:

A method called main

When we run our program from inside of BlueJ, we can create instances, and call whatever method we like. (Often, we might have a static test-method which we call.) But if we're not inside of BlueJ, and we want to run a program, there is a question: What method should the computer first call, to get your program rolling?

The answer: main. This is just a convention, because without BlueJ we're kinda impoverished about being able to specify which method we want to call; the slightly crude solution is “always call a method named public static void main( String[] args )”. (Why static? For that matter, why public? Why void?) The array-of-Strings corresponds to any command-line arguments the program is called with, if you are running your program from a linux shell. The command line is a rather archaic way of starting a program, as 99.9% of all programs double-clicked on. In those cases, args will be an array of length 0.

To do: Write a public static void main( String[] args ) method for your Treasure class. Have it print out "args is an array of size " + args.length + ": " + java.lang.Arrays.toString(args)". Just have it call your test method.

Stand-alone applications

Optional: Alas, this section is something we can't yet use: stand-alone Java applications are useless unless they create their own GUI windows and frames, which we haven't talked about. (This is covered in ITEC220.)

The computer runs a java program by finding the appropriate class, and running that class's main method. Of course, one class might rely on several other classes. So in practice, Java programs are often distributed as “java archive files” (“.jar”). The jar file contains all the classes needed to run your program, as well as one bit of additional information: of all the classes inside the jar file, which one2 has the main method to really gets your program rolling?

From BlueJ, Project > Create jar file…. It will ask you to specify which class contains the main to start your entire program. Save the file as, say, myProgram.jar

If our java file put up its own windows, we could run it by double-clicking. However, since it just uses System.out.println, we have to run it from the command line: Open a terminal or DOS window, go to the directory where you save your jar file, and type java -jar myProgram.jar. Note that if you add extra words — java -jar myProgram.jar some extra args, then your program's main will be passed an array with three Strings, "some", "extra", and "args".

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 have more complicated interfaces, which contain more advanced features.

(Note that in every IDE, there are two initial tasks: Create a project, and then create a file within that project. I've heard that JCreator might let you start out by creating a file, and automatically creates a surrounding project for you, which is handy.)

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

On the other hand, I know of no other IDEs with BlueJ's above-listed strengths.

To do:, optional: work through a JCreator tutorial, or an eclipse tutorial

From Windows' start button, fire up either eclipse or JBuilder. JBuilder is free, and eclipse has a free non-professional version. There are other free/cheap Java IDEs out there as well; just ask google.


1Note that we're really testing your compareTo method, not the sort method somebody else wrote. So you don't need to test an empty list, a list of just one item, a list of all items being identically equal (==), a list of items all being conceptually equal (.equals), and so on.      back

2 Note that many classes can have main methods -- often those are the test drivers -- but only one class's main is the start-the-whole-program main.      back

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.      back

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs


©2006, Ian Barland, Radford University
Last modified 2006.Dec.01 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme