Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 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. This lecture is an introduction to classes, telling what classes are and how they are implemented in Java. The introduction is basic, not covering constructors or details about reference variables that are covered in the text. The best time for this lecture is just before students read Chapter 2--perhaps as early as the second day of class. Before this lecture, students should have a some understanding of 1. Java Strings, and 2. The basic form of a Java application program. Data Structures and Other Objects Using Java

2 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. This lecture will introduce you to object-oriented programming by using one example, which we'll call a "thinking cap".

3 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. The important thing about this thinking cap is that there are three actions which may happen to it. The three actions are described here.

4 Example Here's an example of how the first action works. Words are written on two slips of paper, and the words are inserted in the two slots.

5 Example That test was a breeze !
Once the words have been inserted, either of the buttons may be pressed. When the green button is pressed, the Words from the green slip of paper is spoken.

6 Example I should study harder !
When the red button is pressed, the words from the red slip of paper is spoken. By the way, what would be an appropriate precondition for pressing the red button? Answer: Before the button is pressed, the slips of paper should be inserted in the slots.

7 Thinking Cap Implementation
We can implement the thinking cap using a data type called a class. public class ThinkingCap { . . . } We will implement the thinking cap in Java using a feature called a class.

8 Thinking Cap Implementation
The class will have two components called greenWords and redWords. These compnents are strings which hold the information that is placed in the two slots. Using a class permits two features . . . public class ThinkingCap { String greenWords; String redWords; . . . } The particular class we have in mind has two components that store the two sentences that are inserted in those slots. These components can be declared Strings in Java. All of you have used classes before in your programming, but you might not have realized exactly what features a Java class supports...

9 Thinking Cap Implementation
The two components will be private instance variables. This ensures that nobody can directly access this information. The only access is through methods that we provide for the class. public class ThinkingCap { private char greenWords; private char redWords; . . . } The first class feature is that class components are allowed to be private components. The advantage of private components is that they prevent certain programmers from accessing the components directly. Instead, programmers are forced to use only through the operations that we provide.

10 Thinking Cap Implementation
In a class, the methods which manipulate the class are also listed. class ThinkingCap { private char greenWords; private char redWords; . . . } In a class, the operations to manipulate the data are actually part of the class itself. An implementation of each method is placed as part of the class definition. Implementations of the thinking cap methods go here.

11 Thinking Cap Implementation
Our thinking cap has at least three methods: public class ThinkingCap { private char greenWords; private char redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... } Our ThinkingCap class has three methods to carry out the actions that we’ve already discussed.

12 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 ThinkingCap { private char greenWords; private char redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... } This means that ThinkingCap.java and ThinkingCap.class files will be in a subdirectory edu/colorado/simulations When we implement a class, we’ll put the new class in a new file. The name of the file must match the name of the class. For example, the ThinkingCap class will be in a file called ThinkingCap.java. In order to easily use a new class, it helps to organize classes into groups called packages. To avoid conflicts, the first part of the package names designed to match the name of your internet domain in reverse order. For example, the author of your text comes from colorado.edu, so he might put his work in a package called edu.colorado.simulations. Once you decide on a package name, the .java file and other associated files should go in subdirectories that have the same structure as the package name. In this example, you would start at your main directory for source code. Below that you would create a subdirectory edu, below edu you would create colorado, and below colorado you would create a subdirectory called simulations. The ThinkingCap.java file is placed in the simulations subdirectory, and when you compile it, the ThinkingCap.class file will also go in this subdirectory.

13 Using the Thinking Cap A program that wants to use the thinking cap can import the ThinkingCap class. import edu.colorado.simulations.ThinkingCap; ... Any program that uses a class can have an import statement indicating the name of the package and class.

