36
Chapter 2 Data abstraction: introductory concepts

Chapter 2 Data abstraction: introductory concepts

Embed Size (px)

Citation preview

Page 1: Chapter 2 Data abstraction: introductory concepts

Chapter 2Data abstraction:

introductory concepts

Page 2: 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.

Page 3: Chapter 2 Data abstraction: introductory concepts

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.

Page 4: Chapter 2 Data abstraction: introductory concepts

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.

Page 5: Chapter 2 Data abstraction: introductory concepts

Types Values grouped together

according to how we use them and what operations we perform on them.

Example:Integers use +,-,<,>, etc.

Page 6: Chapter 2 Data abstraction: introductory concepts

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

Java has no built-in composite primitives.

Page 7: Chapter 2 Data abstraction: introductory concepts

Numbers Integers:

byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes)

Used for counting (whole numbers).

Page 8: Chapter 2 Data abstraction: introductory concepts

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.

Page 9: Chapter 2 Data abstraction: introductory concepts

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.

Page 10: Chapter 2 Data abstraction: introductory concepts

Boolean Only 2 values: true or false.

Page 11: Chapter 2 Data abstraction: introductory concepts

Objects The fundamental abstractions from

which we build software systems. Designed to support the

functionality of the system. Responsible for performing specific

tasks.

Page 12: Chapter 2 Data abstraction: introductory concepts

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.

Page 13: Chapter 2 Data abstraction: introductory concepts

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.

Page 14: Chapter 2 Data abstraction: introductory concepts

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.

Page 15: Chapter 2 Data abstraction: introductory concepts

EXAMPLE SYSTEM USING OBJECTS

University registration System

What are the objects?

Student Course

Page 16: Chapter 2 Data abstraction: introductory concepts

Queries:

Student Course•What is your name?•How many credit hours?•What are you enrolled in?•Have you paid your fees?

•What is the course number?•How many credit hours is it?•What room is the course held in?

Properties:name

addressssn

credit hoursmajor

year in school

course numbersection

professorroom

time/daytotal seats

seats taken

Page 17: Chapter 2 Data abstraction: introductory concepts

States:

Student Course

Commands:

•add a course•drop a course•change the address•determine fees

•change the room number•set professor•calculate the number of seats available

Page 18: Chapter 2 Data abstraction: introductory concepts

States and commands

Page 19: Chapter 2 Data abstraction: introductory concepts

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

have?

Page 20: Chapter 2 Data abstraction: introductory concepts

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.

Page 21: Chapter 2 Data abstraction: introductory concepts

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.

Page 22: Chapter 2 Data abstraction: introductory concepts

Particular object responsibility (cont.)

Asking what an object knows: object queries.

Asking what an object does: object commands and queries.

Page 23: Chapter 2 Data abstraction: introductory concepts

Knows:

Student Course

Does(Commands):

•Student’s name•Student’s address•Student’s social security number•Number of credit hours enrolled

•Course’s title•Course’s section•Course’s professor•Course’s time•Course’s room number

•Add a course to the student’s schedule•Drop a course from the student’s schedule•Calculate student’s fees

•Change room assignment•Add a student to the roll•Print a class roll•Decide if the class is full

Page 24: Chapter 2 Data abstraction: introductory concepts

Does(Queries):Student Course

•Supplies a student’s name. Query: What is the student’s name?•Supplies a student’s current classes.•Supplies the student’s address

•Supplies the Professor of the class. Query: Who is teaching this class?•Supplies the number of student’s enrolled in the class.

Page 25: Chapter 2 Data abstraction: introductory concepts

Ticket dispenser example Queries:

How many tickets have we sold? How many tickets are left?

Properties: Next ticket number Tickets left

Page 26: Chapter 2 Data abstraction: introductory concepts

Ticket dispenserexample (cont.)States and Commands

Page 27: Chapter 2 Data abstraction: introductory concepts

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

Page 28: Chapter 2 Data abstraction: introductory concepts

Chess example (cont.)States

Page 29: Chapter 2 Data abstraction: introductory concepts

Chess example (cont.)

Commands: Move a piece? Change whose turn it is?

Page 30: Chapter 2 Data abstraction: introductory concepts

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.

Page 31: Chapter 2 Data abstraction: introductory concepts

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.

Page 32: Chapter 2 Data abstraction: introductory concepts

Reference Examples

Page 33: Chapter 2 Data abstraction: introductory concepts

Reference

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

Page 34: Chapter 2 Data abstraction: introductory concepts

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)

Page 35: Chapter 2 Data abstraction: introductory concepts

Glossary

Page 36: Chapter 2 Data abstraction: introductory concepts

Glossary (cont.)