BlueJ is a free Java environment available from Monash University. BlueJ requires a Java 2 runtime, so you need to install the JDK first before installing BlueJ. BlueJ is written in Java and runs on any platform with a Java 2 runtime, such as Linux, Solaris, Windows, or Mac OS X.
BlueJ is a wonderful environment that makes you think about objects and object-oriented programming. It also contains an excellent, easy to learn debugger. You can use BlueJ to run regular Java programs, but to make the best use of the environment, it is best if you reorganize the programs.
In BlueJ, you should have a subdirectory for each program. This is different from the way that the book code is distributed, with one subdirectory per chapter. Simply make a new subdirectory every time you write or test a new program, and copy the source files that you want into that directory.
To start BlueJ, open a command shell and type a command such as
cd \bluej
bluej
On Linux/Unix, enter a command such as
cd /usr/local/bluej
./bluej
The details depend on your software installation.
Then select Package -> Open Non BlueJ from the menu.
select the subdirectory
containing your files. Caution: You want to select that
directory, and not enter the directory. Highlight the directory name and
then click the "Open in BlueJ" button.
Now you see the class or classes that BlueJ discovered in the selected
subdirectory.
Click the "Compile" button to compile all classes.
You can use BlueJ to simply compile and run all examples in the book. To run an application, right-click on the class with the main method and run main with the default value of null for the args parameter. To run an applet, right-click on the applet class and select "Run applet." However, if you just use BlueJ as a program launcher, you do not take full advantage of its capabilities.
The biggest difference between BlueJ and traditional development environments is that BlueJ isn't concerned with running programs. Instead, you investigate objects.
To instantiate an object, right-click on the class and select an appropriate constructor.
The object is created on the "object workbench" below the class display. To investigate the object, right-click on it and start calling methods. (You can call static methods without instantiating the class, by right-clicking on the class.)
To make effective use of BlueJ, you want to eliminate the very un-object-oriented public static void main and replace it with other means. For example, see the BlueJ version of Hello.java below.
You can simply download a zip file with all the converted examples, ready for use in BlueJ.
Below you will find the rationale behind the conversions.
public class HelloInstantiate a Hello object and call the getGreeting method. You get the BlueJ equivalent of the "Hello, World" printout:
{ public String getGreeting()
{ return "Hello, World!";
}
}
public class Coins1
{ public double getTotal()
{ int pennies = 8; // the purse contains 8 pennies,
int dimes = 4; // four dimes
int quarters = 3; // and three quarters
// compute total value of the coins
double total = pennies * 0.01 + dimes * 0.10
+ quarters * 0.25;
// return result
return total;
}
}
public class Coins3
{ public int getTotal()
{ int pennies = 8; // the purse contains 8 pennies,
int nickels = 0; // no nickels
int dimes = 4; // four dimes
int quarters = 3; // and three quarters
// compute total value in pennies
int total = pennies * PENNY_VALUE
+ nickels * NICKEL_VALUE
+ dimes * DIME_VALUE
+ quarters * QUARTER_VALUE;
return total;
}
// use integer division to convert to dollars, cents
public int getDollar()
{ int total = getTotal();
int dollar = total / DOLLAR_VALUE;
return dollar;
}
public int getCents()
{ int total = getTotal();
int cents = total % DOLLAR_VALUE;
return cents;
}
public static final int PENNY_VALUE = 1;
public static final int NICKEL_VALUE = 5;
public static final int DIME_VALUE = 10;
public static final int QUARTER_VALUE = 25;
public static final int DOLLAR_VALUE = 100;
}
public class Coins4
{ public double getTotal(int pennies, int nickels, int dimes, int quarters)
{ final double PENNY_VALUE = 0.01;
final double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.1;
final double QUARTER_VALUE = 0.25;
double total = pennies * PENNY_VALUE
+ nickels * NICKEL_VALUE
+ dimes * DIME_VALUE
+ quarters * QUARTER_VALUE;
// total value of the coins
return total;
}
}
The programs in this chapter are all applets and don't need to be modifed. To run an applet, simply right-click on the class file and choose "Run Applet".
Save the classes Earthquake and TaxReturn in separate files and discard the driver programs.
public class DoublInv
{ public int getYearsToDouble(double rate, double initialBalance)
{ int year = 0;
double balance = initialBalance;
// keep accumulating interest until balance doubles
while (balance < 2 * initialBalance)
{ year++;
double interest = balance * rate / 100;
balance = balance + interest;
}
return year;
}
}
import java.util.Random;
public class Die
{
public Die()
{ generator = new Random();
}
public int cast()
{ int d = 1 + generator.nextInt(6);
return d;
}
private Random generator;
}
public class PrimeBug // 1You can use the BlueJ debugger to debug the program.
{ /** // 2
Tests whether an integer is a prime // 3
@param n any positive integer // 4
@return true iff n is a prime // 5
*/ // 6
// 7
public static boolean isprime(int n) // 8
{ if (n == 2) return true; // 9
if (n % 2 == 0) return false; // 10
int k = 3; // 11
while (k * k < n) // 12
{ if (n % k == 0) return false; // 13
k = k + 2; // 14
} // 15
return true; // 16
} // 17
// 18
// 19
/** // 20
prints all primes between 1 and n // 21
@param n the highest number to check // 22
*/ // 23
// 24
public static void printPrimes(int n) // 25
{ for (int i = 1; i <= n; i = i + 2) // 26
{ if (isprime(i)) // 27
System.out.println(i); // 28
} // 29
} // 30
} // 31
Discard the AccountTest driver class and place the four account classes in a single directory. Investigate the classes with BlueJ.
import java.awt.Container;Instantiate the class by calling the default constructor. The frame window is displayed (unfortunately sometimes behind the BlueJ frame). Bring it to the front and click on it. The BlueJ console window will pop up and display the mouse spy messages.
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MouseSpyFrame extends JFrame
{ public MouseSpyFrame()
{ MouseSpy listener = new MouseSpy();
JPanel panel = new JPanel();
panel.addMouseListener(listener);
Container contentPane = getContentPane();
contentPane.add(panel);
setSize(200, 200);
show();
}
}
import javax.swing.JFrame;When you instantiate the frame, the frame is shown.
public class EmptyFrame extends JFrame
{ public EmptyFrame()
{ final int DEFAULT_FRAME_WIDTH = 300;
final int DEFAULT_FRAME_HEIGHT = 300;
setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
setTitle("Frame Test");
show();
}
}
Move the calls to setTitle and show inside the frame constructor. Note that the call to show must be the last line of the constructor. Remove the main method and the window closer. Split the program into multiple files, one for each class.
The biggest problem is that you can't put the int[] array that the ArrayUtil.randomIntArray method returns onto the object workbench. Conceptually, that is unfortunate because after all arrays are objects. To overcome that, I made wrappers IntArray and StringArray.
Break up into one class per file.