14 Using the Thinking Cap Just for fun, the example program will declare two ThinkingCap variables named student and fan. import edu.colorado.simulations.ThinkingCap; public class Example { public static void main( ) ThinkingCap student; ThinkingCap fan; After the import statement, we may declare and use variables of the ThinkingCap data type, using just the short name ThinkingCap (without its complete package name). This example actually has two ThinkingCap variables, named student and fan.

15 Using the Thinking Cap The variables are examples of reference variables, which means that they have the capability of refering to ThinkingCap objects that we create with 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( ); These variables are examples of reference variables, which are discussed in Chapter 2. This means that the variables are capable of referring to ThinkingCap objects that we create with the new operator. Here is an example of how we can create two new ThinkingCap objects that student and fan will refer to.

16 Using the Thinking Cap Once the ThinkingCaps are created, we can activate methods such as slot for the student thinking 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"); This illustrates how we call one of the ThinkingCap methods for the student object.

17 Using the Thinking Cap Once the ThinkingCaps are created, we can activate methods such as slot for the student thinking 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"); But, again, let's use the usual OOP terminology, so that instead of saying that we are calling a method we say that we are activating a method. In particular, we are activating the slots method of the student object. (If you go to a cocktail party and tell your OOP friends that today you called a method for an object, they will laugh behind your back. It is better to impress them by saying that you activated a method, even though it’s just jargon.)

18 Using the Thinking Cap The method activation consists of four parts, starting with the variable name. student.slots( "Hello", "Bye"); The complete activation consists of four parts, beginning with the object name. Name of the variable

19 Using the Thinking Cap The variable name is followed by a period.
student.slots( "Hello", "Bye"); The object name is followed by a period. A Period

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

21 Using the Thinking Cap Finally, the arguments for the method. In this example the first argument (newGreen) is "Hello" and the second argument (newRed) is "Bye". student.slots( "Hello", "Bye"); Arguments And finally there is the argument list. In the case of the slots method, there are two String arguments: newGreen (which is given the actual value "Hello" in this example) and newRed (which is given the actual value "Bye" in this example).

22 A Quiz How would you activate student's pushGreen method ?
What would be the output of student's pushGreen method at this point in the program ? public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); Go ahead and write your answers before I move to the next slide.

23 A Quiz Notice that the pushGreen method has no arguments.
At this point, activating student.pushGreen will print the string Hello. public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); student.pushGreen( ); Remember that the pushGreen method does not have any arguments, so the argument list is just a pair of parentheses.

24 A Quiz Trace through this program, and tell me the complete 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( ); . . . Here's a longer program. What is the complete output? Again, write your answers before I move to the next slide.

25 A Quiz Hello Go Cougars! Bye 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( ); . . . The important thing to notice is that student and fan are separate objects of the ThinkingCap class. Each has its own greenWords and redWords data. Or to throw one more piece of jargon at you: Each has its own greenWords and redWords instance variables. instance variables are the data portion of a class. The activation of student.slots fills in the greenWords and redWords data for student, and the activation of fan.slots fills in the greenWords and redWords data for fan. Once these instance variables are filled in, we can activate the pushGreen and pushRed methods. For example, student.pushGreen accesses the greenWords instance variable of student, whereas fan.pushGreen accesses the greenWords instance variable of fan.

26 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. You now know quite a bit about OOP -- but the key missing piece is how to implement a class’s methods.

27 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 ThinkingCap { private String greenWords; private String redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... } We'll start by looking at the implementation of the slots method. The work which the method must accomplish is small: Copy the two arguments (newGreen and newRed) to the two private instance variables of the object (greenWords and redWords).

28 Thinking Cap Implementation
The method’s implementation occurs after the parameter list public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } For the most part, all that's needed is a pair of assignments to assign the two arguments (newGreen and newRed) to the two instance variables (greenWords and redWords). By the way, how many of you have used Java Strings before? But the more interesting part of this implementation is a key feature that you need to know about. There is one feature about a method’s implementation . . .

29 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 newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } Within the body of the method, we may access any of the members of the object. In this example, we are accessing both the greenWords and the redWords instance variables, by assigning values to these instance variables.

30 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 these? Are they student.greenWords student.redWords fan.greenWords fan.redWords public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } The use of these instance variables is a bit confusing. Which instance variables are we talking about? student.greenWords and student.redWords? Or are we referring to fan.greenWords and fan.redWords? Or instance variables of some other object? ?

31 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: student.greenWords student.redWords public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } The answer depends on which object has activated its method. If student.slots is activated, then these two instance variables will refer to student.greenWords and student.redWords.

32 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: fan.greenWords fan.redWords public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } But if fan.slots is activated, then the two instance variables refer to fan.greenWords and fan.redWords.

33 Thinking Cap Implementation
Here is the implementation of the pushGreen method, which prints the green Words: public void pushGreen( ) { System.out.println(greenWords); } Here's the implementation of the pushGreen method.

34 Thinking Cap Implementation
Here is the implementation of the pushGreen method, which prints the green Words: public void pushGreen( ) { System.out.println(greenWords); } The important thing to notice is how the method’s implementation uses the greenWords instance variable of the object. If we activate student.pushGreen, then the method will use student.greenWords. And if we activate fan.pushGreen, then the method will use fan.greenWords. Notice how this method implementation uses the greenWords instance variable of the object.

35 A Common Pattern Often, one or more methods will place data in the instance variables... public class ThinkingCap { private String greenWords; private String redWords; ... } slots pushGreen & pushRed The methods of the ThinkingCap are all simple, but they do illustrate a common pattern: Often a method (such as slots) will place information in the private instance variables, so that other const methods (such as pushGreen and pushRed) may access the information in those instance variables. ...so that other methods may use that data.

36 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. A quick summary This presentation has only introduced classes. You should read all of Chapter 2 to get a better understanding of classes. Pay particular attention to the notion of a constructor, which is a special method that can automatically initialize the instance variables of an object. Also pay attention to the more advanced features such as the clone and equals methods with the Location class given in Chapter 2.

37 THE END 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, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool 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


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google