chapter 1 object orientation: objects and classes

28
Chapter 1 Chapter 1 Object Orientation: Object Orientation: Objects and Classes Objects and Classes

Upload: thomas-wilkerson

Post on 13-Jan-2016

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 1 Object Orientation: Objects and Classes

Chapter 1Chapter 1

Object Orientation: Objects Object Orientation: Objects and Classesand Classes

Page 2: Chapter 1 Object Orientation: Objects and Classes

What is Programming?What is Programming?

Phases of ProgrammingPhases of Programming1.1. DesignDesign

2.2. ImplementationImplementation

3.3. TestingTesting

4.4. RepeatingRepeating

Page 3: Chapter 1 Object Orientation: Objects and Classes

Polya’s Four Step ProcessPolya’s Four Step Process

Four steps to any problem solving Four steps to any problem solving activityactivity

1.1. Understand the problemUnderstand the problem

2.2. Devise a solutionDevise a solution

3.3. Test the solutionTest the solution

4.4. Rework the solutionRework the solution

Page 4: Chapter 1 Object Orientation: Objects and Classes

Solving Computer ProblemsSolving Computer Problems

Need a tool to solve the problemNeed a tool to solve the problemUse a computer language to do thisUse a computer language to do thisDifferent levels of languagesDifferent levels of languages**

1.1. Machine LanguageMachine Language2.2. Assembly LanguageAssembly Language3.3. High Level LanguageHigh Level Language

* Some are talking about 4Some are talking about 4thth Level Languages Level Languages

Page 5: Chapter 1 Object Orientation: Objects and Classes

AlgorithmsAlgorithms

What is an algorithm?What is an algorithm?

A finite set of steps that specify a A finite set of steps that specify a sequence of operations to be carried out in sequence of operations to be carried out in order to solve a specific problem.order to solve a specific problem.

Page 6: Chapter 1 Object Orientation: Objects and Classes

Properties of AlgorithmsProperties of Algorithms

1.1. FinitenessFiniteness

2.2. Absence of AmbiguityAbsence of Ambiguity

3.3. Definition of SequenceDefinition of Sequence

4.4. FeasibilityFeasibility

5.5. InputInput

6.6. OutputOutput

Page 7: Chapter 1 Object Orientation: Objects and Classes

What is a Computer?What is a Computer?

Made up of many independent parts all Made up of many independent parts all working togetherworking together– Memory UnitMemory Unit– Arithmetic/Logic Unit (ALU)Arithmetic/Logic Unit (ALU)– Control UnitControl Unit– Input DevicesInput Devices– Output DevicesOutput Devices– Auxiliary Storage DevicesAuxiliary Storage Devices

Central Processing Unit (CPU)

Page 8: Chapter 1 Object Orientation: Objects and Classes

Memory UnitMemory Unit

Can be thought of to look like a giant gridCan be thought of to look like a giant grid

Each square in the grid is a memory Each square in the grid is a memory locationlocation

We can access each location in the grid, if We can access each location in the grid, if we choose towe choose to

Page 9: Chapter 1 Object Orientation: Objects and Classes

Objects and ClassesObjects and Classes

When you create a program using an When you create a program using an object-oriented language, you are:object-oriented language, you are:– Creating a model of the problemCreating a model of the problem– The model is built up from objects in the The model is built up from objects in the

problem domainproblem domain– Those objects must be represented in your Those objects must be represented in your

programprogram

Page 10: Chapter 1 Object Orientation: Objects and Classes

Object vs. ClassObject vs. Class

The difference between an object and a The difference between an object and a class is subtle.class is subtle.We will examine a student class.We will examine a student class.If we wanted to identify one particular If we wanted to identify one particular student, we would need to ask some student, we would need to ask some questionsquestions– What color is his hair?What color is his hair?– How tall is he?How tall is he?– What is his QCA?What is his QCA?

Page 11: Chapter 1 Object Orientation: Objects and Classes

Object vs. ClassObject vs. Class

In order to answer these questions we In order to answer these questions we need to know what student we are talking need to know what student we are talking about.about.– This particular student object is an This particular student object is an instanceinstance of of

the Student class.the Student class.

In this example, the Student is a class and In this example, the Student is a class and the instance of the Student is a student the instance of the Student is a student object.object.

Page 12: Chapter 1 Object Orientation: Objects and Classes

Object vs. ClassObject vs. Class

A class is a generic example; a blue print if A class is a generic example; a blue print if you will.you will.

An object is one particular instance of a An object is one particular instance of a class.class.

From one set of blue prints we can make From one set of blue prints we can make multiple instances.multiple instances.

All the instances will be unique but based All the instances will be unique but based on the same set of blue printson the same set of blue prints

Page 13: Chapter 1 Object Orientation: Objects and Classes

ConventionConvention

Names of Classes start with a Capital Names of Classes start with a Capital Letters.Letters.

Names of objects start with lower case Names of objects start with lower case letters.letters.

This case convention is fairly well followed This case convention is fairly well followed throughout the Java programming throughout the Java programming community.community.

Page 14: Chapter 1 Object Orientation: Objects and Classes

BlueJ ExampleBlueJ Example

Let’s take a look at an example in BlueJ.Let’s take a look at an example in BlueJ.

All the examples in the notes assume that All the examples in the notes assume that you have BlueJ installed in C:\BlueJyou have BlueJ installed in C:\BlueJ

