Вы находитесь на странице: 1из 35

Java Foundations

2-3

Introduction to Object-Oriented Programming Concepts

2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle and/or its affiliates. All rights
2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle and/or its affiliates. All rights

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

Objectives

This lesson covers the following objectives:

Differentiate between procedural and object-oriented programming

Understand a class as a blueprint for an object

Understand a class is used to create instances of an object

Model objects as a combination of …

Properties (data fields)

Behaviors (methods)

… – Properties (data fields) – Behaviors (methods) JFo 2-3 Introduction to Object-Oriented Programming Concepts
… – Properties (data fields) – Behaviors (methods) JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

3

Topics

Procedural vs. Object-Oriented Languages

Classes, Instances, Properties, and Behaviors Translating into Java Syntax

The Software What What Is Is My My Development Program Program Process Doing? Doing? Section
The Software
What What Is Is My My
Development
Program Program
Process
Doing? Doing?
Section 2
Program Program Process Doing? Doing? Section 2 JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Object- Object-

Oriented

Oriented

Programming

Programming

Concepts

Concepts

Oriented Programming Programming Concepts Concepts Copyright © 2015, Oracle and/or its affiliates. All rights

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

4

Review

So far, we've taken …

Decades of computer science innovation

Gigabytes of modern computing power

science innovation – Gigabytes of modern computing power • And much like the Internet … –

And much like the Internet …

We've made a cat!

And much like the Internet … – We've made a cat! JFo 2-3 Introduction to Object-Oriented
And much like the Internet … – We've made a cat! JFo 2-3 Introduction to Object-Oriented

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

5

Java Can Do More!

Procedural languages …

Read one line at a time.

The C language is procedural.

one line at a time. – The C language is procedural. • Object-oriented languages… – Read

Object-oriented languages…

Read one line at a time.

Model objects through code.

Emphasize object interaction.

Allow interaction without a prescribed order.

Java and C++ are object-oriented languages.

order. – Java and C++ are object-oriented languages. JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

6

Object-Oriented Programming

Interaction of objects

No prescribed sequence

• Interaction of objects • No prescribed sequence JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle and/or its affiliates. All rights

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

7

Exercise 1

Play Basic Puzzles 1 through 5.

Exercise 1 • Play Basic Puzzles 1 through 5. – Your Goal: Design a solution that

Your Goal: Design a solution that deflects the ball to Duke.

Consider the following:

What objects do you find on the field of play?

What happens when you put a triangle wall or simple wall icon on the blue wheel?

put a triangle wall or simple wall icon on the blue wheel? JFo 2-3 Introduction to
put a triangle wall or simple wall icon on the blue wheel? JFo 2-3 Introduction to
put a triangle wall or simple wall icon on the blue wheel? JFo 2-3 Introduction to
put a triangle wall or simple wall icon on the blue wheel? JFo 2-3 Introduction to
put a triangle wall or simple wall icon on the blue wheel? JFo 2-3 Introduction to
put a triangle wall or simple wall icon on the blue wheel? JFo 2-3 Introduction to

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

8

About Java Puzzle Ball • Play a set of puzzles. • Become familiar with the

About Java Puzzle Ball

Play a set of puzzles.

Become familiar with the game mechanics.

Consider questions as you play.

Listen to the lesson's debriefing on what you’ve observed.

Apply your observations to understand Java concepts.

• Apply your observations to understand Java concepts. JFo 2-3 Introduction to Object-Oriented Programming Concepts
• Apply your observations to understand Java concepts. JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

9

Object Types

Object Types What objects did you find on the field of play? • • Ball RedBumper

What objects did you find on the field of play?

•

Ball

RedBumper

•Duke
•Duke

LevelGeometry

BlueBumper

• Ball RedBumper •Duke • LevelGeometry • BlueBumper JFo 2-3 Introduction to Object-Oriented Programming Concepts
• Ball RedBumper •Duke • LevelGeometry • BlueBumper JFo 2-3 Introduction to Object-Oriented Programming Concepts
• Ball RedBumper •Duke • LevelGeometry • BlueBumper JFo 2-3 Introduction to Object-Oriented Programming Concepts
• Ball RedBumper •Duke • LevelGeometry • BlueBumper JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

10

Object Interaction • Interaction of objects • No prescribed sequence JFo 2-3 Introduction to Object-Oriented

Object Interaction

Interaction of objects

No prescribed sequence

• Interaction of objects • No prescribed sequence JFo 2-3 Introduction to Object-Oriented Programming Concepts
• Interaction of objects • No prescribed sequence JFo 2-3 Introduction to Object-Oriented Programming Concepts
• Interaction of objects • No prescribed sequence JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

