D76V 35 Software Development: Object Oriented Programming. Bell College. 2.
Getting Started with BlueJ page 1. 2: GETTING STARTED WITH BLUEJ. What is ...
What is BlueJ? ..............................................................................................................1 Your first Java program ................................................................................................1 Java applications and the main method......................................................................5 EXERCISE ......................................................................................................................7
What is BlueJ? BlueJ is a Java integrated development environment (IDE) which has been designed specifically for learning object oriented programming in Java. It is more convenient to use than the standard Java command line tools, and easier to learn than a full-featured IDE such as JBuilder. It will also help you understand how the classes in your object oriented programs are related to each other.
Your first Java program Follow these steps to create and run your first Java program. Start BlueJ using the desktop icon. The BlueJ window opens up. Select the Project > New Project menu item. The New Project dialog opens. Select where you want to create the new project and enter the name hello as shown below:
Click Create. The BlueJ window should now look like this:
The title bar tells us we are working with a project called hello. The white area is used to show what classes are in your project. At the moment there are none – the document icon represents a project README file which you can use to save notes about the project. Your project needs a class before it will do anything useful. Click the New Class button. The Create New Class dialog appears:
Give your new class the name HelloWorld as shown above, and make sure the Class option is selected for Class Type. Click OK. The new class now appears in the BlueJ window. It is shown as a box with the class name. The cross hatching indicates that the class has not been compiled yet.
Double-click the HelloWorld class. The code editor opens. BlueJ creates some sample code for every new class. Replace the sample code with the following (you can replace “John” with your own name): /** * HelloWorld - a first Java program * * @author John * @version 1.0 */ public class HelloWorld { public static void main(String[] args){ String myName = “John”; //Display greeting System.out.println("Hello World!"); System.out.println(“My name is “ + myName); } } Click the Compile button in the editor window. If you have made any mistakes in typing the code you will get error messages. When the code is compiled successfully, click the Close button.
You have now created and compiled a Java program. The only thing left to do is to run it. Right-click on the HelloWorld class. Select void main(args) from the pop-up menu which appears.
Click OK in the dialog box which appears.
This runs the program. The output appears in the Terminal Window (this opens now if it is not already open). The program simply prints out the words “Hello World!” and your name.
You have now created, compiled and run a Java program.
Java applications and the main method This simple program shows several features of Java applications. Comments /** * HelloWorld - a first Java program * * @author John * @version 1.0 */ Text between /* and */ symbols are comments to make the code easier to understand, and are ignored by the Java compiler. Comments can also be used to generate documentation automatically for your programs. Get into the habit of writing comments in this form at the top of all your programs. Comments can also be on a single line with // at the start, for example //Display greeting Classes The code in the example program is contained in a class. A class starts with a declaration: public class HelloWorld
and all the code is inside a pair of curly brackets { … } A more complicated program can have more than one class. Every program must have at least one class. Methods Methods are the actions that a class can perform. A class needs at least one method before it can actually do anything. Our example has a method called hello. public static void hello(){ … } Again, the code is inside curly brackets. Note that the main method is inside the class’s brackets. In BlueJ, to run a program, you can right-click on a class and select the main method (void main) from the pop-up menu. Statements A statement is a single instruction. In Java, each statement must be ended with a semicolon (;) Variables A variable is a name which identifies a piece of data used by the program. The example contains a variable which contains a string of characters. String myName = “John”; This variable is of type String. Other variable types include int, char, float, double and boolean. For example int myAge = 21; double myHeight = 1.83; Console output Applications can output results to the terminal window, or console, using System.out.println statements. These can output specified characters or the value of variables. System.out.println("Hello World!"); System.out.println(“My name is “ + myName);
EXERCISE 1. Modify the HelloWorld application so that your name is stored in two String variables called firstName and lastName, and both names are output to the console. 2. Create a new BlueJ project called hello2, and in it create an application called HelloWorld2 which prints out a greeting and the values of your age and your height (stored as int and double variables respectively). Remember to include comments at the top of the class file. 3. Create a new BlueJ project called useless. Download the file Useless.java from your course web site into the project folder. (In Internet Explorer, right-click on the link, select Save Target As…, and enter the filename in quotes in the Save dialog, i.e. “Useless.java” – the capital letter at the start is important!). Use the Add Class from File menu option to add this class to your project. Compile and run the application. 4. Create a new BlueJ project called hello3. Download the file HelloWorld3.java from your course web site and add it to your project. Compile the class. There is an error in this class – find the error, correct it and compile and run the application.