Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.

Slides:



Advertisements
Similar presentations
List Implementations That Use Arrays
Advertisements

Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Object Oriented Programming & Software Engineering.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Information Hiding and Encapsulation
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Data Abstraction: The Walls
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Chapter 3 Introduction to Collections – Stacks Modified
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures and Abstractions with Java, 4e Frank Carrano
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Grouping objects Introduction to collections 5.0.
1 CSC 221: Computer Programming I Fall 2004 Lists, data access, and searching  ArrayList class  ArrayList methods: add, get, size, remove  example:
 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.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Puzzle 2 1  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Lists Chapter 4.
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,
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.
TCSS 143, Autumn 2004 Lecture Notes
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Slides by Steve Armstrong LeTourneau University Longview, TX
Adapted from Pearson Education, Inc.
EE 422C Sets.
ArrayLists 22-Feb-19.
Stacks Chapter 5.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recitation 2 CS0445 Data Structures
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A type is a collection of values
© 2016 Pearson Education, Ltd. All rights reserved.
CHAPTER 3: Collections—Stacks
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Lists Chapter 4

2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending Machine

3 Specifications for the ADT List A list provides a way to organize data A to-do list, address list, grocery list etc… ADT list must be considered in general, not necessarily a list of strings. It can contain any type of objects.

4 Specifications for the ADT List Operations on lists Add new entry – at end, or anywhere Remove an item Remove all items Replace an entry Look at any entry Look for an entry of a specific value Count how many entries Check if list is empty, full Display all the entries

5 Specifications for the ADT List To specify an ADT list Describe its data ( A collection of objects in a specific order and having the same data type, the number of objects in the collection.) Specify the operations ADT does not indicate how to store the data or how to implement the operations. In contrast, a data structure is an implementation of an ADT within a programming language.

6 Example The effect of ADT list operations on an initially empty list. Convenient way of identify a particular entry is by the entry’s position within the list. Position-oriented ADT

7 Potential Problem Operations add, remove, replace, getEntry work OK when valid position given remove, replace and getEntry not meaningful on empty lists A list could become full, what happens to add ? What else operations may cause problems?

8 Possible Solutions Assume the invalid situations will not occur. State a precondition, up to the client to check Ignore the invalid situations Nothing will be done. Client is left with no knowledge. Make reasonable assumptions, guess at the client’s intention. act in predictable way E.g. If a client wants to remove the 6 th entry for a list with only three entries. Remove the third entry. Return a signal or Boolean value indicating success or failure of the operation E.g. return null if getEntry method gives an nonexistent position Throw an exception Simply report a problem without deciding what to do. The exception allow each client to do what is needed in its own particular situation. The method invocation in the client must appear within a try block. The documentation for ADT should describe these possible solutions.

9 Redefining Specifications A first draft of an ADT specifications may ignore potential problems Simplifies the first draft Concentrate on details after major portions of specifications written Makes the specifications complete After writing specifications, Write Java interface for its operations Java interface can be used for class to implement the ADT

10 Generic Types Within an Interface and Class public interface Pairable { public void setPairs(S firstItem, S secondItem) } A class that implements this interface could begin with the statement public class OrderedPair implements Pairable A class to represent pair of objects of the same type. Since each pair can be of any class type, we used a generic type in the definition of the class. The method setPair has parameters of a generic type. Imagine an interface Pairable that declares this method.

11 ListInterface /** An interface for the ADT list. * Entries in the list have positions that begin with 1.*/ public interface ListInterface { /** Task: Adds a new entry at a specified position within the list. The list’s size is * increased by 1. newPosition an integer that specifies the desired position of the new entry newEntry the object to be added as a new entry. true if the addition is successful, or false if either the list is full, newPosition getLength() +1 */ public boolean add( int newPosition, T newEntry); How to write the comments for method remove? public T remove( int givenPosition); }

12 Using the ADT List A list of numbers that identify runners in the order in which they finish a race

13 Using the ADT List AList implements interface ListInterface. public class ListClient { public static void main(String[] args) { testList(); } // end main public static void testList() { ListInterface runnerList = new AList (); // has only methods // in ListInterface runnerList.add("16"); // winner runnerList.add(" 4"); // second place runnerList.add("33"); // third place runnerList.add("27"); // fourth place runnerList.display(); } // end testList } // end ListClient The data type of runnerList is ListInterface. This declaration obliges runnerList to call only methods in the interface. If the data type was Alist, what methods are available to runnerList ?

Question? 14 1.What changes to testList are necessary to represent the runner’s numbers as Integer objects instead of strings? 2. Write Java statements that exchange the third and seventh entries in a list of 10 string objects by only using remove and add methods. We name the list to be Mylist.

15 Java Class Library: The Interface List The standard package java.util contains a list interface – called List Methods provided public boolean add(Object newEntry) public void add(int index, Object newEntry) public Object remove(int index) public void clear() public Object set(int index, Object anEntry) // like replace public Object get(int index) // like getEntry public boolean contains(Object anEntry) public int size() // like getLength public boolean isEmpty()

16 A List Interface is Like a Vending Machine A vending machine.

17 A List Interface is Like a Vending Machine Observations about vending machines Can perform only tasks shown on interface Must understand the tasks Cannot see inside the machine Can use the machine even though don’t know what happens inside If inside of machine replaced with new improved version Interface remains unchanged Customer uses machine in same way as before

18 A List Interface is Like a Vending Machine Observations about clients and List ADT Client can perform only operations from the ADT List Client must adhere to specifications Client cannot access data without an ADT operation. Encapsulation hides the data representation within the ADT Client can use the list – even though unable to access entries directly. Don’t need to know how the data is stored. If implementation is changed, client still uses list in same way as before

19 Exercise Suppose that you have a list that is created by the following statement: ListInterface quizScores = new AList (); Imagine that someone has added to this list the quiz scores received by a student throughout a course. The professor would like to know the average of these quiz scores, ignoring the lowest score. a. Write Java statements at the client level that will find and remove the lowest score in the list. b. Write Java statements at the client level that will compute the average of the scores remaining in the list.