Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to...

47
Workshop for CS-AP Teachers Chapter 1 Introduction to Object- Oriented Development

Transcript of Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to...

Page 1: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Workshop for CS-AP Teachers

Chapter 1

Introduction to Object-Oriented Development

Page 2: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Learning Goals

• Understand at a conceptual and practical level– What does object-oriented mean?– Objects and classes– Messages and methods– Encapsulation and data hiding

Page 3: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

What is Object-Oriented?

• This means that we focus on the objects not just the procedures– Focus on – who (what objects?)– as well as – what (what do the

objects do?)

Page 4: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Example

• Imagine that you are put into a group to do a job: say design and sell t-shirts to raise money for an organization– You would want to

specify the tasks to be done

– You would also want to specify who will work on each task

Page 5: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Task List• Design T-Shirts

– What will people buy?– What will look good?– What would be too expensive?

• Make the T-Shirts– Where to get them?– How to add the design?– How long will they take to

make?

• Sell T-Shirts– How much to sell them for?– When and where to sell?– How do you keep track of who

sold what and who ordered what?

Page 6: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Job List

• Designers– Responsible for

designing the t-shirts

• Manufacturers– Responsible for

making the t-shirts

• Sellers– Responsible for selling

the t-shirts

Page 7: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Objects have Responsibilities

• An object-oriented design– Determines the tasks to be done– Determines what objects will be responsible

for each task• No one object does everything• Objects work together to accomplish tasks

– The assignment of responsibilities is the key skill in object-oriented design

Page 8: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

What is an Object?

• A person, place, or thing– That knows something about

itself• Has data (attributes, fields)• A cashier has a id, name, and a

password

– And can do something• Has operations (methods)• A cashier can total the items,

take payment, make change

Page 9: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

What is a Class?

• The type of an object – The way we classify

an object• “The Idiot” by

Dostoevsky is a book• “War and Peace” by

Tolstoy is a book• Mary is a cashier• Tasha is a cashier

• Grouping of objects with the same data and operations

Page 10: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Class: Example

• Mary is a cashier• Tasha is a cashier• Cashier is a class

– All cashiers have an id, name and password

• Each will have a different id, name, and password

– All cashiers can total an order, take payment, make change

Page 11: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Object Data

• Each object has its own data– Tasha’s id is 4 and

password is mhall– Mary’s id is 3 and

password is smile4

• All cashier objects have an id, name, and password

• Changing Mary’s data won’t affect Tasha’s data

Page 12: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Teaching Objects and Classes• Point out various objects

in the room and ask what “type” of thing are they.

• Ask what data is known about each type and what operations can objects of that type do.

• Point out that there are several objects of the same “type”.– How are they the same and

how different?

Page 13: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Find Object Types Exercise• List the “types” of objects in this picture

Page 14: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

History of Objects and Classes

• Plato’s Republic (~375 BC) theory of forms– Let us take any common

instance; there are beds and tables in the world -- plenty of them, are there not?

– Yes. – But there are only two ideas

or forms of them -- one the idea of a bed, the other of a table.

Page 15: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

History of Inheritance

• Aristotle Parts of Animals (350 BC) describes inheritance.

• Linnaeus’s Species Plantarum (1753) applied inheritance systematically and is the basis for modern botanical nomenclature.

Mammal

Cat Dog

Page 16: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

SketchPad

• Object-oriented graphics editor– Developed by Ivan Sutherland at MIT in 1963

– Allowed the creation of objects that could be manipulated distinct from any other object.

– Allowed the user to define a "master drawing" from which one could define a set of "instance drawings ". Changes on the master drawing would affect the instance drawings.

Page 17: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Simula

• Language for creating simulations– Developed by Ole-Johan Dahl and Kristen Nygaard at

the Norwegian Computing Centre (NCC) in Oslo between 1962 and 1967.

– Simula allowed one to define an activity from which any number of working versions of that, called processes could be created.

– Each of Simula's processes was a distinct object: It had its own data and its own behavior, and no object could mess with the data and behavior of another object without permission.

Page 18: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Smalltalk

• General purpose object-oriented language– Alan Kay led the group that created

Smalltalk at Xerox PARC which was released in 1980

• This group also created the first personal computer, graphical user interface, and networking

• Smalltalk was never a popular language– But people who programmed in it loved it

Page 19: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Development of Smalltalk

– Alan’s background in biology and exposure to SketchPad and Simula as a graduate student made him think that complex software could better be created when programming objects were more like cells.

• Independent, indivisible, but able to interact with its peers along standard mechanisms (such as absorbing food, expelling waste, etc.)

• By combining thousands or more of these cells, we can build very complex and robust systems that can grow and support reuse.

Page 20: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

C++

• Developed by Bjarne Stroustrup at Bell Labs in 1985– created a highly-efficient version of Simula

by extending the C language – very popular in the late 80s to 90s– still popular for 3d graphics

Page 21: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Java

– In 1991, Sun Microsystems began an internal project to produce a language that could run on intelligent consumer electronic devices

– James Gosling created the programming language Oak for this project as a highly portable, object-oriented programming language.

– Oak evolved into Java and was released in 1995.

– Very popular

Page 22: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

C# (pronounced See-Sharp)

– Microsoft created language • Anders Hejlsberg 2001

– Similar to Java• Garbage collection• Doesn’t compile to machine code• Single inheritance but multiple interfaces• Exception handling

– Closest to C++ in design• Enumerations

– Goal of making it easier to create components

Page 23: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

History Exercise

Page 24: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Simulation

• Object-oriented development means creating a simulation of the problem– We need to know the objects in the problem

• So we can create software objects to represent them

– We need to know the types of the objects (classes)

