JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Lecture Objectives  Learn how to think recursively  Trace a recursive method  Prove a recursive method is correct  Write recursive algorithms and methods.
Data Structures and Algorithms
Factorial Recursion stack Binary Search Towers of Hanoi
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 5.
Computer Science II Recursion Professor: Evan Korth New York University.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Recursion. Binary search example postponed to end of lecture.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 7.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion Chapter 5.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
RECURSION Chapter 5. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Recursion. Midterm Exam Wednesday,November 4, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices Simple problem solving.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Recursion.
Recursion Topic 5.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 15 Recursion.
Java Software Structures: John Lewis & Joseph Chase
Recursion Chapter 5.
7.2 Recursive Definitions of Mathematical Formulas
7.4 Problem Solving with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursive Thinking.
7.2 Recursive Definitions of Mathematical Formulas
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

JUnit test and Project 3 simulation

Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices Simple problem solving questions Writing code 2

3 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT tutorial by Hong Qing Yu (

4 The Testing Problems programmers Should write few Do Why? I am so busy It is difficult

5 The Testing Problems Programmers need such kind of tool: “Writing a few lines of code, then a test that should run, or even better, to write a test that won't run, then write the code that will make it run.” JUnit is that kind of tool!

6 JUnit The testing problems The framework of JUnit A case study

7 The Framework of JUnit

8 JUnit The testing problems The framework of JUnit A case study

9 A Case Study Lab3Queue: enQueue method deQueue method

Include junit library in eclipse 10

11 How to Write A TestCase using Junit (available in Eclipse 3.1 or later) Step 1:Create a JUNIT test case (File -> New -> Junit Test Case

Create a test case 12

Create a test case import junit.framework.*; public class Lab3QueueTest { public void setUp() throws Exception { } public void tearDown() throws Exception { } 13

Create a test case import junit.framework.*; public class Lab3QueueTest extends TestCase { Lab3Queue testQueue; int queueSize; public void setUp() throws Exception { testQueue = new Lab3Queue(); queueSize = testQueue.getSize(); } public void tearDown() throws Exception { } 14

Create a test case For each method that you are going to test: Write a corresponding test method named: test in the test case 15

Create a test case public void testenQueue() { int newItem = 1; queueSize = testQueue.getSize(); testQueue.enQueue(newItem); Assert.assertEquals(queueSize+1, testQueue.getSize()); int actualItem = ((Integer) testQueue.getLastNode()).intValue(); Assert.assertEquals(newItem, actualItem); } 16

17 Assert assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) assertEquals(message, expected, actual, delta) assertFalse(condition) assertFalse(message, condition) Assert(Not)Null(object) Assert(Not)Null(message, object) Assert(Not)Same(expected, actual) Assert(Not)Same(message, expected, actual) assertTrue(condition) assertTrue(message, condition)

18 Structure setUp() Storing the fixture's objects in instance variables of your TestCase subclass and initialize them by overriding the setUp method tearDown() Releasing the fixture’s

Writing a test suite Step 2: Create a test suite by choosing 19

Writing a test suite 20

Writing a test suite import junit.framework.Test; import junit.framework.TestSuite; public class AllTests { public static Test suite() { TestSuite suite = new TestSuite("Test for AiportSimulation"); //$JUnit-BEGIN$ suite.addTestSuite(Lab3QueueTest.class); //$JUnit-END$ return suite; } 21

Running a test 22 AllTests -> choose Run -> Run As -> Junit Test

Running a test 23

24 Design Test Cases The real world scenarios The number boundaries

25 Tips Testcases must extend TestCase All ‘test’ methods must include at least one call to an assert method or to fail: assertEquals (String message,...) assertNotNull (String message, Object obj) assertNull (String message, Object obj) assertSame (String message, Obj exp, Obj actual) assertTrue (String message, boolean condition) fail (String message) Remove System.out.println after test cases are working and rely on Junit assert methods to determine success/failure.

26 Dynamic Run Since JUnit 2.0 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. suite.addTestSuite(Lab3QueueTest.class);

Project 3 - Algorithm 27

Recursion

29 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways Recursion splits a problem into one or more simpler versions of itself

30 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways Recursion splits a problem into one or more simpler versions of itself

Chapter 7: Recursion31 Steps to Design a Recursive Algorithm Step 1: There must be at least one case (the base case), for a small value of n, that can be solved directly Step 2: A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case) Step 3: Recognize the base case and provide a solution to it Step 4: Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case Step 5: Combine the solutions of the smaller problems in such a way as to solve the larger problem

Chapter 7: Recursion32 Steps to Design a Recursive Algorithm Step 1: There must be at least one case (the base case), for a small value of n, that can be solved directly Step 2: A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case) Step 3: Recognize the base case and provide a solution to it Step 4: Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case Step 5: Combine the solutions of the smaller problems in such a way as to solve the larger problem

Chapter 7: Recursion33 Steps to Design a Recursive Algorithm Step 1: There must be at least one case (the base case), for a small value of n, that can be solved directly Step 2: A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case) Step 3: Recognize the base case and provide a solution to it Step 4: Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case Step 5: Combine the solutions of the smaller problems in such a way as to solve the larger problem

Chapter 7: Recursion34 Steps to Design a Recursive Algorithm Step 1: There must be at least one case (the base case), for a small value of n, that can be solved directly Step 2: A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case) Step 3: Recognize the base case and provide a solution to it Step 4: Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case Step 5: Combine the solutions of the smaller problems in such a way as to solve the larger problem

Chapter 7: Recursion35 Steps to Design a Recursive Algorithm Step 1: There must be at least one case (the base case), for a small value of n, that can be solved directly Step 2: A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case) Step 3: Recognize the base case and provide a solution to it Step 4: Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case Step 5: Combine the solutions of the smaller problems in such a way as to solve the larger problem

