Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

COMP171 Fall 2005 Lists.
DATA STRUCTURES USING C++ Chapter 5
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Introduction - The Need for Data Structures Data structures organize data –This gives more efficient programs. More powerful computers encourage more complex.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Data Structures Using Java1 Chapter 4 Linked Lists.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Data Structure & Algorithms
Mutability SWE 332 Fall 2015 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 4 Linked Structures.
EECE 310: Software Engineering
CS2006- Data Structures I Chapter 5 Linked Lists I.
Object Oriented Programming using Java - Class Instance Variables
Top Ten Words that Almost Rhyme with “Peas”
Object Based Programming
Chapter 13 Collections.
Linked List (Part I) Data structure.
Data Abstraction David Evans cs205: engineering software
Dynamic Data Structures and Generics
Lecture 4: Data Abstraction CS201j: Engineering Software
CSE 143 Lecture 5 References and Linked Nodes
Dynamic Data Structures and Generics
Introduction to Data Structure
Recursive Objects Singly Linked Lists.
Presentation transcript:

Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul

Overview Data abstraction Specification/Design of Abstract Data Types (ADTs) Implementation of ADTs

The Problem Programs are complex. –Windows XP: ~45 million lines of code –Mathematica: over 1.5 million Abstraction helps – Many-to-one – “forget the details” –Must separate “what” from “how”

Information Hiding Modularity - Procedural abstraction –By specification Locality Modifiability –By parameterization Data Abstraction –What you can do with the data is separated from how it is represented

Software development cycle Specifications – What do you want to do? Design – How will you do what you want? Implement – Code it. Test – Check if it works. Maintain – School projects don’t usually make it this far. Bugs are cheaper earlier in the cycle!

Database Implementation Database on library web-server stores information on users: userID, name, , etc. You are responsible for implementing the interface between the web-server and database –What happens when we ask for the address for a specific user?

Client asks for address What is address of nate? Client Server Database

Client/Server/Database Interaction I need Nate’s . The interaction between the server and database is your part. Database Server Client

Client/Server/Database Interaction Client Server Database

Client/Server/Database Interaction Client Server Database

Example: Database System Need a new data type Abstract Data Types (ADTs) –Help separate what from how –Client will use the specifications for interaction with data –Client of the web database should not know the “guts” of the implementation

Data abstraction in Java An ADT is defined by a class –The ADT in the web/database application will be a User –A private instance variable hides the class internals –public String get (); What is private in the implementation? OVERVIEW, EFFECTS, MODIFIES –A class does not provide data abstraction by itself

Accessibility Class User { // OVERVIEW: // mutable object // where the User // is a library // member. public String ; … } String nate = myUser. ; send (nate ); /* The client’s code can only see what is made public in the User class. The user’s data is public in the User class. This is BAD. */ /* Client code using a User object, myUser */

Program Maintenance Suppose storage space is at a premium –Everyone in the database is so we can drop the virginia.edu –What kind of problems will occur with the code just seen?

Program Maintenance Suppose storage space is at a premium –Everyone in the database is so we can drop the virginia.edu –What kind of problems could occur had the client code been able to access the address directly? String nate = myUser. ; send (nate ); was public in User class. ***ERROR!!!***

Accessibility (fixed) Class User { // OVERVIEW: A // mutable object where // User is a library // member. private String ; … public String get () { // EFFECTS: returns user’s // primary return ; } String nate = myUser.get (); send (nate ); /* This code properly uses data abstraction when returning the full address. */ // Client code using a User object, myUser

Accessibility (fixed) Class User { // OVERVIEW: A // mutable object where // User is a library // member. private String ; … public String get () { // EFFECTS: returns user’s // primary return ”; } String nate = myUser.get (); send (nate ); /* The database dropped and only one line of code needed changing. */ // Client code using a User object, myUser

Advantages/Disadvantages of Data Abstraction? - More code to write and maintain initially -Overhead of calling a method -Greater initial time investment + Client doesn’t need to know about representation + Maintenance is easier. + Increases locality and modifiability

Specifying ADTs

Bad Users at the Library The library now wants to crack down on bad Users with overdue books, so the code will need to work with a group of Users. What should be used to represent the group? What data structures do we know about? How should we integrate this code with what we have? What operations should be supported? –deleteUser(String userID); –isInGroup(String userID);

Library keeping track of “bad” people You need to write some code that will manipulate a group of Users that are on the “bad” list. Implementation at right uses an array Class GroupUsers { // OVERVIEW: // Operations provided // to manage a mutable group // of users private User [] latePeople; … public void toString() { // OVERVIEW: Print user // names to standard output … }

Array implementation initialization for GroupUsers Class GroupUsers { // OVERVIEW: Unbounded, mutable // group of Users private User [] latePeople; … public void GroupUsers(String [ ] userIDs) { // OVERVIEW: Initialize group // from userIDs latePeople = new User[userIDs.length + 10]; for(int i = 0; i < userIDs.length; i++) { latePeople[i] = new User(userIDs[i]); }

ADT design Mutable/Immutable ADTs –Mutable – object’s fields or values change –Immutable – object’s fields permanently set at creation –Is this being modified? Tradeoffs Immutability simpler and safer Immutability is slower (creation/deletion of objects)

Classification of ADT operations Creator (constructor) –GroupUsers(String userIDs[ ]) Producer –addUser(String userID) Mutator –setUser (String ) Observer –isMember (String userID)

Implementing ADTs

A bad implementation Most common characteristics –Modifying implementation forces other code to be changed (violdates modifiability) –Must understand more code than necessary to reason about code (violates locality) –Maintenance is difficult

A good implementation User class needed a way to store state of a user, so operations will build around the stored state. Methods should be (procedure abstraction): –Easily coded as possible –Efficient –Exhibit locality –Should enable better testing, maintenance

Changing the group implementation The “guts” of the implementation is subject to change. What happens on the GroupUser’s deleteUser(String userID)?

deleteUser(String userID) The array must shift down an average of n/2 items when deleting an element X

Linked Lists A new data structure User 1User 2User 3 Each User has its own representation, but we store the collection in a list. In the following implementation, each user object is contained in a Node object. Head X

class Node { // OVERVIEW: // Mutable nodes that is used for a linked list // of users private User theUser; private Node next; … } List-node implementation User 1 next points to the next “bad” user User 2 … latePeople

class GroupUsers { // OVERVIEW: // Mutable, unbounded group of users private Node latePeople; /* head of list */ private int numUsers; … } /* Nodes are users with an additional member field called next. The Node class was added, so the User class would not need modification. */ List implementation

Adding a user into GroupUsers /* in GroupUsers.java */ public void addUser(User newUser) { // MODIFIES: this // EFFECTS: this_pre = this_pre U { (Node)newUser } latePeople.add(new Node(newUser)); numUsers++; }

Adding a node into a group of nodes (Node.java) public void add (Node n) { // MODIFIES: this // EFFECTS: n is inserted just after this in the list // first user in list? if (this.next == null) { this.next = n; } else { n.next = this.next; this.next = n; }

deleteUser(String userID) cont. User 1User 2User 3 Head X User 1User 3 Head X X

deleteUser(String userID) Node.java public void delete (String userID) { // MODIFIES: this // EFFECTS: this_pre = this_pre – node // where node.userID = userID Node currNode; Node prevNode; if(this.next == null) return; prevNode = this; currNode = this.next; // continued on next slide

deleteUser(String userID) cont. while(currNode.next != null) { if(userID.equals(currNode.getUserID())) { prevNode.next = currNode.next; break; } currNode = currNode.next; prevNode = prevNode.next; } // user at end of list? if (currNode.next == null && userID.equals(currNode.getUserID())) { prevNode.next = null; }

Linked List vs. Array Array is better for: –Accessing a randomly desired element Linked list is better at: –Inserting –Deleting –Dynamic resizing Users of your implementation may need to use a list or an array for efficiency, so you need an implementation that can be changed easily.

Questions?