ACS-1903 Lab 1: Using BlueJ Fall 2007

Using BlueJ at UofW

The purpose of this lab is to give you experience with BlueJ. Over the course of the term, in classes and in labs, you will receive more instruction. If you have any questions during this lab do not hesitate to ask your lab demonstrator.

In this lab you will :

·  Start BlueJ

·  Create a new BlueJ project

·  Compile and run a Java program

·  Interact with an object system by sending messages

The example deals with objects that are squares, triangles and circles. We will not discuss the Canvas class. Its responsibilities are to draw or repaint the images we see on the screen. (You can consider the Canvas class later on this term.)

We will obtain the files from the web and import them into a project that we create.

Answer the questions on the following pages and hand in these sheets to the lab demonstrator.

Student Identification

Student number ______

Last name ______

First name ______

Workstation user id ______

Email address ______

1. Logon to your workstation

Logon to a workstation (logon information is available from the lab demonstrator).

2. Obtain the Java files

Create a folder on your workstation called MyShapesSource. This folder could be located in MyDocuments. Dowload the four java files that are found at http://io.uwinnipeg.ca/~rmcfadye/1903/shapes/ to your MyShapesSource folder.

3. Start BlueJ

Click Start, then click on Programs, then click on BlueJ, and then click the BlueJ program

Start > Programs > BlueJ > BlueJ

4. Create a BlueJ Project

Create a project called MyShapes by selecting New Project from the Project Menu. It can be useful to you if you store such a project on the H drive. You have access to this drive from both computer labs.

Project > New Project

5. Importing java files

Import the four java files to your shapes project by using the Import option found in the Project menu:

Project > Import, and select MyShapesSource

After the files are imported you will see a window such as:

The files must be compiled; use the Compile option from the Tools menu:

Tools > Compile

You may get warnings from the compiler which you should be able to ignore.

Now your Java classes can be used, but first we will examine one of the source files.

6. Reviewing the Java Program

You can view a Java class by double-clicking on the appropriate rectangle in the above display.

Double-click on the orange rectangle named Triangle. This action opens the class editor.

Reviewing the Triangle class

Examine the code on your workstation, and note there is coloured text in use: some red, some blue, etc. BlueJ uses colour to denote special aspects of the code.

The first line is a Java import statement.

import java.awt.*;

In this case the program is stating that it will use a Java library known as awt. The editor shows the word import is in red because it is keyword.

The blue-coloured text is used for comments.

Following the comments, you will see the beginning of the Triangle class.

public class Triangle

{

private int height;

private int width;

private int xPosition;

private int yPosition;

private String color;

private boolean isVisible;

A class always begins with the keywords public class followed by its name; in this case Triangle.

The above shows the variables in Triangle. These hold values that describe a particular triangle (its height, width, position, etc).

Following the lines giving the name of the class and its variables, we see a method that is executed whenever we ask for an instance of the Triangle to be created. This type of method is called a constructor method.

public Triangle()

{

height = 30;

width = 40;

xPosition = 50;

yPosition = 15;

color = "green";

isVisible = false;

}

Questions

Where do new triangles position themselves when they are instantiated?

Answer: ______

What colour is given to a newly instantiated triangle?

Answer: ______

How high is a newly instantiated triangle?

Answer: ______

How wide is a newly instantiated triangle?

Answer: ______

Following the “constructor” method, we have several methods which begin with the words public void followed by the name of the method. Each method defines a responsibility an object has, or a capability it has. In this example you will see that a triangle can

make itself visible

make itself invisible

move to the right

move to the left

etc.

The programmer that designed shapes decided that triangles should be responsible for making themselves visible and invisible, moving themselves right, left, up, or down on the screen. As you will see, we can ask a triangle to do these things.

Questions:

What can we ask a triangle to do? (The answer is the names of its methods)

Answer:


At this time it is important that you realize a class may have many methods and that each is designed for a specific purpose: to become visible, to become invisible, to move up, etc. Do not be concerned with exactly how each of those is done… that will come later on in the course.

There is a concept called anthropomorphism that is important to object programming. (Consult a dictionary if needed, or wikipedia.) Java programmers will design systems with classes where each method is some responsibility that an object of that class has.

Consider an ordinary office where people are employed and each person is given specific responsibilities. When asked, a person will perform a particular duty such as completing an invoice, issuing a cheque and so on. The same principle is applied to object-oriented computing systems.

After exploring the Triangle class, we know that triangles can be asked to do very specific things.

7. Interacting with the shapes program

Let’s create a triangle …. Remember the constructor method – the triangle will initially be invisible and so we will ask it to make itself visible.

Begin by right-clicking the Triangle class and choose newTriangle(). You will be asked to name the triangle.

When you click OK for naming the triangle you will see a red object appear on your screen indicating that you have instantiated an object.

Right-click on an object and the messages it can respond to are shown.

Choose makeVisible().

After selecting makeVisible() look at the bottom of your screen. You should see a new button,

If you don’t see the following image, then click this button and you will see your program’s output.

Question What happens when you instantiate a second triangle and make it visible?

Answer:

So far we have only considered the Triangle class. Examine the Square and Circle classes.

Now… make a square. Make it visible.

Can you align it directly under the triangle? You may need to send the square or triangle some messages. Don’t hesitate to experiment.

To instantiate a square and have it align directly under the triangle, you needed to send various messages asking for certain things to be done.

Question What messages did you send to the triangle and/or the square after they were instantiated to have them aligned vertically?

Answer:

7