Ch. 4 Data Abstraction Modular programming Procedural abstraction

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Two 2x2 (block) matrices can be multiplied C==AB
Abstract Data Types. Typical operations on data  Add data to a data collection  Remove data from a data collection  Ask questions about the data in.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Information and Computer Sciences University of Hawaii, Manoa
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
Chapter 5 Linked Lists II
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 9: Implementing ADTs Lecturer: Santokh Singh Please try to Understand All concepts involved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
CompSci 230 S Programming Techniques
The List ADT.
Linked Lists Chapter 5 (continued)
Abstract Data Types (ADT)
Data Abstraction: The Walls
Stacks.
Implementing ArrayList Part 1
List Representation - Array
Advanced C++ Topics Chapter 8.
Data Abstraction A technique for increasing the modularity of a program The need to support several operations on the data arise need to define abstract.
Lecture 2: Implementing ArrayIntList reading:
null, true, and false are also reserved.
Programming in Java Lecture 11: ArrayList
Building Java Programs
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
EE 422C Sets.
Abstract Data Types (ADT)
Lecture 2: Implementing ArrayIntList reading:
Ch. 5 Linked List When a list is implemented by an array Linked list
Array-Based Implementations
Data Abstraction: The Walls
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Ch. 12 Tables and Priority Queues
ArrayLists 22-Feb-19.
Linked Lists Chapter 5 (continued)
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
Programming II (CS300) Chapter 07: Linked Lists
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Data Abstraction: The Walls
CSE 143 Lecture 21 Advanced List Implementation
Linked Lists Chapter 5 (continued)
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

Ch. 4 Data Abstraction Modular programming Procedural abstraction Know what each method does but not how it does Data abstraction Focus what you can do to a collection of data but not how you do it ADT (Abstract data type) A collection of data together with a set of operations on the data

Examples of ADT A sorted array together w/ the binary search algorithm A set of students together w/ student-affairs operations Grocery items together w/ operations on them

ADT Operations Isolates a Data Structure from the Program

An Example Design of ADT Grocery list ADT consists of items operations that you can perform on the items

Operations on the Items Create an empty list Determine whether a list is empty Determine the # of items on a list Add an item in the list Remove an item from the list Retrieve an item from the list Get the price of an item Get the price sum of the items in the list Print the items on the list “How to implement” is another issue

A Bad Design Direct access to the data structure

Abstraction Example firstItem = aList.item[0]; vs. firstItem = aList.get(1); Get the 1st item of the list Get the item at slot 0 in the array item[ ] for list What if the array changes to another data structure later?

JAVA Classes Constructors Inheritance Class Object

constructors public class Sphere { private double theRadius; public Sphere( ) { setRadius(1.0); } // default constructor public Sphere(double initialRadius) { setRadius(initialRadius); } public void setRadius(double newRadius) { if (newRadius >= 0.0) { theRadius = newRadius; public double radius( ) { return theRadius; public double volume( ) { return (4.0 * Math.PI * Math.pow(theRadius, 3.0)) / 3.0; public void displayStatistics( ) { System.out.println(“\nRadius = ” + radius( ) + “\nDiameter = ” + diameter( ) + “\nCircumference = ” + circumference( ) + “\nArea” + area( ) + “\nVolume” + volume( )); } // end Sphere constructors

Inheritance new methods overwriting (overriding) public class Ball extends Sphere { private String theName; public Ball( ) { // at first create a sphere w/ radius 1.0 setName(“unknown”); } // default constructor public Ball(double initialRadius, String initialName) { super(initialRadius); setName(initialName); } public String name( ) { return theName; public void setName(String newName) { theName = newName; public void displayStatistics( ) { System.out.print(“\nStatistics for a ”+ name( )); super.displayStatistics( ); } // end Ball new methods overwriting (overriding)

Every class is a subclass of the class Object public class Sphere { … } public class Sphere extends Object { … } implicitly means

Object Equality Class Object provides the method equals( ) Method equals( ) basically checks to see if two references are the same Sphere sphere1 = new Sphere( ); Sphere sphere2 = sphere1; If (sphere1.equals(sphere2)) { System.out.println(“The same!”); } else { System.out.println(“Different!”); } The same!

Object Equality Sphere sphere1 = new Sphere(2.0 ); If (sphere1.equals(sphere2)) { System.out.println(“The same!”); } else { System.out.println(“Different!”); } Different!

Overriding public class Sphere { … public boolean equals(Object sp) { return ((sp instanceof Sphere) && theRadius == ((Sphere)sp).radius( )); }

Cf: Overloading public class Sphere { … public boolean equals(Sphere sp) { return (theRadius == sp.radius( )); } 헛점이 있으나 논외 Overriding: the same name, the same number & types of parameters Has only one method accessible w/ the name Overloading: the same name, different number or types of parameters Has two different methods accessible w/ the name

ADT List Implementation w/ Array 1 2 3 4 Operations createList( ) isEmpty( ) size( ) add(newPosition, newItem) remove(index) removeAll( ) get(index)

Interface Design public interface ListInterface { public boolean isEmpty( ); public int size( ); public void add(int index, Object item) throws ListIndexOutOfBoundsException, ListException; public Object get(int index) throws ListIndexOutOfBoundsException; public void remove(int index) public void removeAll( ); }

Implementation public class ListArrayBased implements ListInterface { private final int MAX_LIST = 50; private Object items[ ]; // array of list items[1…MAX_LIST] private int numItems; // # of items in the list public ListArrayBased( ) { items = new Object[MAX_LIST+1]; numItems = 0; } public boolean isEmpty( ) { return (numItems == 0); public int size( ) { return numItems;

예: Shifting Items Before Insertion at Position 3 new item shift

Continued… public void removeAll( ) { items = new Object[MAX_LIST+1]; numItems = 0; } public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (numItems > MAX_LIST) { Exception 처리; if (index >= 1 && index <= numItems+1) { // shift right for (int pos = numItems; pos >= index; pos--) { items[pos+1] = items[pos]; items[index] = item; numItems++; } else {

예: Shifting Items After Deletion at Position 4 deleted shift

public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { return items[index]; } else { // index out of range Exception handling; } public void remove(int index) if (index >= 1 && index <= numItems) { // shift left for (int pos = index+1; pos <= size( ); pos++) { items[pos-1] = items[pos]; numItems--; } // end class ListArrayBased