Testing change to a linked list

Slides:



Advertisements
Similar presentations
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
CS 1110 Prelim II: Review Session 1. Exam Info Prelim II: 7:30–9:00PM, Tuesday, 8 November, Baker Lab 200, 219, 119 Look at the previous Prelims Arrive.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
CS100A, Fall Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
A: A: double “4” A: “34” 4.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Implementing an ADT Example: Sum-Constraint ADT Part II Ohad Barzilay May 2004.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Introduction to Arrays
Data Structures with Java © Rick Mercer
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
5. Function (2) and Exercises
Mid Term Review Advanced Programming Ananda Gunawardena
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
CS October 2008 The while loop and assertions
"First things first, but not necessarily in that order " -Dr. Who
AP Java Warm-up Boolean Array.
CS November 2010 Ragged arrays, Applets
Unit 3 - The while Loop - Extending the Vic class - Examples
Building Java Programs
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
JSON Data Demo.
Asymptotic complexity Searching/sorting
Recursion Problems.
Binary Search and Loop invariants
Asymptotic complexity
Introduction to Arrays
CS November 2010 Developing array algorithms. Reading:
Recursion Method calling itself (circular definition)
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
Linear and Binary Search
Dasar-Dasar Pemrograman 2: Recursion
CS100A Sections Dec Loop Invariant Review C Review and Example
More on iterations using
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Michele Weigle - COMP 14 - Spr 04
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

Testing change to a linked list d.prepend(5); assertEquals(“[…]”, d.toString()); // uses first, all next fields, val fields assertEquals(“[…]”, d.gnirtSot()); // uses last, all prev fields, val fields assertEquals(4, d.size()); // uses field size Thus, those three assertEquals calls test all fields. You don’t have to worry about how to test, TEST ALL FIELDS! You must test all methods that change the list in this fashion. Otherwise, points deducted DLL@7 N@1 first 3 size N@2 last d N@8 next 6 val null prev 7

Iterative palindrome testing /** = " s is a palindrome" */ public static boolean isPal(String s) { if (s.length() <= 1) return true; // { s has at least 2 chars } int n= s.length()-1; return s.charAt(0) == s.charAt(n) && isPal(s.substring(1,n)); } /** = " b is a palindrome" */ public static boolean isPal(int[] b) We demo the use of loop invariants to develop an iterative version of palindrome testing. We use an array instead of a String because the notation is easier in Java. That’s the only reason.

Iterative palindrome testing /** = " s is a palindrome" */ public static boolean isPal(String s) { if (s.length() <= 1) return true; // { s has at least 2 chars } int n= s.length()-1; return s.charAt(0) == s.charAt(n) && isPal(s.substring(1,n)); } ? one is the reverse of the other Recursive isPal compares successive outer pairs.

Iterative palindrome testing /** = " b is a palindrome" */ public static boolean isPal(char[] b) { } ? 0 b.length Pre: b ? 0 h k b.length Post: b b[k..] rev b[0..h] rev k-(h+1) <= 1 ? 0 h k b.length Inv: b b[k..] rev b[0..h] rev

Iterative palindrome testing /** = " b is a palindrome" */ public static boolean isPal(char[] b) { } int h= -1; int k= b.length; Start? Make invariant true? ? 0 b.length Pre: b ? 0 h k b.length Inv: b b[k..] rev b[0..h] rev

Iterative palindrome testing /** = " b is a palindrome" */ public static boolean isPal(char[] b) { int h= -1; int k= b.length; while ( ) { } When k-(h+1) <= 1 Continue when k-(h+1) > 1 Continue when k-h-1 > 1 h < k-2 Continue when k-2 > h Continue when h < k-2 Stop? When is postcondition true? ? 0 h k b.length Post: b b[k..] rev b[0..h] rev k-(h+1) <= 1 ? 0 h k b.length Inv: b b[k..] rev b[0..h] rev

Iterative palindrome testing /** = " b is a palindrome" */ public static boolean isPal(char[] b) { int h= -1; int k= b.length; while ( h < k-2 ) { } h= h+1; k= k-1; How to make progress toward termination? ? 0 h k b.length Inv: b b[k..] rev b[0..h] rev

Iterative palindrome testing /** = " b is a palindrome" */ public static boolean isPal(char[] b) { int h= -1; int k= b.length; while ( h < k-2 ) { h= h+1; k= k-1; } if (b[h] != b[k]) return false; return true; How to make to make invariant true again? h k ? 0 h k b.length Inv: b b[k..] rev b[0..h] rev

Iterative palindrome testing // Store “array b is a palindrome” in variable isPal // Store “array b is a palindrome” in variable isPal int h= -1; int k= b.length; isPal= true; while ( isPal && h < k-2 ) { h= h+1; k= k-1; if (b[h] != b[k]) isPal= false; } ? 0 h k b.length Post: b b[k..] rev b[0..h] rev k-(h+1) <= 1 && isPal = (b[0..h] is reverse of b[k..]) ? 0 h k b.length Inv: b b[k..] rev b[0..h] rev isPal = (b[0..h] is reverse of b[k..])