Chapter 7: Recursion36 Proving that a Recursive Method is Correct Proof by induction Prove the theorem is true for the base case Show that if the theorem is assumed true for n, then it must be true for n+1 Recursive proof is similar to induction Verify the base case is recognized and solved correctly Verify that each recursive case makes progress towards the base case Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly

Chapter 7: Recursion37 Recursive Definitions of Mathematical Formulas Mathematicians often use recursive definitions of formulas that lead very naturally to recursive algorithms Examples include: Factorial Powers Greatest common divisor If a recursive function never reaches its base case, a stack overflow error occurs

Chapter 7: Recursion38 Recursion Versus Iteration There are similarities between recursion and iteration In iteration, a loop repetition condition determines whether to repeat the loop body or exit from the loop In recursion, the condition usually tests for a base case You can always write an iterative solution to a problem that is solvable by recursion Recursive code may be simpler than an iterative algorithm and thus easier to write, read, and debug

Chapter 7: Recursion39 Efficiency of Recursion Recursive methods often have slower execution times when compared to their iterative counterparts The overhead for loop repetition is smaller than the overhead for a method call and return If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method The reduction in efficiency does not outweigh the advantage of readable code that is easy to debug

40 Efficiency of Recursion (continued) Efficient Inefficient

Chapter 7: Recursion41 Recursive Array Search Searching an array can be accomplished using recursion Simplest way to search is a linear search Examine one element at a time starting with the first element and ending with the last Base case for recursive search is an empty array Result is negative one Another base case would be when the array element being examined matches the target Recursive step is to search the rest of the array, excluding the element just examined

Chapter 7: Recursion42 Algorithm for Recursive Linear Array Search

Chapter 7: Recursion43 Design of a Binary Search Algorithm Binary search can be performed only on an array that has been sorted Stop cases The array is empty Element being examined matches the target Checks the middle element for a match with the target Throw away the half of the array that the target cannot lie within

Chapter 7: Recursion44 Design of a Binary Search Algorithm (continued)

Chapter 7: Recursion45 Efficiency of Binary Search and the Comparable Interface At each recursive call we eliminate half the array elements from consideration O(log2 n) Classes that implement the Comparable interface must define a compareTo method that enables its objects to be compared in a standard way CompareTo allows one to define the ordering of elements for their own classes

Chapter 7: Recursion46 Method Arrays.binarySearch Java API class Arrays contains a binarySearch method Can be called with sorted arrays of primitive types or with sorted arrays of objects If the objects in the array are not mutually comparable or if the array is not sorted, the results are undefined If there are multiple copies of the target value in the array, there is no guarantee which one will be found Throws ClassCastException if the target is not comparable to the array elements

Chapter 7: Recursion47 Method Arrays.binarySearch (continued)

Chapter 7: Recursion48 Recursive Data Structures Computer scientists often encounter data structures that are defined recursively Trees (Chapter 8) are defined recursively Linked list can be described as a recursive data structure Recursive methods provide a very natural mechanism for processing recursive data structures The first language developed for artificial intelligence research was a recursive language called LISP

Chapter 7: Recursion49 Recursive Definition of a Linked List A non-empty linked list is a collection of nodes such that each node references another linked list consisting of the nodes that follow it in the list The last node references an empty list A linked list is empty, or it contains a node, called the list head, that stores data and a reference to a linked list

Chapter 7: Recursion50 Problem Solving with Recursion Will look at two problems Towers of Hanoi Counting cells in a blob

Chapter 7: Recursion51 Towers of Hanoi

Chapter 7: Recursion52 Towers of Hanoi (continued)

Chapter 7: Recursion53 Counting Cells in a Blob Consider how we might process an image that is presented as a two-dimensional array of color values Information in the image may come from X-Ray MRI Satellite imagery Etc. Goal is to determine the size of any area in the image that is considered abnormal because of its color values

Chapter 7: Recursion54 Counting Cells in a Blob (continued)

Chapter 7: Recursion55 Counting Cells in a Blob (continued)

Chapter 7: Recursion56 Counting Cells in a Blob (continued)

Chapter 7: Recursion57 Backtracking Backtracking is an approach to implementing systematic trial and error in a search for a solution An example is finding a path through a maze If you are attempting to walk through a maze, you will probably walk down a path as far as you can go Eventually, you will reach your destination or you won’t be able to go any farther If you can’t go any farther, you will need to retrace your steps Backtracking is a systematic approach to trying alternative paths and eliminating them if they don’t work

Chapter 7: Recursion58 Backtracking (continued) Never try the exact same path more than once, and you will eventually find a solution path if one exists Problems that are solved by backtracking can be described as a set of choices made by some method Recursion allows us to implement backtracking in a relatively straightforward manner Each activation frame is used to remember the choice that was made at that particular decision point A program that plays chess may involve some kind of backtracking algorithm

Chapter 7: Recursion59 Backtracking (continued)