11

BlueBumper Objects

BlueBumper Objects What happens when you put a triangle wall or simple wall icon on a

What happens when you put a triangle wall or simple wall icon on a blue wheel?

A wall appears on every instance of a blue bumper object.

Walls give bumpers behaviors that deflect and interact with the ball.

All blue bumper instances share these same behaviors.

Instance

blue bumper instances share these same behaviors. Instance Instance JFo 2-3 Introduction to Object-Oriented Programming

Instance

instances share these same behaviors. Instance Instance JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

12

Describing a BlueBumper

Describing a BlueBumper • Properties: – Color – – x-position – y-position Shape • Behaviors: –
Describing a BlueBumper • Properties: – Color – – x-position – y-position Shape • Behaviors: –

Properties:

Color

x-position

y-position

Shape

– Color – – x-position – y-position Shape • Behaviors: – Make ping sound – –

Behaviors:

Make ping sound

Deflect ball (via Simple Wall)

Deflect ball (via Triangle Wall)

Flash

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

13

Describing a Ball

Describing a Ball • Properties: – Direction – x-position – y-position • Behaviors: – Make ping

Properties:

Direction

x-position

y-position

Properties: – Direction – x-position – y-position • Behaviors: – Make ping sound – Change direction

Behaviors:

Make ping sound

Change direction

Change x-position

Change y-position

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

14

BlueBumper and Ball Interaction

BlueBumper and Ball Interaction Interaction occurs when the BlueBumper deflects the Ball. When this happens …

Interaction occurs when the BlueBumper deflects the Ball. When this happens …

The Ball’s properties change:

The Ball travels in a different direction.

The Ball’s future x-position and y-position change.

The BlueBumper performs behaviors:

Makes ping sound.

Flashes.

performs behaviors: – Makes ping sound. – Flashes. JFo 2-3 Introduction to Object-Oriented Programming Concepts
performs behaviors: – Makes ping sound. – Flashes. JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

15

Why Does This Matter?

Why Does This Matter? • We've observed important aspects of object-oriented programming. • Remember these

We've observed important aspects of object-oriented programming.

Remember these observations as lessons and exercises become increasingly technical.

Objects can be described as a combination of properties and behaviors.

There may be many instances of the same object type.

All instances of an object share the same behaviors.

Objects may interact with each other, possibly affecting each other's properties and triggering other behaviors.

each other's properties and triggering other behaviors. JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

16

Topics

Procedural vs. Object-Oriented Languages

Classes, Instances, Properties, and Behaviors Translating into Java Syntax

The Software Development Process
The Software
Development
Process

Section 2

Object- What Is My Program Doing? Oriented Programming Concepts
Object-
What Is My
Program
Doing?
Oriented
Programming
Concepts
What Is My Program Doing? Oriented Programming Concepts JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

17

A Different Example

A Different Example • Properties: – Name – – – Favorite Food Age Breed • Behaviors:

Properties:

Name

Favorite Food

Age

Breed

Properties: – Name – – – Favorite Food Age Breed • Behaviors: – Make meow sound

Behaviors:

Make meow sound

Eat

Hunt

Play

Wash

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

18

Classes and Instances

The combination of properties and behaviors is …

Called a class

A blueprint or recipe for an object

Used to create object instances

Object instances

Cat class -Properties -Behaviors
Cat class
-Properties
-Behaviors
Object instances Cat class -Properties -Behaviors JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

19

Creating New Instances from a Blueprint

Cat class Properties Behaviors • name • meow • age • play • favoriteFood •
Cat class
Properties
Behaviors
• name
meow
• age
play
• favoriteFood
eat
cat1
cat2
cat3
• "Opal"
• "Misty"
• "Tiger"
• 11
• 5
• 15
• "Mice"
• "Chicken"
• "BlueJ"
"Mice" • "Chicken" • "BlueJ" JFo 2-3 Introduction to Object-Oriented Programming Concepts
"Mice" • "Chicken" • "BlueJ" JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

20

Object-Oriented Strategy

How do you write programs that achieve this level of flexibility?

When you have an idea or requirement for a program …

Consider what type of objects may exist in this program.

Consider the properties and behaviors of these object types.

Consider how objects interact.

of these object types. – Consider how objects interact. JFo 2-3 Introduction to Object-Oriented Programming Concepts
of these object types. – Consider how objects interact. JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

21

Duke's Choice Online Shopping

Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle
Duke's Choice Online Shopping JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

22

Characteristics of Objects

Objects are physical or conceptual.

Objects have properties:

