p chapter 2 introduces object oriented programming. p oop is a relatively new approach to...

37
Chapter 2 introduces Chapter 2 introduces Object Oriented Object Oriented Programming. Programming. OOP is a relatively new OOP is a relatively new approach to programming approach to programming which supports the which supports the creation of new data creation of new data types and operations to types and operations to manipulate those types. manipulate those types. This presentation This presentation introduces OOP. introduces OOP. Object Oriented Programming Data Structures Data Structures and Other Objects and Other Objects Using Java Using Java

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Chapter 2 introduces Object Chapter 2 introduces Object Oriented Programming.Oriented Programming.

OOP is a relatively new OOP is a relatively new approach to programming approach to programming which supports the creation of which supports the creation of new data types and operations new data types and operations to manipulate those types.to manipulate those types.

This presentation introduces This presentation introduces OOP.OOP.

Object OrientedProgrammingObject OrientedProgramming

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

What is this Object ?What is this Object ?

There is no real There is no real answer to the question, answer to the question, but we’ll call it a but we’ll call it a “thinking cap”.“thinking cap”.

The plan is to describe The plan is to describe a thinking cap by a thinking cap by telling you what telling you what actions can be done to actions can be done to it.it.

Using the Object’s SlotsUsing the Object’s Slots

You may put a piece of You may put a piece of paper in each of the two paper in each of the two slots (green and red), with a slots (green and red), with a sentence written on each.sentence written on each.

You may push the green You may push the green button and the thinking cap button and the thinking cap will speak the sentence from will speak the sentence from the green slot’s paper.the green slot’s paper.

And same for the red button.And same for the red button.

ExampleExample

ExampleExample

That test was a breeze !

ExampleExample

I shouldstudy harder !

Thinking Cap ImplementationThinking Cap Implementation

We can implement the We can implement the thinking cap using a thinking cap using a data type called a data type called a classclass..

public class ThinkingCap {

. . .

}

Thinking Cap ImplementationThinking Cap Implementation

The class will have two The class will have two components called components called greenWordsgreenWords and and redWordsredWords. These . These compnents are strings compnents are strings which hold the which hold the information that is information that is placed in the two slots.placed in the two slots.

Using a class permits Using a class permits two features . . .two features . . .

public class ThinkingCap { String greenWords; String redWords; . . . }

Thinking Cap ImplementationThinking Cap Implementation

The two components The two components will be will be private private instance variablesinstance variables. . This ensures that This ensures that nobody can directly nobody can directly access this access this information. The only information. The only access is through access is through methods that we methods that we provide for the class.provide for the class.

public class ThinkingCap { private char greenWords; private char redWords; . . .}

Thinking Cap ImplementationThinking Cap Implementation

In a class, the In a class, the methods which methods which manipulate the class manipulate the class are also listed.are also listed.

class ThinkingCap { private char greenWords; private char redWords; . . .}

Implementations of Implementations of the thinking cap the thinking cap methods go here.methods go here.

Thinking Cap ImplementationThinking Cap Implementation

public class ThinkingCap { private char greenWords; private char redWords;

public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )...

}

Our thinking cap has at least three methods:Our thinking cap has at least three methods:

Thinking Cap ImplementationThinking Cap Implementation

package edu.colorado.simulations;public class ThinkingCap { private char greenWords; private char redWords;

public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )...

}

The code for a new class is generally put in a Java The code for a new class is generally put in a Java package, as shown here:package, as shown here:

This means that

ThinkingCap.java and

ThinkingCap.class files

will be in a subdirectory

edu/colorado/simulations

Using the Thinking CapUsing the Thinking Cap

A program that A program that wants to use the wants to use the thinking cap can thinking cap can importimport the the ThinkingCap ThinkingCap class.class.

import edu.colorado.simulations.ThinkingCap;

...

Using the Thinking CapUsing the Thinking Cap

Just for fun, the Just for fun, the example program example program will declare two will declare two ThinkingCap ThinkingCap variables named variables named student and fan.student and fan.

import edu.colorado.simulations.ThinkingCap;

