Computer Science 111:

Introduction to Computer Science I

Chapter Four

Gregory M. Kapfhammer

creative commons licensed ( BY-NC-SA ) flickr photo shared by Ed Yourdon

Classes and Objects

Past: Using and modifying Java classes!

Future: Writing our own Java classes!

An object has state, defined by values of attributes

An object has behavior, defined by the operations

Rolling Dice

We can define two classes: Die and RollingDice. Why?

Run the git pull command in your "share" repository!

Now, find the class/source/chapter04/ directory.

Use GVim to study the source code of this system.

Draw a picture to explain how you understand this code.

Compile and run this program. What is the output?

Anatomy of a Class

Find all of the following parts of Die and RollingDice!

Data declarations

Method declarations

Constructor declaration

Instance data

Main method


An object should be self-governing

It should be difficult to "reach in" to instance data

Why is this an important issue to enforce?

Let's review Figure 4.5 on page 169!

Visibility modifiers: public and private

See Figure 4.6 to understand the impact of visibility!

This about methods as either accessors or mutators

Anatomy of a Method

Run the git pull to get new code!

Review the source code of Transaction and Account

What is the purpose of a return statement?

Please find at least two return statements in the code

Type of the return = type of the returned variable


Please find the parameter list for one of the methods

What do you notice about each entry in the parameter list?

Wait! Let's find the formal and actual parameters

What is the purpose of formal and actual parameters?

Methods can also define local data. Can you find any?

Make sure that you fully understand the working example!


A constructor is invoked when an object is created

The name of the constructor is the same as the class

The constructor returns the type of the class itself

The return type of the constructor is not explicitly stated


The default constructor already exists!

But, it makes an object without assigning values

Please think about the initial state of your objects!

Can you find a constructor in the provided source code?

Graphical Objects

Created in the same was a other objects

You still interact with them by calling methods

Organize the drawing of distinct visual objects in methods

Refer to the lab assignment and Section 4.9!

Graphical User Interfaces

Three kinds of objects: components, events, listeners

The focus is on event-driven programming

What kinds of GUI components are available?

Section 4.8: you can add buttons!

Section 4.9: what about text fields?

But, there are also sliders, menus, and others!