Section 2.3 Array-Based StringLog ADT Implementation.

Slides:



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

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Stacks, Queues, and Linked Lists
Linear Lists – Array Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
CHAPTER 7 Queues.
Queue Overview Queue ADT Basic operations of queue
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chapter 4 ADT Sorted List.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Data Structures Using Java1 Chapter 8 Search Algorithms.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 8 ARRAYS Continued
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
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 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Click to edit Master text styles Stacks Data Structure.
Chapter 2 Abstract Data Types. Chapter 2: Abstract Data Types 2.1 – Abstraction 2.2 – The StringLog ADT Specification 2.3 – Array-Based StringLog ADT.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
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.
Sections 3.4 Formal Specification
The need for Programming Languages
Section 2.6 Linked List StringLog ADT Implementation
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ADT list.
Logical Operations In Matlab.
CIS 199 Final Review.
Chapter 9 The Priority Queue ADT
Presentation transcript:

Section 2.3 Array-Based StringLog ADT Implementation

2.3 Array-Based StringLog ADT Implementation Class name: ArrayStringLog Distinguishing feature: strings are stored sequentially, in adjacent slots in an array Package: ch02.stringLogs (same as StringLogInterface)

Instance Variables String[] log; –The elements of a StringLog are stored in an array of String objects named log. int lastIndex = -1; –Originally the array is empty. Each time the insert command is invoked another string is added to the array. We use this variable to track the index of the “last” string inserted into the array. String name; –Recall that every StringLog must have a name. We call the needed variable name.

Instance variables and constructors package ch02.stringLogs; public class ArrayStringLog implements StringLogInterface { protected String name; // name of this log protected String[] log; // array that holds log strings protected int lastIndex = -1; // index of last string in array public ArrayStringLog(String name, int maxSize) // Precondition: maxSize > 0 // // Instantiates and returns a reference to an empty StringLog object // with name "name" and room for maxSize strings. { log = new String[maxSize]; this.name = name; } public ArrayStringLog(String name) // Instantiates and returns a reference to an empty StringLog object // with name "name" and room for 100 strings. { log = new String[100]; this.name = name; }

The insert operation public void insert(String element) // Precondition: This StringLog is not full. // // Places element into this StringLog. { lastIndex++; log[lastIndex] = element; } An example use: ArrayStringLog strLog; strLog = new ArrayStringLog("aliases", 4); strLog.insert("Babyface"); String s1 = new String("Slim"); strLog.insert(s1);

Example use of insert

Example use of insert continued

The clear operation The “lazy” approach: public void clear() // Makes this StringLog empty. { lastIndex = -1; }

The clear operation The “thorough” approach: public void clear() // Makes this StringLog empty. { for (int i = 0; i <= lastIndex; i++) log[i] = null; lastIndex = -1; }

Three Observers public boolean isFull() // Returns true if this StringLog is full, otherwise returns false. { if (lastIndex == (log.length - 1)) return true; else return false; } public int size() // Returns the number of Strings in this StringLog. { return (lastIndex + 1); } public String getName() // Returns the name of this StringLog. { return name; }

The toString Observer public String toString() // Returns a nicely formatted string representing this StringLog. { String logString = "Log: " + name + "\n\n"; for (int i = 0; i <= lastIndex; i++) logString = logString + (i+1) + ". " + log[i] + "\n"; return logString; } For example, if the StringLog is named “Three Stooges” and contains the strings “Larry”, “Moe”, and “Curly Joe”, then the result of displaying the string returned by toString would be Log: Three Stooges 1. Larry 2. Moe 3. Curly Joe

Stepwise Refinement Approach a problem in stages. Similar steps are followed during each stage, with the only difference being the level of detail involved. The completion of each stage brings us closer to solving our problem. There are two standard variations of stepwise refinement: –Top-down: The problem is broken into several large parts. Each part is in turn divided into sections, then the sections are subdivided, and so on. Details are deferred as long as possible. The top-down approach is often used for the design of non-trivial methods. –Bottom-up: Details come first. They are brought together into increasingly higher-level components. A useful approach if you can identify previously created program components to reuse in creating your system.

contains method: top-down stepwise refinement phase 1 public boolean contains(String element) { Set variables while (we still need to search) { Check the next value } return (whether or not we found the element) } A combination of a programming language with a natural language, such as we use here, is called pseudocode and is a convenient means for expressing algorithms.

contains method: top-down stepwise refinement phase 2 public boolean contains(String element) { Set variables; while (we still need to search) { if (the next value equals element) return true; } return false; }

contains method: top-down stepwise refinement phase 3 public boolean contains(String element) { int location = 0; while (we still need search) { if (element.equalsIgnoreCase(log[location])) return true; else location++; } return false; }

contains method: top-down stepwise refinement phase 4 public boolean contains(String element) // Returns true if element is in this StringLog // otherwise returns false. // Ignores case differences when doing string comparison. { int location = 0; while (location <= lastIndex) { if (element.equalsIgnoreCase(log[location])) // if they match return true; else location++; } return false; }