public class Example{ public static void main( ) { ThinkingCap student; ThinkingCap fan;

Using the Thinking CapUsing the Thinking Cap

The variables are The variables are examples of examples of reference variables, reference variables, which means that which means that they have the they have the capability of capability of refering to refering to ThinkingCap objects ThinkingCap objects that we create with that we create with the new operator.the new operator.

import edu.colorado.simulations.ThinkingCap;

public class Example{ public static void main( ) { ThinkingCap student; ThinkingCap fan;

student = new ThinkingCap( ); fan = new ThinkingCap( );

Using the Thinking CapUsing the Thinking Cap

Once the Once the ThinkingCaps ThinkingCaps are created, we are created, we can activate can activate methods such as methods such as slot for the slot for the student thinking student thinking cap.cap.

import edu.colorado.simulations.ThinkingCap;

public class Example{ public static void main( ) { ThinkingCap student; ThinkingCap fan;

student = new ThinkingCap( ); fan = new ThinkingCap( );

student.slots( "Hello", "Bye");

Using the Thinking CapUsing the Thinking Cap

Once the Once the ThinkingCaps ThinkingCaps are created, we are created, we can can activateactivate methods such as methods such as slot for the slot for the student thinking student thinking cap.cap.

import edu.colorado.simulations.ThinkingCap;

public class Example{ public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan;

student = new ThinkingCap( ); fan = new ThinkingCap( );

student.slots( "Hello", "Bye");

Using the Thinking CapUsing the Thinking Cap

The method The method activation activation consists of four consists of four parts, starting parts, starting with the variable with the variable name.name. student.slots( "Hello", "Bye");

Name of the variable

Name of the variable

Using the Thinking CapUsing the Thinking Cap

The variable The variable name is followed name is followed by a period.by a period.

student.slots( "Hello", "Bye");A

Per

iod

A P

erio

d

Using the Thinking CapUsing the Thinking Cap

After the period After the period is the name of is the name of the method that the method that you are you are activating.activating. student.slots( "Hello", "Bye");

Name of the method

Name of the method

Using the Thinking CapUsing the Thinking Cap

Finally, the Finally, the arguments for the arguments for the method. In this method. In this example the first example the first argument argument (newGreen) is (newGreen) is "Hello" and the "Hello" and the second argument second argument (newRed) is (newRed) is "Bye"."Bye".

student.slots( "Hello", "Bye");

Arg

umen

ts

Arg

umen

ts

A QuizA Quiz

How would you How would you activate student's activate student's pushGreen method ?pushGreen method ?

What would be the What would be the output of student's output of student's pushGreen method at pushGreen method at this point in the this point in the program ?program ?

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan;

student = new ThinkingCap( ); fan = new ThinkingCap( );

student.slots( "Hello", "Bye");

A QuizA Quiz

Notice that the Notice that the pushGreen pushGreen method method has no arguments.has no arguments.

At this point, At this point, activating activating student.pushGreen student.pushGreen

will print the stringwill print the string

HelloHello..

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan;

student = new ThinkingCap( ); fan = new ThinkingCap( );

student.slots( "Hello", "Bye"); student.pushGreen( );

A QuizA Quiz

Trace through this Trace through this program, and tell program, and tell me the complete me the complete output.output.

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( );

student.slots( "Hello", "Bye");

fan.slots( "Go Cougars!", "Boo!");

student.pushGreen( );

fan.pushGreen( );

student.pushRed( ); . . .

A QuizA Quiz

HelloHello

Go Cougars!Go Cougars!

ByeBye

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( );

student.slots( "Hello", "Bye");

fan.slots( "Go Cougars!", "Boo!");

student.pushGreen( );

fan.pushGreen( );

student.pushRed( ); . . .

What you know about ObjectsWhat you know about Objects

Class = Data + Methods.Class = Data + Methods. You know how to write a new class type, and You know how to write a new class type, and

place the new class in a package.place the new class in a package. You know how to import the class into a You know how to import the class into a

program that uses class type.program that uses class type. You know how to activate methods.You know how to activate methods. But you still need to learn how to write the But you still need to learn how to write the

implementations of a class’s methods.implementations of a class’s methods.

Thinking Cap ImplementationThinking Cap Implementation

public class ThinkingCap { private String greenWords; private String redWords;

public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )...

}

We will look at the body of slots, which must copy its We will look at the body of slots, which must copy its two arguments to the two private instance variables.two arguments to the two private instance variables.

Thinking Cap ImplementationThinking Cap Implementation

public void slots(String newGreen, String newRed){ greenWords = newGreen; redWords = newRed;}

The method’s implementation occurs after the The method’s implementation occurs after the parameter listparameter list

There is one feature about a method’s There is one feature about a method’s implementation . . .implementation . . .

Thinking Cap ImplementationThinking Cap Implementation

public void slots(String newGreen, String newRed){ greenWords = newGreen; redWords = newRed;}

Within the body of the method, the class’s instance variables Within the body of the method, the class’s instance variables and other methods may all be accessed.and other methods may all be accessed.

Thinking Cap ImplementationThinking Cap Implementation

?

Within the body of the method, the class’s instance variables Within the body of the method, the class’s instance variables and other methods may all be accessed.and other methods may all be accessed.

public void slots(String newGreen, String newRed){ greenWords = newGreen; redWords = newRed;}

But, whose instance variables are

these? Are they

student.greenWords

student.redWords

fan.greenWords

fan.redWords

Thinking Cap ImplementationThinking Cap Implementation

Within the body of the method, the class’s instance variables Within the body of the method, the class’s instance variables and other methods may all be accessed.and other methods may all be accessed.

public void slots(String newGreen, String newRed){ greenWords = newGreen; redWords = newRed;}

If we activate student.slots:

student.greenWords

student.redWords

Thinking Cap ImplementationThinking Cap Implementation

public void slots(String newGreen, String newRed){ greenWords = newGreen; redWords = newRed;}

If we activate fan.slots:

fan.greenWords

fan.redWords

Within the body of the method, the class’s instance variables Within the body of the method, the class’s instance variables and other methods may all be accessed.and other methods may all be accessed.

Thinking Cap ImplementationThinking Cap Implementation

public void pushGreen( ){ System.out.println(greenWords);}

Here is the implementation of the pushGreen method, Here is the implementation of the pushGreen method, which prints the green Words:which prints the green Words:

Thinking Cap ImplementationThinking Cap Implementation

Here is the implementation of the pushGreen method, Here is the implementation of the pushGreen method, which prints the green Words:which prints the green Words:

Notice how this method implementation uses the Notice how this method implementation uses the greenWords instance variable of the object.greenWords instance variable of the object.

public void pushGreen( ){ System.out.println(greenWords);}

A Common PatternA Common Pattern

Often, one or more methods will place data in Often, one or more methods will place data in the instance variables...the instance variables...

public class ThinkingCap { private String greenWords; private String redWords; ...}

...so that other methods may use that data....so that other methods may use that data.

slots pushGreen & pushRed

Classes have instance variables and methods. An Classes have instance variables and methods. An object is a variable where the data type is a class.object is a variable where the data type is a class.

You should know how to declare a new class type, You should know how to declare a new class type, how to implement its methods, how to use the how to implement its methods, how to use the class type.class type.

Frequently, the methods of an class type place Frequently, the methods of an class type place information in the instance variables, or use information in the instance variables, or use information that's already in the instance variables.information that's already in the instance variables.

In the future we will see more features of OOP.In the future we will see more features of OOP.

Summary Summary

THE ENDTHE END

Presentation copyright 1999, Addison Wesley LongmanPresentation copyright 1999, Addison Wesley LongmanFor use with For use with Data Structures and Other Objects Using JavaData Structures and Other Objects Using Javaby Michael Mainby Michael Main

Some artwork in the presentation is used with permission from Presentation Task ForceSome artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubGraphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).

Students and instructors who use Students and instructors who use Data Structures and Other ObjectsData Structures and Other Objects Using Java Using Java arearewelcome to use this presentation however they see fit, so long as this copyright notice welcome to use this presentation however they see fit, so long as this copyright notice remains intact.remains intact.