Professional Documents
Culture Documents
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?
Start BlueJ and open project figures. There are five classes named Canvas, Circle,
Square, Triangle and Person.
ARH 1
BITP 3113 LAB 1
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
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.
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?
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
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.
ARH 3
BITP 3113 LAB 1
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.
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.
Create an instance of class Picture and invoke the draw method. Try out the
setBlackAndWhite and setColor methods.
ARH 4
BITP 3113 LAB 1
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