• So we can define the data and the operations for all objects of that type

Page 25: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Classes Create Objects

• The class can be thought of as a recipe, blueprint, or factory

• Many objects can be created from one class

• Objects keep track of the class that created them– I am an object (instance) of

the Cookie class

Page 26: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Classes Define the Objects

• The computer doesn’t know what we mean by a car or cashier

• We define the class Cashier so that the computer will understand what a cashier or bank account “is” and what it can “do”– In the context of the problem we are trying to

solve

• Then the computer can create objects from the classes

Page 27: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Abstraction

• Pull out only the important details about the thing we are simulating– Cashiers have hobbies but we don’t need to

know about them

Page 28: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

What does a Class Look Like?• Usually the kewords public and class followed by the

class name– The class name should be uppercase

• Next is the body of the class definition between open and close curly braces– The order of fields, contstructors, and methods doesn’t matter to

the computer– But, it is good practice to keep them consistent

public class Name{ ///////// fields, constructors, methods, and inner classes}

Page 29: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

What do Objects Look Like?

• Objects are created with space for their data

• Object have a reference to the object that represents the class– Object of the class

“Class”

Mary : Cashier

id = 3, name=“Mary”

password = smile4

Tasha : Cashier

id = 4

Name=“Tasha”

password = mhall

Name = Cashier

Methods = totalOrder()

takePayment(payment)

makeChange()

Cashier : Class

Page 30: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Software Objects are Models

• The objects we create in software are models of the physical object– We can’t stick a

person in our software– We can create a

model of the person with the information we need to know for that person for our task

Cashier

id

name

password

Page 31: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Creating Objects in Java

• To create an object in Java you ask the class to create a new object– Teacher unknown = new Teacher();– Teacher barb = new Teacher(“Barb Ericson”);

• This creates the new object and calls a constructor to initialize the object’s data– public Teacher()– public Teacher(String name)

Page 32: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Exercise: Create Objects

• Open DrJava and create several Picture objects using– Picture picture1 = new

Picture(FileChooser.pickAFile());– Picture picture2 = new

Picture(FileChooser.pickAfile());

• Ask the pictures to show – picture1.show();– picture2.show();

Page 33: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

What is a Picture?

• What data do you think a picture object has?

• What things (operations) can it do?

Page 34: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Picture Class

• Picture objects have the following data– fileName the name of the file– bufferedImage a BufferedImage for holding

the digitized picture information

• Picture objects have the following methods– getWidth() the width in pixels– getHeight() the height in pixels– show() show the picture– getPixel(int x, int y) get a pixel object

Page 35: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Communicating Objects

• Objects in the simulation need to communicate– They send each other

messages– Messages cause

methods (operations) to be executed

– Objects can refuse to do what is asked of them

Clean your room, please

later

Page 36: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Data Responsibility

• Objects are responsible for their data

• The data should not be allowed to get into an invalid state– Like withdrawing more

money than is in a back account

Page 37: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Encapsulation

• Data and operations are combined in classes• All changes to data should be done by methods

in the class– Making sure that the data stays valid– Data should be private

data

methods

data

methodsmessage

Page 38: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Why use Encapsulation?

• If something goes wrong we know where the trouble is– Some class didn’t protect the data or didn’t make sure

the data was valid

• If something changes it is easy to find the class to fix– If I need to add or change data the methods that work

on it are together in the class

• If you need a method it is easy to check if such a method exists

Page 39: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Data Hiding

• In OO Programming we hide data from objects in other classes – No direct changing of data

in objects of another class

• Objects send messages asking for operations to be done on the data– They don’t need to know

how an object is going to do something as long as they do it

Page 40: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Encapsulation Exercise

• Run the Customer class

• Which class didn’t protect its data?

• Fix the data by making it private

• Change the Customer class to call the public withdraw method

• What happens now when the pirate tries to withdraw money from another account?

Page 41: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Simulation Example

• If you were developing software for a doctors office to track appointments and payment you would need:– Objects that represent the

patients, doctors, and payment information

– Classes that define • Patient

• Doctor

• Payment

Page 42: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Creating the Simulation

• Find the objects in the problem– What are the things doing

the work or being worked on?

• Determine the types of the objects– What classes do they

belong to?• What data does each

object of the class have?

• What operations can each object of the class do?

Page 43: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Coding and Running the Simulation• Define the classes in

Java– Usually each class is

defined in a separate file– The computer needs to

know what you mean by the class

• In the main method create objects to start the simulation– These objects can create

other objects– Objects work together to

accomplish a task

Page 44: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Teaching Object Simulations

• For each situation ask students what roles (objects) are needed and pick students to do that role

• Role-play Situations– Movie theater

• Goal: watch movie

– Doctor’s office• Goal: feel better

– Concert• Goal: listen to music

Page 45: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Simulation Exercise

• What were the objects involved in the simulations?

• How would you categorize the objects? What are their “types” or classes?

• How many objects of each type do you need?

• What data did they need to keep track of?

• What things could they do?

Page 46: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Advantages to OO Development

• Improve Software Quality– Understandability

• the program is a “simulation” of the domain

– Maintainability• easier to find the code to change (encapsulated

objects)• code is easier to read (inheritance, polymorphism)

– Improve Software Economics• Build and deliver software faster that meets user

needs better• Reuse code and designs

Page 47: Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 1 Introduction to Object-Oriented Development.

Summary• Object-oriented programs are simulations

– Objects simulate the “real world” and interact to accomplish a task

– Objects use messages to ask other objects to do something

• Objects belong to classes– An object is an instance of a class– Classes describe the data and operations that all

objects of the class will have – A class creates objects

• Objects are responsible – They should control their data