Outline. iterator review iterator implementation the Java foreach statement testing

Similar documents
Linked List Nodes (reminder)

Java Review: Objects

ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Class 26: Linked Lists

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Java Collection Framework

CSE 143. Computer Programming II

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Assertions, pre/postconditions

[TAP:PMUHE] Stack vs Queue

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

JAVA COLLECTION FRAMEWORK & SETS

Programming II (CS300)

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Assertions. Assertions - Example

Lists. The List ADT. Reading: Textbook Sections

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Programming II (CS300)

Iterators and Sequences

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

The Java Collections Framework and Lists in Java Parts 1 & 2

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Chapter 4 Defining Classes I

EECS 2011 M: Fundamentals of Data Structures

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Data Structures and Algorithms

Lecture 8: Iterators and More Mutation

CS S-05 Abstract Data Types and Lists 1

CS 3 Introduction to Software Engineering. 5: Iterators

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

Design Patterns. Command. Oliver Haase

The Java Collections Framework. Chapters 7.5

Lecture 4. The Java Collections Framework

6.005 Elements of Software Construction Fall 2008

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

List ADT. Announcements. The List interface. Implementing the List ADT

Programming Languages and Techniques (CIS120)

ITI Introduction to Computing II

Basic Data Structures 1 / 24

Linked Lists. Chapter 12.3 in Savitch

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Program Correctness and Efficiency. Chapter 2

Collections, Maps and Generics

Classes, interfaces, & documentation. Review of basic building blocks

JVA-103. Java Programming

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Class 32: The Java Collections Framework

Examination Questions Midterm 1

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Basic Data Structures

Data Structures Brett Bernstein

Fun facts about recursion

CSC 321: Data Structures. Fall 2017

Building Java Programs

JAVA V Source files Java, winter semester

CS 314 Exam 2 Spring

Higher-Order Sequential Operations

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Introduction to Linked Lists

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Introduction to Object-Oriented Programming

List ADT. B/W Confirming Pages

Building Java Programs

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Linked List Implementation of Queues

Active Learning: Streams

+ Abstract Data Types

Lecture 7: Lists, Version 2 (with Mutation)

CS 314 Final Fall 2011

Advanced Java Concepts Unit 2: Linked Lists.

EXAMINATIONS 2016 TRIMESTER 2

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java 1996 AP Computer Science Question 3

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Lecture 3: Maps and Iterators

CSE 331 Winter 2016 Midterm Solution

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Design for reuse. Design patterns for reuse

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Outline. From last Time. Announcements. CS1007: Object Oriented Design and Programming in Java. Design considerations Testing Putting all together

Introduction to Programming System Design CSCI 455x (4 Units)

Java Collections Framework. 24 April 2013 OSU CSE 1

Java Language Features

Building Java Programs

Transcription:

Outline iterator review iterator implementation the Java foreach statement testing

review: Iterator methods a Java iterator only provides two or three operations: E next(), which returns the next element, and also advances the references boolean hasnext(), which returns whether there is at least one more element void remove(), which removes the last element returned by next() (this method is optional) using remove may invalidate any other existing (concurrent) iterators

review: ListIterator the Java Iterator interface is very general and reasonably powerful sometimes it is useful to be able to move backwards and forwards, and add or replace as well as remove elements the ListIterator interface adds these operations to the basic Iterator interface it also keeps track of the position and can return the index of the next or previous item

review: Automatic use of iterators instead of having to use the while loop to use an iterator, the for loop has been specialized to call the iterator LinkedList<Integer> values =... int sum = 0; for (Integer value: values) { } sum = sum + value; Java creates and calls the iterator, but the iterator itself is not visible in the code the same code can loop over arrays

review: Explicit use of iterators this is the equivalent code showing the iterator: LinkedList<Integer> values =... int sum = 0; Iterator<Integer> i = values.iterator(); while(i.hasnext()) { } Integer value = i.next(); sum = sum + value;

Java for and foreach this automated (and invisible) use of iterators with for loops is called the Java enhanced for statement or for each statement the foreach statement works on any expression that has a value that satisfies the Iterable interface The Iterable interface simply requires an iterator method: class MyCollection<E> implements Iterable<E> { // return a new iterator Iterator<E> iterator();

Iterator Implementation a Java iterator may or may not be internal to the collection class every Java iterator must have sequential access to the elements of the collection every Java iterator must have at least one variable to keep track of where it is in the traversal, that is, which elements have not yet been returned See LinkedListIterator.java for a very simple iterator on linked lists. in-class exercise (everyone together): design the code for the iterator() method of the LinkedList class

Iterator in-class exercise work with your friends add an iterator method to this ArrayList class: public class ArrayList<E> { protected E [] data; protected int size; /* implementation of the ArrayList methods */ you do this by first defining an ArrayListIterator class public class ArrayListIterator<E> implements Iterator<E> { /* you should declare some class variables */ /* you should declare some constructor(s) */ public boolean has_next () { /* your code goes here */ } public E next () { /* your code goes here */ } public void remove () { throw new UnsupportedOperationException ("not supported"); } }

Iterator in-class exercise, part II now implement the iterator method: // create and return an interator Iterator<E> iterator() { // your code goes here The iterator method is implemented inside the ArrayList class, and returns an object of type ArrayListIterator public class ArrayList<E> { protected E [] data; protected int size; public class ArrayListIterator<E> implements Iterator<E> {

Testing unless the code is written correctly from the start, the errors must be found by testing the code writing correct code is a lot of work, so most programmers use testing to make their code as close to correct as possible test routines can (and probably should) be included in the main method of any class that doesn't already have one. This is a unit test. This main method is a driver program. The driver program can also be defined separately. the unit test should call all the methods of the class, with as many combinations of parameters as possible if the writer of the test code doesn't study the code under test, this is black box testing at the very least, the goal of testing is full coverage: making sure that every path through the code has been used at least once, and has produced an acceptable result to produce full coverage, the programmer of the test program must study the code being tested: this is white box testing

A few common types of errors off-by-one (fencepost): how many fence posts are needed for a fence that is 20 feet long and has a post every 2 feet? not initializing data correctly. Sometimes this causes null pointer access using different variables as if they were one, or using one variable as if it were two variables assumptions that don't turn out to be true (misconceptions), not establishing and maintaining invariants not checking things that should be checked, e.g. if (x == null)

strategies for testing print/show all method invocations and their parameters and return values write code to check that the invariants are established and maintained write test cases to not only provide full coverage, but also check all boundary conditions, where the result should change (make sure it changes where it should) some common special cases: < 0, 0, 1, > 1 first and last elements of an array, collection, linked list elements and values that are null desired element is not in the collection, or is in the collection more than once collection has size 0, 1, or larger for example, when testing inserting on a linked list, can test inserting at the beginning of a linked list, at the end, in the middle, and inserting into an empty linked list. Also, inserting an element that has the value null (is the behavior of your program defined in that case? Should it be?) if code to be tested needs to call a method that is not yet implemented, a stub of that method can do only what is needed for the test

reasoning about programs a precondition must be true before a method is called the code in the method may (and usually will) assume that the precondition is true the caller of a method must guarantee (be sure) that the precondition holds a postcondition will be true after a method is called the code in the method must guarantee that the postcondition is true preconditions and postconditions are a little bit like a contract or any other agreement: if the caller provides the preconditions, the method will provide the postconditions preconditions and postconditions are documented in Javadoc invariants are postconditions of every method, including the constructors invariants are preconditions of every method except the constructors invariants are usually documented for the entire class rather than for each method