Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: Hidden Figure - May 24, 2019

AP Computer Science :: Lessons :: Introduction to BlueJ

What Is BlueJ?

BlueJ is a Java development environment that is specifically geared for introductory teaching of object-oriented programming concepts. It was jointly developed by the University of Kent in Canterbury and Deakin University in Melbourne. It is also supported financially by Sun Microsystems. The program, like Java itself, supports multiple platforms and currently runs on Solaris, Linux, Macintosh, and Windows. You can download BlueJ for free from bluej.org.

Objects in BlueJ

BlueJ is setup to really show how an object-oriented program works. An object is a collection of data and operations, in which the data can only be accessed and modified by the operations. The data in an object represents different variables while the operations represent methods. The easiest way to see this is by looking at a pre-made example.

BlueJ Environment
  1. Open BlueJ.
  2. Download and unzip the project people.

You'll notice that the environment in BlueJ is fairly minimalist. You will see a few buttons on the left, some boxes in the main area, and an empty window on the bottom. Let's explore these areas.

The main window with the boxes contains all of the classes associated with this project. In BlueJ, you don't have to compile the entire project at once. You can compile individual classes as you create or modify them to make sure they work correctly. The dotted line you see means the Person class uses the Database class. The solid lines indicate Staff and Student inherit from the Person class. Person is an abstract class, which means it defines methods and attributes for other classes but cannot you cannot make an instance of it. Person is also known as the base class for Staff and Student because both of those classes inherit from it. The Staff and Student classes are referred to as derived classes because they inherit attributes from a base class.

You can create a new instance of a class by right-clicking on it. Doing so will give you a contextual menu with a number of options. One or more of those options will be the class constructor. A constructor is a method that runs when an object is instantiated. You should notice that the Person class does not have a constructor since it is an abstract class. The other classes all have at least one constructor, which you can recognize easily since the constructor has the same name as the class. Let's make a new instance of the Staff class so we can run some of its methods.

  1. Right-click on the Staff class.
  2. Choose the constructor with no parameters.
  3. Name the instance "staff1" and press "Ok".

Object BenchAfter you create the instance you should see a red box appear in the object bench at the bottom of the BlueJ environment. This is the instance you created. An instance is an object that has the attributes and behavior that were specified by a class. A good explanation of instances is comparing them to houses. If you have a blueprint of a house you can build as many houses as you want based off that blueprint. Likewise, when you have a class you can build as many instances as you want based off that class. In our case we have created a new staff member object based off the Staff class.

When you right-click on the instance you will be given a few options. At the top of the contextual menu you will see all of the methods you can use that are inherited from other classes. In the case of the Staff class it inherits from Person and Object so you have methods included from each. Underneath the inherited methods you will find the methods that belong to the Staff class. If you try calling the getRoom method you will notice it returns "(unknown room)" because we have not assigned a room for this staff member yet. Let's set a room for this staff member.

setRoom Method
  1. Right-click on the staff1 instance.
  2. Choose the setRoom method.
  3. Enter "1028" as the room and press "Ok".

If you didn't put quotes around "1028" you will get an error because the program is looking for a string and you input an integer. Once you change the room number the getRoom method should now return 1028.

Editing and Compiling

Editing a class in BlueJ is simple. You can either right-click on it to edit it, but the easiest way is to double-click on the class you want to edit. We will edit the Staff class and even though we haven't learned the specifics of the Java language yet the code should be fairly recognizable.

  1. Double-click on the Staff class.
  2. Find the getRoom method.
  3. Change the "return room;" line to the following:
return "Room " + room;

We modified the getRoom method to show the text "Room" before the actual room number. After you close out of the editor you will notice a change in the Staff class. The class now has a series of stripes across it. A striped class is a class that has not been compiled since it was last edited. A compiler converts a programming language (such as Java) into machine code (binary) so the computer can execute the instructions. You can either use the Compile Button on the left, which compiles all uncompiled classes, or right-click on the Staff class to compile it.

  1. Compile the Staff class.

After compiling the new Staff class you will notice the staff1 instance disappears. If we change the way a class works we need to recreate any instances of that class so BlueJ removes the instances for us.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram