Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Air Force Institute of Technology Electrical and Computer Engineering
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 9 Imperative and object-oriented languages 1.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Hash Tables1 Part E Hash Tables  
Fundamental Programming Structures in Java: Strings.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 2 Data Types, Declarations, and Displays
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
5-1 Data types: values, operations, and data representation. Abstract data type: values and operations only. Requirements, contract, implementation(s).
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Course: Object Oriented Programming - Abstract Data Types Unit1: IntroductionSlide Number 1 Introduction Course: Object Oriented Programming Abstract Data.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
I Power Int 2 Computing Software Development High Level Language Constructs.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Hash Tables1   © 2010 Goodrich, Tamassia.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Pointers OVERVIEW.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Introduction to Java Java Translation Program Structure
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
© 2004 Goodrich, Tamassia Hash Tables1  
Chapter 7: Characters, Strings, and the StringBuilder.
6 Strings and Tuples © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Chapter 16 Linked Structures
CS150 Introduction to Computer Science 1
Data Structures & Algorithms
Visual Programming COMP-315
COMS 261 Computer Science I
Presentation transcript:

Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq

Prof A Alkhorabi 3- 2 Data types: values, operations, and data representation (in memory). Abstract data type: values and operations only. Requirements, contract, implementation(s). Design of abstract data types. String abstract data types. Abstract data types in the C++ class library. Abstract Data Types Overview

Prof A Alkhorabi 3- 3 Data types We classify all data into data types, such as: –Floating-point numbers –integers –objects of various classes. Each data type is characterized by: –a set of values –a data representation (which is common to all these values) –a set of operations (which can be applied uniformly to all these values).

Prof A Alkhorabi 3- 4 C++ built-in data types Type24ValuesData representation Operations bool false, true1 byte || && ! char Unicode characters2 bytes (as for int ) int negative, zero, positive whole numbers 4 bytes + - * / % == etc. float negative, zero, positive floating-point numbers 4 bytes floating- point + - * / double negative, zero, positive floating-point numbers 8 bytes floating- point + - * /

Prof A Alkhorabi 3- 5 Introducing new data types To introduce a new data type, we must define its values, data representation, and operations. In C++, use a class declaration: –The class’s instance variables determine the values and data representation. –The class’s constructors and methods are the operations. Each object of the class: –has those instance variables –is created by one of those constructors –may be inspected and/or updated by any of those methods.

Prof A Alkhorabi 3- 6 Abstract data types An abstract data type (ADT) is characterized by: –a set of values –a set of operations. It is not characterized by its data representation. The data representation is private, so application code cannot access it. (Only the operations can.) The data representation is changeable, with no effect on application code. (Only the operations must be recoded.)

Prof A Alkhorabi 3- 7 ADT specification Each ADT should have a contract that: –specifies the set of values of the ADT –specifies each operation of the ADT (i.e., the operation’s name, parameter type(s), result type, and observable behavior). The contract does not specify: –the data representation, nor –the algorithms used to implement the operations.

Prof A Alkhorabi 3- 8 ADT specification The ADT programmer undertakes to provide an implementation of the ADT The application programmer undertakes to process values of the ADT using only the operations specified Separation of concerns: –The ADT programmer is not concerned with what applications the ADT is used for. –The application programmer is not concerned with how the ADT is implemented. Separation of concerns is essential for designing and implementing large systems.

Prof A Alkhorabi 3- 9 Example: Contract for Date ADT Assumed application requirements: 1)The values must be all past, present, and future dates. 2)It must be possible to construct a date from year number y, month number m, and day-in-month number d. 3)It must be possible to compare dates. 4)It must be possible to put a date in ISO format “y-m-d”. 5)It must be possible to advance a date by n days.

