Chapter 2
Data abstraction:
introductory concepts

This chapter discusses
How to build components of a software system.
Foundational notions of value/type and object/class.
Java primitives.
Reference values.

Values and types
value: a fundamental pieces of information that can be manipulated in a program.
type: a set of related values along with the operations that can be performed with them.

Values
2 kinds of values:
simple (atomic): integers, characters
composite: date(day, month, year), string(several characters)
Values are abstractions used to model various aspects of a problem.

Types
Values grouped together according to how we use them and what operations we perform on them.
Example:Integers use +,-,<,>, etc.

Primitives
Built-in types.
Java primitives: byte, short, int, long, float, double, char, and boolean.
Java has no built-in composite primitives.

Numbers
Integers:
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)
Used for counting (whole numbers).

Numbers (cont.)
Floating point:
float (4 bytes)
double (8 bytes)
Used to represent things we measure (anything with a decimal point).
The computer’s representation may not be exact.

Characters
char includes upper case and lower case letters, digits, punctuation marks, and other special characters.
Characters contained in the Unicode character set.
Used for input/output.

Boolean
Only 2 values: true or false.

Objects
The fundamental abstractions from which we build software systems.
Designed to support the functionality of the system.
Responsible for performing specific tasks.

Queries and properties
Furnish relevant information about what the object represents.
properties: characteristics about which the object provides information.
Each property has an associated value.
queries: requests for information concerning properties.

Commands and state
The values of properties can change at any time.
state: the set of an object’s properties and associated values at any given time.

Commands and state (cont.)
Commands instruct the object to perform some action which often results in the object changing state.
At any given time, a particular value is associated with each property of an object.

Slide 15

Slide 16

Slide 17

Slide 18

Designing with objects
What are the objects?
What features should these objects have?

Particular object responsibility
What must an object know?
The properties of the entity the object is modeling, and
About other objects with which it needs to cooperate.

Particular object responsibility (cont.)
What must an object do?
Compute particular values.
Perform actions that modify state.
Create and initialize other objects.
Control and coordinate the activities of other objects.

Particular object responsibility (cont.)
Asking what an object knows: object queries.
Asking what an object does: object commands and queries.

Slide 23

Slide 24

Ticket dispenser example
Queries:
How many tickets have we sold?
How many tickets are left?
Properties:
Next ticket number
Tickets left

Ticket dispenser
example (cont.)
States and Commands

Chess example
Queries:
Where is a certain piece?
What color is a player’s pieces?
Is a player in check?
Properties:
Player’s color
Piece’s position

Chess example (cont.)
States

Chess example (cont.)
Commands:
Move a piece?
Change whose turn it is?

Classes
An object is an instance of a class.
A class specifies the features of a group of similar objects.
Two objects from the same class will have the same set of queries and commands, but usually contain different values.

Objects as properties of objects
A relation associates two or more objects in a specific, well defined way.
Relations define how objects interact to solve a problem.
An object’s property can be a reference to another object.

Reference Examples

Reference
A value that denotes an object
null reference: refers to no object.

We’ve covered
Introduction of fundamental notions of values and objects.
Java provides:
Integer types (byte, short, int, long)
Real or floating type (float, double)
Character type ( char)
Boolean type (boolean--true/false)

Glossary

Slide 36