You are on page 1of 5

BITP 3113 LAB 1

LAB 1 : OBJECTS AND CLASSES

1.1 Objects and Classes

Java allows you to model the world (or problem domain) in terms of classes and objects.
A class Book may represent all kinds of books say, in a library. A particular example of
the class (or an instance) is an object; this object can be the “Object First with Java” book.
We can also state that the “Object First with Java” book is an instance of the Book class.

Can you identify the various classes and objects around you?

1.2 Creating Objects

Start BlueJ and open project figures. There are five classes named Canvas, Circle,
Square, Triangle and Person.

Right click on the Square class and select new Square().

When asked about the instance name, select the default


value by clicking OK.

ARH 1
BITP 3113 LAB 1

You’ll see a red rectangle


representing the new square1
object in the bottom left-hand side
of the BlueJ window called the
object bench.

Create several objects from the


circle, triangle and person classes.

1.3 Calling Methods

Right-click on the square


object and select
makeVisible() from the pop-
up menu. You are actually
invoking the method
makeVisible().

A new window appears with a red square. Can you make it


invisible? Try invoking moveUp, moveDown, moveLeft and moveRight methods a
few times.

When you select a method, you are calling or invoking a method; by doing so, you are
manipulating the behavior of an object.

1.4 Parameters

Now, invoke method moveHorizontal. A dialog box


appears prompting you to enter the number of pixels the
object should move. The second line in the dialog box
shows

void moveHorizontal(int distance)

ARH 2
BITP 3113 LAB 1

This is the method signature; indicating the parameter required to execute the method.
You can see the type (int) and the name of the parameter (distance). Enter, for example,
35 and click OK. Notice in which direction the object moved.

Does method moveUp has parameter?

1.5 Data types

The signature of a method indicates the data types and the order of the parameter(s)
passed to the method. Properly supplied parameter(s) ensures a successful execution of
the method.

Invoke method changeColor . What is the data type for method changeColor?

1.6 Multiple instances

You can create many instances from a class; meaning that you can create many objects
with different states from a single class.

Create a small green triangle and two big yellow circles. Change the size of the triangle.

1.7 State

An object’s state refers to the values of all attributes,


such as size, diameter, height, width and visible. In
BlueJ, we can inspect these values using the
Inspect function from the object pop-up menu.

1.8 What is in an object?

Objects created from the same class possess the same fields or attributes. For example,
objects created from the Square class (square1, square2, etc.) have the same size,
xposition, yposition, color and isVisible.attributes. Objects created from the Circle class
may have different attributes from the square objects.

Can you re-create the following images?

ARH 3
BITP 3113 LAB 1

1.9 Java code

Whenever we create an object and invoke methods in BlueJ, we are actually executing
Java statements. In order for you to view the commands, select Show Terminal from the
View menu. Then select Record method calls from the terminal’s Options menu.

A sample sequence of creating a triangle object may look like this:


(What can you highlight about the Java statements?)

Triangle segiTiga = new Triangle();


segiTiga.makeVisible();
segiTiga.changeColor("magenta");
segiTiga.slowMoveVertical(-45);

Instead of viewing the Java statements, you can also write the Java statements. Select
Show Code pad from the View menu. This should display a new pane next to the object
bench in your BlueJ window.

Now type the Java statements to simulate the sun rising up between two mountains.

1.10 Object Interaction

Close project figures and open project house.

Create an instance of class Picture and invoke the draw method. Try out the
setBlackAndWhite and setColor methods.

ARH 4
BITP 3113 LAB 1

All the classes in house are the same to those in


figures, with an additional class Picture. This new
class execute a sequence of Java statements (to
create an image of a house and sun).

The point here is that objects can create other objects.

1.11 Source Code

The source code for a class can be viewed by selecting the Open
Editor from the class pop-up menu, or by double-clicking the class
icon. When you modify the source code, the class icon will appear in
stripes. You need to compile the source code by selecting Compile
button in the Editor window or selecting Compile from the class pop-
up menu.

In the source code of class Picture, find the part that actually draws the picture. Change
it so that the sun will be blue rather than yellow.

Add a second sun to the picture. To do this, pay attention to the field definitions close to
the top of the class. Create a sunset using the second sun (Make the second sun move
down slowly). Then, make a person walks up to the house.

1.12 Summary

Classes and objects are essential in object oriented programming; we model our problem
domain as classes and create objects from the classes.

An object must have identity, states and behavior. The states of an object are represented
by variables (of certain data types). The behavior of an object can be manipulated by
invoking methods; these methods have certain return type and parameters.

ARH 5

You might also like