Prof A Alkhorabi Example: Specifications for Date ADT Possible specifications (contract), expressed as an outline class declaration: class Date { // Each Date value is a past, present, or future date. private … ; public Date (int y, int m, int d); // Construct a date with year y, month m, and day-in-month d. public int compareTo (Date that); // Return –1 if this date is earlier than that, // or 0 if this date is equal to that, // or +1 if this date is later than that.

Prof A Alkhorabi Example: Specifications for Date ADT public String toString (); // Return this date rendered in ISO format. public void advance (int n); // Advance this date by n days (where n ≥ 0). }

Prof A Alkhorabi Example: Specifications for Date ADT Possible application code: Date today = … ; Date DOB = new Date(2001, 4, 15); today.advance(16); if (today.compareTo(DOB) < 0) cout << today.toString();

Prof A Alkhorabi ADT implementation An implementation of an ADT entails: –choosing a data representation –choosing an algorithm for each operation. The data representation must be private. The data representation must cover all possible values. The operations algorithms must be consistent with the data representation. جواب 2

Prof A Alkhorabi Example: implementation of Date ADT Class declaration: class Date { // Each Date value is a past, present, or future date. // This date is represented by a year number year, a month number // month, and a day-in-month number day : private: int year, month, day; public: Date (int y, int m, int d) { // Construct a date with year y, month m, and day-in-month d. year = y; month = m; day = d; }

Prof A Alkhorabi Example: implementation of Date ADT Class declaration (continued): int compareTo (Date that) { // Return –1 if this date is earlier than that, // or 0 if this date is equal to that, // or +1 if this date is later than that. return (year that.year ? +1 : month that.month ? +1 : day that.day ? +1 : 0); }

Prof A Alkhorabi Example: implementation of Date ADT Class declaration (continued): String toString () { // Return this date in ISO format. return newFormatDate; // year + '-' + month + '-‘ + day } void advance (int n) { // Advance this date by n days (where n ≥ 0). … } }

Prof A Alkhorabi ADT design Operations are sufficient if together they meet all the ADT’s requirements. –Can the application be written entirely in terms of calls to these operations? An operation is necessary if it is not surplus to the ADT’s requirements. –Could the operation be safely omitted? A well-designed ADT provides operations that are necessary and sufficient for its requirements.

Prof A Alkhorabi Example: design of Date ADT Recall the Date specification : class Date { private: … ; public: Date (int y, int m, int d); int compareTo (Date that); String toString (); void advance (int n); } These operations are sufficient. All these operations are necessary.

Prof A Alkhorabi Example: design of Date ADT Consider another possible Date specification: public class Date { private: … ; public: Date (int y, int m, int d); int getYear (); int getMonth (); int getDay (); } These operations are sufficient. (Date comparison and rendering are clumsier, but still possible.) All these operations are necessary.

Prof A Alkhorabi Example: design of Date ADT Consider yet another possible Date specification : public class Date { private: … ; public: Date (int y, int m, int d); int compareTo (Date that); String toString (); void advance (int n); void advance1Day (); } Operation advance1Day is unnecessary.

Prof A Alkhorabi Example: Date data type A Date class can be built as an ADT: A Date class that may represent any date (past, present, and future) can be declared as follows: Each object of the class: –a set of values: values for integer types, to represent the Date day, month, and year –a set of operations: class constructors, and methods to manipulate and inspect and/or update the set of values, isLeap (), equals (), compareTo (), length() and advance(). In C++ an ADT is implemented as a class.

Prof A Alkhorabi Example: Date data type Class declaration: class Date { // Each Date past, present, or future date, is represented by year, month & a day private : int day, month, year; public : Date (int d, int m, int y) { // Construct a date with year y, month m, and day-in-month d. // Throw an exception if they constitute an improper date. if (d 31 || m 12) { cout << "badly formed date" << endl; throw; } day = d; month = m; year = y; } int isLeap (int y) { // Return true if and only if y is a leap year. return (y%4 == 0 && (y%100 != 0 || y%400 == 0)); }

Prof A Alkhorabi Example: Date data type Class declaration : int equals (Date that) { // Return true if this date equal to that, or false otherwise. return (year == that.year && month == that.month && day == that.day); } int compareTo (Date that) { // Return -1 if this date comes before that, // or 0 if this date is equal to that, // or +1 if this date come after that. return (year < that.year ? -1 : year > that.year ? +1 : month < that. month ? -1 : month < that. month ? +1 : day < that.day ? -1 : day > that.day ? +1 : 0); } Continue …

Prof A Alkhorabi Example: Date data type Class declaration : int length (int m, int y); void advance (int n); void showDate () { // Return this date's ISO string representation. // return (day + "-" + month + "-" + year); cout << day << "-" << month << "-" << year << endl << endl; } }; // End of class Date Continue …

Prof A Alkhorabi Example: Date data type Class declaration (continued): int Date::length (int m, int y) { // Return the number of days in month m in year y. switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return (isLeap(y) ? 29 : 28); default: return -1; } } Continue …

Prof A Alkhorabi Example: Date data type Class declaration : void Date::advance (int n) { // Advance this date by n days (where n >= 0). int d = day + n, m = month, y = year; int last = length(m, y); while (d > (last)) { d -= last; if (m < 12) m++; else { m = 1; y++; } // if the same year advance month // otherwise, advance year and let m=1 } day = d; month = m; year = y; }

Prof A Alkhorabi Example: Date data type Continue … Possible application code: int main () { // Given a date and number of days advanced, shows new advanced date. Date date1(17, 1, 1960); date1.showDate(); date1.advance (2000); date1.showDate(); return 0; } The output Date1.cpp :Date1.cpp Example: Date1.cppDate1.cpp Date advanced by 2000 days

Prof A Alkhorabi A Person class that may represent any person can be declared as follows: Each object of the class: –a set of values: values of string types to represent the Person surname, forename, and a value for integer to represent the year of birth –a set of operations: class constructors, and methods to manipulate and inspect and/or update the set of values, changeName(), getPerson(), showPerson(). Example: Person data type

Prof A Alkhorabi Example: Person data type Class declaration: class Person { char *surname, *forename; int yearOfBirth; public : Person(int surname_sz, int forename_sz) { // Constructor surname = new char[surname_sz]; forename = new char[forename_sz]; }; Person (char *sname, char *fname, int year) { surname = new char[20]; forename = new char[20]; surname = sname; forename = fname; yearOfBirth = year; };

Prof A Alkhorabi void changeName (char *Newsname, char *Newfname) { surname = Newsname; forename = Newfname; }; void getPerson(void) { strcpy(surname, "Hussien"); strcpy(forename, "Ahmed"); yearOfBirth = 1988; }; void showPerson(void) { cout << "Person Name : " << forename << " " << surname << endl << "\nYear of Birth : " << yearOfBirth << endl << endl << endl; }; ~Person() { // Destructor delete forename; delete surname; } }; Example: Person.cppPerson.cpp Example: Person data type

Prof A Alkhorabi Example: Person data type Possible application code: Person p1 = new Person(“Hussien", “Ahmed", 1980); Person p2 = new Person(“Abdullah", “Ali", 1867); However a complete main() function that make use of the class Person follow.

Prof A Alkhorabi Example: Person data type int main(void) { Person person(15, 15);// Declare a Person object - Constuctor // & allocate memory to it. person.getPerson(); person.showPerson(); person.changeName("Ali", "Mohammed"); person.showPerson(); Person person2("Jamil", "Abdullah", 1977);// Declare a Person // object - 2nd Constuctor person2.showPerson(); return 0; } Example: Person.cppPerson.cpp

Prof A Alkhorabi The output of Person.cppPerson.cpp Person Name : Ahmed Hussien Year of Birth : 1988 Person Name : Mohammed Ali Year of Birth : 1988 Person Name : Abdullah Jamil Year of Birth : 1977 Example: Person data type

Prof A Alkhorabi Strings A string is a sequence of characters. The characters have consecutive indices. A substring of a string is a subsequence of its characters. The length of a (sub)string is its number of characters. The empty string has length zero.

Prof A Alkhorabi String ADTs Assumed requirements: 1)The data values are to be strings of any length. 2)It must be possible to determine the length of a string. 3)It must be possible to obtain the character at a given index. 4)It must be possible to obtain the substring at a given range of indices. 5)It must be possible to compare strings. 6)It must be possible to concatenate strings.

Prof A Alkhorabi Strings: specification Possible specification expressed as an outline class declaration: class String { // Each String value is an string of characters, // of any length, with indices starting at 0. private: … ; /////////////// Constructor //////////// public: String (char[] cs); // Construct a string consisting of all the chars in cs.

Prof A Alkhorabi Strings: specification Possible specification(continued): int length (); // Return the length of this string. char charAt (int i); // Return the character at index i in this string. bool equals (String that); // Return true if and only if this string is equal to that. int compareTo (String that); // Return –1 if this string is lexicographically less than that, // or 0 if this string is equal to that, // or +1 if this string is lexicographically greater than that.

Prof A Alkhorabi Strings: specification Possible specification (continued): String substring (int i, int j); // Return the substring of this string consisting of the characters // whose indices are between i, …, j –1. String concat (String that); // Return the string obtained by concatenating this string to that. }

Prof A Alkhorabi Strings: implementations One way to represent a string is by include its length n together with an array of exactly n characters, e.g.: Or represent a string by its length n together with a linked list of characters, e.g.: The array representation is much easier, but we cannot insert or delete characters. 4‘H’‘a’‘n’‘i’ 3210length ‘H’‘a’‘n’‘i’4 length first

Prof A Alkhorabi Strings: implementations Write a C++ program that implements the string ADT. The main() function should declare a number of objects of the String class, and use most of the class operations.