Computer Science 111:

Introduction to Computer Science I

Gregory M. Kapfhammer


flickr photo shared by Yelp.com under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Classes and Objects

Past: Using and modifying Java classes

Future: Writing our own Java classes!

Why do we need to create our own 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/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

Encapsulation

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!

Classify 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

Parameters

Please find the parameter list for one of the methods

Characteristics of 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?

Do you fully understand this working example?


flickr photo shared by ebayink under a Creative Commons ( BY-NC-ND ) license

Constructors

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

Why not require explicit statement of this return type?

Constructors

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 way as other objects

You still interact with them by calling methods

Organize the drawing of distinct visual objects in methods

Refer to the (past and future) lab assignment(s) and Sections 4.6 - 4.9 for more details!


creative commons licensed ( BY-NC-ND ) flickr photo shared by Russ Allison Loar

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: you can create text fields!


creative commons licensed ( BY-NC-ND ) flickr photo shared by Russ Allison Loar