The Shape ExampleThe Shape Example

Page 15: Chapter 1 Object Orientation: Objects and Classes

Calling MethodsCalling Methods

When we right clicked on a object on the When we right clicked on a object on the workbench in BlueJ, we got a popup menu with workbench in BlueJ, we got a popup menu with a bunch of function names listed.a bunch of function names listed.In Java, these functions are called In Java, these functions are called methodsmethodsWhen we chose one of these methods for the When we chose one of these methods for the object to perform, we object to perform, we called called or or invokedinvoked the the methodmethodWhen the method requires information from the When the method requires information from the programmer, the method is said to have a programmer, the method is said to have a parameterparameter

Page 16: Chapter 1 Object Orientation: Objects and Classes

Method Method SignaturesSignatures

A method signature provides information A method signature provides information about the method.about the method.– It tells us the name of the methodIt tells us the name of the method– Whether or not the method requires a Whether or not the method requires a

parameterparameter– What type of information is returned from the What type of information is returned from the

methodmethod

Page 17: Chapter 1 Object Orientation: Objects and Classes

Data TypesData Types

The values a parameter can takeThe values a parameter can take

ExamplesExamples– int – whole numbersint – whole numbers– String – a section of textString – a section of text

Pitfall – forgetting the double quotes Pitfall – forgetting the double quotes around a string. You’ll get a message like around a string. You’ll get a message like “Error: undefined variable”“Error: undefined variable”

Page 18: Chapter 1 Object Orientation: Objects and Classes

Java Data TypesJava Data Types

Java supports two kinds of data typesJava supports two kinds of data types

Built-in data typesBuilt-in data types

Object data typesObject data types

Built-in data types are types like integers, Built-in data types are types like integers, characters, etc.characters, etc.

Object data types are like Strings, Arrays, Object data types are like Strings, Arrays, etc.etc.

Page 19: Chapter 1 Object Orientation: Objects and Classes

Built-in TypesBuilt-in Types

Java has a limited amount of built-in types.Java has a limited amount of built-in types.

We will be most concerned withWe will be most concerned with

intint - For integer numbers- For integer numbers

doubledouble - For floating point numbers- For floating point numbers

booleanboolean - For true/false values- For true/false values

char – For character datachar – For character data

Page 20: Chapter 1 Object Orientation: Objects and Classes

Object TypesObject Types

There are many objects types.There are many objects types.

Some are provided for usSome are provided for us

The others we will create ourselves.The others we will create ourselves.

There are some object types we can use There are some object types we can use with no extra workwith no extra work

String – For representing strings of String – For representing strings of characterscharacters

System – For some basic outputSystem – For some basic output

Page 21: Chapter 1 Object Orientation: Objects and Classes

StateState

All of the attributes that define an object is All of the attributes that define an object is referred to as the object’s referred to as the object’s statestate

Java refers to the individual attributes as Java refers to the individual attributes as fieldsfields

Page 22: Chapter 1 Object Orientation: Objects and Classes

Multiple InstancesMultiple Instances

Object-oriented programming (OOP) Object-oriented programming (OOP) allows for creating more than one object allows for creating more than one object from the same class.from the same class.

The internal state of the object allows the The internal state of the object allows the computer to tell the different objects apart.computer to tell the different objects apart.

Page 23: Chapter 1 Object Orientation: Objects and Classes

What is an object?What is an object?

All objects of the same class have the All objects of the same class have the same fields (and methods).same fields (and methods).

The values stored in those fields can be The values stored in those fields can be different.different.

Simply being an object of a given class Simply being an object of a given class guarantees that the objects will have the guarantees that the objects will have the same fieldssame fields

Page 24: Chapter 1 Object Orientation: Objects and Classes

Object InteractionObject Interaction

Objects do not normally operate by Objects do not normally operate by themselvesthemselves

Objects can create other objects and Objects can create other objects and invoke those object’s methodsinvoke those object’s methods

Most Java programs have objects that will Most Java programs have objects that will do just thisdo just this

Question: How do we do this?Question: How do we do this?

Page 25: Chapter 1 Object Orientation: Objects and Classes

Source CodeSource Code

Like most programming languages, Java Like most programming languages, Java is written using plain text files or source is written using plain text files or source code filescode filesThe source code tells the program how The source code tells the program how objects are created and how the objects objects are created and how the objects will interactwill interactAfter a source code file is written, it must After a source code file is written, it must be compiled so that the Java Virtual be compiled so that the Java Virtual Machine (JVM) can interpret itMachine (JVM) can interpret it

Page 26: Chapter 1 Object Orientation: Objects and Classes

Return ValuesReturn Values

Some methods not only take parameters, Some methods not only take parameters, but give something back to you.but give something back to you.These methods are said to have a These methods are said to have a return return valuevalueYou can tell this in the signatureYou can tell this in the signature– String getName()String getName()

This methods returns a StringThis methods returns a String

– void moveRight()void moveRight()This method returns void (nothing)This method returns void (nothing)

Page 27: Chapter 1 Object Orientation: Objects and Classes

HomeworkHomework

1.2, 1.5, 1.6, 1.9, 1.11, 1.18, 1.261.2, 1.5, 1.6, 1.9, 1.11, 1.18, 1.26

Page 28: Chapter 1 Object Orientation: Objects and Classes

QuizQuiz

Explain the difference between an object Explain the difference between an object and a class.and a class.