Object Oriented Programming Chapter 2 introduces Object Oriented

  • Slides: 37
Download presentation
Object Oriented Programming Chapter 2 introduces Object Oriented Programming. OOP is a relatively new

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

What is this Object ? There is no real answer to the question, but

What is this Object ? There is no real answer to the question, but we’ll call it a “thinking cap”. The plan is to describe a thinking cap by telling you what actions can be done to it.

Using the Object’s Slots You may put a piece of paper in each of

Using the Object’s Slots You may put a piece of paper in each of the two slots (green and red), with a sentence written on each. You may push the green button and the thinking cap will speak the sentence from the green slot’s paper. And same for the red button.

Example

Example

Example That test was a breeze !

Example That test was a breeze !

Example I should study harder !

Example I should study harder !

Thinking Cap Implementation We can implement the thinking cap using a data type called

Thinking Cap Implementation We can implement the thinking cap using a data type called a class. public class Thinking. Cap {. . . }

Thinking Cap Implementation The class will have public class Thinking. Cap two components called

Thinking Cap Implementation The class will have public class Thinking. Cap two components called { String green. Words; green. Words and String red. Words; red. Words. These. . . compnents are strings which hold the } information that is placed in the two slots. Using a class permits two features. . .

Thinking Cap Implementation The two components public class Thinking. Cap will be private {

Thinking Cap Implementation The two components public class Thinking. Cap will be private { private char green. Words; instance variables. private char red. Words; This ensures that. . . nobody can directly } access this information. The only access is through methods that we provide for the class.

Thinking Cap Implementation In a class, the methods which manipulate the class are also

Thinking Cap Implementation In a class, the methods which manipulate the class are also listed. Implementations of the thinking cap methods go here. class Thinking. Cap { private char green. Words; private char red. Words; . . . }

Thinking Cap Implementation Our thinking cap has at least three methods: public class Thinking.

Thinking Cap Implementation Our thinking cap has at least three methods: public class Thinking. Cap { private char green. Words; private char red. Words; public void slots(String new. Green, String new. Red). . . public void push. Green( ). . . public void push. Red( ). . . }

Thinking Cap Implementation The code for a new class is generally put in a

Thinking Cap Implementation The code for a new class is generally put in a Java package, as shown here: package edu. colorado. simulations; public class Thinking. Cap { Th This Th inki me private char green. Words; wi inkin ng. C ans ed ll be g. C ap. ja that private char red. Words; a u /co in a p. c va las an lor s u ad bd s fi d o public void slots(String new. Green, String new. Red). . . /si irec les mu to lat ry public void push. Green( ). . . ion s public void push. Red( ). . . }

Using the Thinking Cap A program that wants to use thinking cap can import

Using the Thinking Cap A program that wants to use thinking cap can import the Thinking. Cap class. import edu. colorado. simulations. Thinking. Cap; . . .

Using the Thinking Cap Just for fun, the example program will declare two Thinking.

Using the Thinking Cap Just for fun, the example program will declare two Thinking. Cap variables named student and fan. import edu. colorado. simulations. Thinking. Cap; public class Example { public static void main( ) { Thinking. Cap student; Thinking. Cap fan;

Using the Thinking Cap The variables are examples of reference variables, which means that

Using the Thinking Cap The variables are examples of reference variables, which means that they have the capability of refering to Thinking. Cap objects that we create with the new operator. import edu. colorado. simulations. Thinking. Cap ; public class Example { public static void main( ) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( );

Using the Thinking Cap Once the Thinking. Caps are created, we can activate methods

Using the Thinking Cap Once the Thinking. Caps are created, we can activate methods such as slot for the student thinking cap. import edu. colorado. simulations. Thinking. Cap ; public class Example { public static void main( ) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( ); student. slots( "Hello", "Bye");

Using the Thinking Cap Once the Thinking. Caps are created, we can activate methods

Using the Thinking Cap Once the Thinking. Caps are created, we can activate methods such as slot for the student thinking cap. import edu. colorado. simulations. Thinking. Cap; public class Example { public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( ); student. slots( "Hello", "Bye");

Using the Thinking Cap The method activation consists of four parts, starting with the

Using the Thinking Cap The method activation consists of four parts, starting with the variable name. e m Na he t f o v le b a ari . student slots( "Hello", "Bye");

Using the Thinking Cap The variable name is followed by a period. . AP

Using the Thinking Cap The variable name is followed by a period. . AP eri od student slots( "Hello", "Bye");

Using the Thinking Cap After the period is the name of the method that

Using the Thinking Cap After the period is the name of the method that you are activating. . student slots( "Hello", "Bye"); Na m e of th e m et ho d

en gu m Ar Finally, the arguments for the method. In this example the

en gu m Ar Finally, the arguments for the method. In this example the first argument (new. Green) is "Hello" and the second argument (new. Red) is "Bye". ts Using the Thinking Cap . student slots( "Hello", "Bye");

A Quiz How would you activate student's push. Green method ? What would be

A Quiz How would you activate student's push. Green method ? What would be the output of student's push. Green method at this point in the program ? public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( ); . student slots( "Hello", "Bye");

A Quiz Notice that the push. Green method has no arguments. At this point,

A Quiz Notice that the push. Green method has no arguments. At this point, activating student. push. Green will print the string Hello. public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( ); . student slots( "Hello", "Bye"); student. push. Green( );

A Quiz public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap

A Quiz public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( ); student. slots( "Hello", "Bye"); fan. slots( "Go Cougars!", "Boo!"); student. push. Green( ); fan. push. Green( ); student. push. Red( ); . . . Trace through this program, and tell me the complete output.

A Quiz public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap

A Quiz public static void main(String[ ] args) { Thinking. Cap student; Thinking. Cap fan; student = new Thinking. Cap( ); fan = new Thinking. Cap( ); student. slots( "Hello", "Bye"); fan. slots( "Go Cougars!", "Boo!"); student. push. Green( ); fan. push. Green( ); student. push. Red( ); . . . Hello Go Cougars! Bye

What you know about Objects Class = Data + Methods. You know how to

What you know about Objects Class = Data + Methods. You know how to write a new class type, and place the new class in a package. You know how to import the class into a program that uses class type. You know how to activate methods. But you still need to learn how to write the implementations of a class’s methods.

Thinking Cap Implementation We will look at the body of slots, which must copy

Thinking Cap Implementation We will look at the body of slots, which must copy its two arguments to the two private instance variables. public class Thinking. Cap { private String green. Words; private String red. Words; public void slots(String new. Green, String new. Red). . . public void push. Green( ). . . public void push. Red( ). . . }

Thinking Cap Implementation The method’s implementation occurs after the parameter list public void slots(String

Thinking Cap Implementation The method’s implementation occurs after the parameter list public void slots(String new. Green, String new. Red) { green. Words = new. Green; red. Words = new. Red; } There is one feature about a method’s implementation. . .

Thinking Cap Implementation Within the body of the method, the class’s instance variables and

Thinking Cap Implementation Within the body of the method, the class’s instance variables and other methods may all be accessed. public void slots(String new. Green, String new. Red) { green. Words = new. Green; red. Words = new. Red; }

Thinking Cap Implementation Within the body of the method, the class’s instance variables and

Thinking Cap Implementation Within the body of the method, the class’s instance variables and other methods may all be accessed. But, whose instance variables are public void slots(String new. Green, String new. Red) these? Are they { student. green. Words = new. Green; red. Words = new. Red; student. red. Words } fan. green. Words fan. red. Words ?

Thinking Cap Implementation Within the body of the method, the class’s instance variables and

Thinking Cap Implementation Within the body of the method, the class’s instance variables and other methods may all be accessed. If we activate student. slots: public void slots(String new. Green, String new. Red) student. green. Words { student. red. Words green. Words = new. Green; red. Words = new. Red; }

Thinking Cap Implementation Within the body of the method, the class’s instance variables and

Thinking Cap Implementation Within the body of the method, the class’s instance variables and other methods may all be accessed. If we activate fan. slots: public void slots(String new. Green, String new. Red) fan. green. Words { fan. red. Words green. Words = new. Green; red. Words = new. Red; }

Thinking Cap Implementation Here is the implementation of the push. Green method, which prints

Thinking Cap Implementation Here is the implementation of the push. Green method, which prints the green Words: public void push. Green( ) { System. out. println(green. Words); }

Thinking Cap Implementation Here is the implementation of the push. Green method, which prints

Thinking Cap Implementation Here is the implementation of the push. Green method, which prints the green Words: public void push. Green( ) { System. out. println(green. Words); } Notice how this method implementation uses the green. Words instance variable of the object.

A Common Pattern Often, one or more methods will place data in the instance

A Common Pattern Often, one or more methods will place data in the instance variables. . . slots public class Thinking. Cap { private String green. Words; private String red. Words; . . . } push. Green & push. Red . . . so that other methods may use that data.

Summary Classes have instance variables and methods. An object is a variable where the

Summary Classes have instance variables and methods. An object is a variable where the data type is a class. You should know how to declare a new class type, how to implement its methods, how to use the class type. Frequently, the methods of an class type place information in the instance variables, or use information that's already in the instance variables. In the future we will see more features of OOP.

Presentation copyright 1999, Addison Wesley Longman For use with Data Structures and Other Objects

Presentation copyright 1999, Addison Wesley Longman For use with Data Structures and Other Objects Using Java by Michael Main Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc. ) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3 G Graphics Inc. , Archive Arts, Cartesia Software, Image Club Graphics Inc. , One Mile Up Inc. , Tech. Pool Studios, Totem Graphics Inc. ). Students and instructors who use Data Structures and Other Objects Using Java are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. THE END