CS 1054: Lab 1
Lab meeting 1: Objects and classes
Note
This is an individual assignment. You should NOT be working with
a partner.
Aim
This lab is supposed to introduce you to the BlueJ environment and help you
understand the basic concepts of objects and classes.
List of tasks
-
Make a directory on the Z: drive with your university pid as the name. Copy the
picture
and
shapes
subdirectories from the c:\BlueJ\examples\ directory to the directory that you
just created. They are also
available here.
-
Open the BlueJ environment. This can be done by clicking on Start -> BlueJ or
Start -> All Programs -> BlueJ -> BlueJ.
-
Open the shapes project from the Z: drive where you just copied the directories
by clicking on Project -> Open Project. You should see four rectangles on the
screen. Each rectangle represents a particular
class.
If the shapes have diagonal stripes on them, compile them by right clicking on
each shape and selecting the compile option.
Note:
For more detailed instructions on using the BlueJ environment, you can refer to
the BlueJ tutorial (Shift
+ Click to open it in another window).
-
Create multiple
objects
from each shape class. To create a new object, you should right click on the
particular class and then select the "new" option. For example, you can create
a new square object by right clicking on the square class and selecting the
"new Square()" option.
-
Experiment with the properties associated with each object by right clicking on
the object (not the class) and then selecting the various
methods. Move the objects around and change
their colors using the appropriate methods.
-
Inspect the properties of each object by right clicking and selecting the
"Inspect" option. You can observe that if you modified the properties
associated with the objects, different objects derived from the same class,
will have different values for their fields.
-
Double click on each class to open a new window that displays the
source code
associated with each class. You can see that each property associated with an
object has a corresponding method in the source code. If you make any changes
to the course code, you can observe diagonal stripes appearing on the classes
in the BlueJ window. This implies that the classes need to be re-compiled.
Note: Please do not make any changes to the code that
might cause compilation errors.
-
Close this project. By this point, you should be familiar with opening and
closing a project in the BlueJ environment and a few other basics regarding
classes and objects.
-
Re-open the shapes project. Perform exercise 1.9 from the textbook. Use the
various shapes provided to build the picture shown in Figure 1.7. Once you have
finished this task, show the picture of your house to the GTA.
-
Open the picture project. Perform exercises 1.10 and 1.11 from the textbook.
-
If there is time remaining, perform exercises 1.13 and 1.14 from the textbook.
Position the second sun to be above the roof of the hut.
-
Show your work to the GTA.
Please answer the following
questions using the Square class:
- What keyword is used to state the type of square?
- For the square class, list all the instance variables, and the
method names.
- For each instance variable, list its type and state what type
of values each can accept.
- How do the method names differ from the instance variable
names?
- Except for the method names, how do each statement end?
- List all the keywords used in the square class. Its alright
not to know their exact meanings.
- Can you pass the value "astring" to the moveVertical()? Why
not?
© Mir Farooq Ali 2003.