P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
Object Oriented Programming Data Structures and Other Objects Using C++ Dr. Donghyun Kim Department of Computer Science College of Computing and Software.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Object Oriented Programming
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Object Oriented Programming
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

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

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

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

Example

That test was a breeze !

Example I should study harder !

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

Thinking Cap Implementation p 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. p Using a class permits two features... public class ThinkingCap { String greenWords; String redWords;... }

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;... }

Thinking Cap Implementation In a class, the methods which manipulate the class are also listed. class ThinkingCap { private char greenWords; private char redWords;... } Implementations of the thinking cap methods go here.

Thinking 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:

Thinking 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 package, as shown here: This means that ThinkingCap.java and ThinkingCap.class files will be in a subdirectory edu/colorado/simulations

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

Using the Thinking Cap p 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;

Using the Thinking Cap p 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( );

Using the Thinking Cap p 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");

Using the Thinking Cap p 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");

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

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

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

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

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");

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( );

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( );...

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( );...

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 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 two arguments to the two private instance variables.

Thinking Cap Implementation public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } The method’s implementation occurs after the parameter list There is one feature about a method’s implementation...

Thinking Cap Implementation public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } Within the body of the method, the class’s instance variables and other methods may all be accessed.

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; } But, whose instance variables are these? Are they student.greenWords student.redWords fan.greenWords fan.redWords

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; } If we activate student.slots: student.greenWords student.redWords

Thinking 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 and other methods may all be accessed.

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

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

A Common Pattern p Often, one or more methods will place data in the instance variables... public class ThinkingCap { private String greenWords; private String redWords;... } p...so that other methods may use that data. slots pushGreen & pushRed

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

T HE E ND 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.