Physical: Conceptual: Shirt Online Account
Physical:
Conceptual:
Shirt
Online Account

Size

Price

Color

Shirt Online Account – Size – Price – Color • Objects have behaviors : Color property

Objects have behaviors:

Color property value is red.

Shop

Put item in cart

Pay

value is red. – Shop – Put item in cart – Pay Mrs. Duke JFo 2-3

Mrs. Duke

is red. – Shop – Put item in cart – Pay Mrs. Duke JFo 2-3 Introduction

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

23

Classes and Instances

Remember, a class …

Is a blueprint or recipe for an object

Describes an object’s properties and behaviors

– Is used to create Object instances Object instances Class -Properties -Behaviors
– Is used to create Object instances
Object instances
Class
-Properties
-Behaviors
instances Object instances Class -Properties -Behaviors JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

24

Exercise 2, Part 1

Given the following scenario, what objects could you potentially model to complete your program?

Design a program for a coin-sorting machine. This machine should measure, count, and sort coins based on their size or value. It should also print a receipt.

on their size or value. It should also print a receipt. • List at least 3

List at least 3 objects:

1.

2.

3.

also print a receipt. • List at least 3 objects: 1. 2. 3. JFo 2-3 Introduction

JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts Copyright © 2015, Oracle and/or its affiliates. All rights

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

25

Exercise 2, Part 2

Chose an object from Part 1.

What properties and behaviors of this object could you include in your program?

Properties:

Behaviors:

1.

1.

2.

2.

3.

3.

• Behaviors: 1. 1. 2. 2. 3. 3. JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

26

Topics

Procedural vs. Object-Oriented Languages

Classes, Instances, Properties, and Behaviors Translating into Java Syntax

The Software Development Process
The Software
Development
Process

Section 2

Object- What Is My Program Doing? Oriented Programming Concepts
Object-
What Is My
Program
Doing?
Oriented
Programming
Concepts
What Is My Program Doing? Oriented Programming Concepts JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

27

Customer Properties and Behaviors

Properties:

Name

Address

Age

Order number

Customer number

– Address – Age – Order number – Customer number • Behaviors: Shop – – Set

Behaviors:

Shop

Set address

Add item to cart

Ask for a discount

Display customer details

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

28

Translating into Java Syntax

1 public class Customer { 2 3 4 Properties 5 6 7 8 9 Behaviors
1 public class
Customer {
2
3
4
Properties
5
6
7
8
9
Behaviors
10
11 }
{ 2 3 4 Properties 5 6 7 8 9 Behaviors 10 11 } JFo 2-3

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

29

Java Terminology

Class declaration

Java Terminology Class declaration 1 public class Customer { 2 public String name = "Junior Duke"

1 public class Customer {

2

public String name = "Junior Duke";

3

public int

custID = 1205;

4

public String address;

5

public int

orderNum;

6

public int

age;

7

8

public void displayCustomer(){

9

System.out.println("Customer: "+name);

10

}

11 }

out .println( "Customer: " + name ); 10 } 11 } Fields (Properties) (Attributes) Methods (Behaviors)
out .println( "Customer: " + name ); 10 } 11 } Fields (Properties) (Attributes) Methods (Behaviors)

Fields

(Properties)

(Attributes)

Methods

(Behaviors)

Fields (Properties) (Attributes) Methods (Behaviors) JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

30

Modeling Properties and Behaviors

Customer class name address billing info age customer number order number requestDiscount() setAddress() shop()
Customer class
name
address
billing info
age
customer number
order number
requestDiscount()
setAddress()
shop()
displayCustomer()

Class name

Fields

Methods

shop() displayCustomer() Class name Fields Methods JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

31

Data Fields

Fields or Data Fields are the official Java terminology. They’re also called:

Properties

Attributes

Data Members

Java has particular ways of representing data.

Section 3 will take a closer look at data.

We’ll use the main method for this investigation.

For now, it’s alright to include a lot of code in the main method.

BUT a large main method is strongly discouraged, and Section 4 explores how to avoid this scenario.

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

33

Summary

In this lesson, you should have learned how to:

Differentiate between procedural and object-oriented programming

Understand a class as a blueprint for an object

Understand a class is used to create instances of an object

Model objects as a combination of …

Properties (data fields)

Behaviors (methods)

… – Properties (data fields) – Behaviors (methods) JFo 2-3 Introduction to Object-Oriented Programming Concepts
… – Properties (data fields) – Behaviors (methods) JFo 2-3 Introduction to Object-Oriented Programming Concepts

JFo 2-3 Introduction to Object-Oriented Programming Concepts

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

34