object oriented design: identifying objects. review what did we do in the last lab? what did you...

16
Object Oriented Design: Identifying Objects

Upload: randell-dawson

Post on 30-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Object Oriented Design:Identifying Objects

Page 2: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Classes

• An object is defined by a class

• A class is the blueprint of an object:

– A class represents a concept, and an object represents the embodiment (i.e., concrete instance) of that concept

– The class uses methods to define behaviors of the object

– Multiple objects can be created from the same class

Copyright © 2012 Pearson Education, Inc.

Page 3: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Class = Blueprint• One blueprint to create several similar, but different,

houses:

Copyright © 2012 Pearson Education, Inc.

Page 4: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Basic class structure

Three major components of a class:– Fields – store data for the object to use– Constructors – allow the object to be set up properly

when first created– Methods – implement the behavior of the object

public class ClassName{ Fields Constructors Methods}

Page 5: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Classes/objects (state/behaviors)

• Car– My first car – a maroon Saab 9000

• Chair• Book

– Java Software Solutions, 7th Edition– Java Software Solutions, 8th Edition

• Table• Student• Teacher

Page 6: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

More Identification

• Think about activities you do everyday– What real-world entities do you interact with?– What about programs you use?– Your phone, laptop/desktop

• Identify– Classes– Information/data– Behaviors/actions

Page 7: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Object-Oriented Programming (OOP)

• Objects make it easier to reuse code that others have written

– Java comes with built-in OO functionality (standard library)

• Objects commonly represent real-world entities

– For instance, an object might be a particular employee in a company

– Each employee object handles the processing and data management related to that employee

• OO is to programming like the assembly line was to industrialization

Copyright © 2012 Pearson Education, Inc.

Page 8: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Fields

• Fields store values for an object.

• They are also known as instance variables.

• Fields define the state of an object.

public class Square{ private int x; private int y; private int size; private Color fillColor;  // Further details omitted.}

private int size;

visibility modifiertype

variable name

Page 9: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Similar Names for Fields

• All of these terms can be used interchangeably:– Fields– Instance Variables– Characteristics– Data– Attributes– Properties

• The values of the fields define the object’s _______state

Page 10: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Data Types

• boolean• char• int• long• float• double• String• Student• (any class)

— true, false— a…z,A…Z,[symbols]— integers— larger integers (2x)— floating decimal— larger floating decimal (2x)— characters strung together— defined class

Page 11: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

8 Primitive Data Types in Java• Four represent integers:

– byte, short, int, long

• Two represent floating point numbers (with decimals):– float, double

• One represents single characters:– char

• One represents boolean values:– boolean

• What about other data?

Copyright © 2012 Pearson Education, Inc.

It must be defined as a class!

Page 12: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Numeric Primitive Data

• Why so many types to hold numbers?• Different sizes affect what values can be stored:

Type

byteshortintlong

floatdouble

Storage

8 bits16 bits32 bits64 bits

32 bits64 bits

Min Value

-128-32,768-2,147,483,648< -9 x 1018

+/- 3.4 x 1038 with 7 significant digits+/- 1.7 x 10308 with 15 significant digits

Max Value

12732,7672,147,483,647> 9 x 1018

Copyright © 2012 Pearson Education, Inc.

Page 13: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Constructors

• Constructors initialize an object.• They have the same name as their class.• They store initial values into the fields.• They often receive external parameter values for

this.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

public Square(){ x = 0; y = 0; size = 0; fillColor = Color.blue;}

Page 14: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

/** * Gets the size of the square. */

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Methods

public int getSize(){ return size;}

return typemethod name

parameter list (empty)

start and end of method body (block)

return statement

visibility modifier

method header/signature

Page 15: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

What we discussed today …

• Classes and objects– What they are– How to identify them– How to identify their components

• Data types

Page 16: Object Oriented Design: Identifying Objects. Review What did we do in the last lab? What did you learn? What classes did we use? What objects did we use?

Homework

• Work on Lab 2 and submit Lab 1• Work with Codingbat exercises• Read Chapter 2