160 likes | 181 Views
CS100J Lecture 5. Previous Lecture Programming Concepts Rules of thumb learn and use patterns inspiration from hand-working problem boundary conditions validation Pattern for processing input values up to (but not including) a stopping signal Example Processing exam grades
E N D
CS100J Lecture 5 • Previous Lecture • Programming Concepts • Rules of thumb • learn and use patterns • inspiration from hand-working problem • boundary conditions • validation • Pattern for processing input values up to (but not including) a stopping signal • Example • Processing exam grades • Java Constructs • Casts and Rounding • Reading, Lewis & Loftus, Section 3.9 • This Lecture • Programming Concepts • Programming by stepwise refinement • a pattern • sequential refinement • case analysis • iterative refinement • Use of comments as higher-level statements Lecture 5
Programming By Stepwise Refinement • An “algorithm” for you to follow when writing a program that solves problem P: if ( P is simple enough to code immediately ) Write the code that solves P; else { Refine P into subproblems; Write the code that solves each subproblem; } • Stepwise refinement is an example of • a divide-and-conquer algorithm, because it breaks problems down into simpler parts, • a recursive algorithm, because you use the same “algorithm” to write the code for any given subproblem. • The refinement of P into subproblems must include a description of how the code segments solving the subproblems combine to form code that solves P. • You can write the code segments that solve the subproblems in any order. Lecture 5
Ways to Refine P into Subproblems • A program pattern • Do whatever n times • Process input values up until (but not including) a stopping value. • A sequential refinement • A case analysis • An iterative refinement Lecture 5
Sequential Refinement • The refinement is structured so that solving P1 through Pnin sequence solves P. /* Solve problem P */ { /* Solve subproblem P1*/ . . . /* Solve subproblem P2*/ . . . . . . /* Solve subproblem Pn*/ . . . } Lecture 5
Sequential Refinement, cont. • Example 1: /* Drive from Ithaca to NYC. */ { /* Drive from Ithaca to Binghamton */ . . . /* Drive from Binghamton to NYC */ . . . } • Example 2: /* Drive from Ithaca to NYC. */ { /* Drive from Ithaca to Albany. */ . . . /* Drive from Albany to NYC. */ . . . } Lecture 5
Sequential Refinement, cont. • Example 1, continued /* Drive from Ithaca to NYC. */ { /* Drive from Ithaca to Binghamton. */ . . . /* Drive from Binghamton to NYC. */ /* Drive from Binghamton to Stroudsburg. */ . . . /* Drive from Stroudsburg to NYC. */ . . . } Lecture 5
Sequential Refinement, cont. • Example 3 Let X1,…,Xn be an ordered sequence of variables. Let k be an integer between 1 and n. /* Rotate the values in X1,…,Xn left k places, where values shifted off the left end reenter at the right. */ E.g., suppose k = 3, n = 7, and the x’s contain letters. Before: x1 x2 x3 x4 x5 x6 x7 a b c d e f g After: x1 x2 x3 x4 x5 x6 x7 d e f g a b c Lecture 5
Sequential Refinement, cont. /* Rotate the values in X1,…,Xn left k places, where values shifted off the left end reenter at the right. */ { /* Reverse the order of X1,…,Xk */ . . . /* Reverse the order of Xk+1,…,Xn */ . . . /* Reverse the order of X1,…,Xn */ . . . } Lecture 5
Sequential Refinement, cont. • Example 3, continued E.g., suppose k = 3 and n = 7 x1 x2 x3 x4 x5 x6 x7 a b c d e f g c b a d e f g c b a g f e d d e f g a b c time Lecture 5
Case Analysis • The refinement is structured so that solving one of the subproblems P1,…,Pn solves P. Which Pi is solved is determined during execution by testing conditions. /* Solve problem P */ if (P is an instance of case 1) /* Solve subproblem 1 */ . . . else if (P is an instance of case 2) /* Solve subproblem 2 */ . . . else if (P is an instance of case 3) /* Solve subproblem 3 */ . . . . . . else /* P is an instance of case n */ /* Solve subproblem n */ . . . Lecture 5
Case Analysis, cont. • Example 1 /* Let x be the absolute value of y. */ if ( y < 0 ) /* Let x be -y. */ . . . else /* Let x be y. */ . . . • Example 2 /* Let x be the absolute value of y. */ x = Math.abs(y); I.e., sometimes case analysis is counter-productive and there is a uniform way to solve the problem. Lecture 5
Iterative Refinement • The refinement of P is structured so that repeated solution of subproblem P’ eventually solves P. /* Solve problem P */ while (P has not yet been solved) /* Solve subproblem P’ */ . . . • Question: How can repeatedly doing P’ solve P ? • Answer: P’ must be parameterized in terms of some state. Each execution of P’ must change the state so that progress is made, i.e., with each iteration, we move to a state that is “closer” to a solution for P. • The notion of “distance” must be well-founded, i.e., it must converge to 0 in a finite number of steps that get “closer”. • Different notions of “distance” lead to different programs. Lecture 5
Iterative Refinement, cont. • Example: Running a maze /* There are n2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper-leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ • Rule of thumb: Work some test cases by hand. Inspiration: Keep your left hand on the wall and keep walking (the left-hand rule). Lecture 5
Iterative Refinement, cont. /* There are n2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper-leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ while ( you are not in lower rightmost room ) /* Get “closer” to lower rightmost room. */ . . . • Different notions of “closer” lead to different versions of /* Get “closer” to lower rightmost room. */ . . . Notion A: wall length away, using the “left-hand rule”. Notion B: # of rooms away, using the “left-hand rule”. Lecture 5
Iterative Refinement, cont. • Refinement A (using wall-length distance) /* There are n2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper-leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ while ( you are not in lower rightmost room ) /* Get at least one wall “closer” to the lower rightmost room. */ if (you are facing a door){ Go through the door; Turn 90 degrees counter-clockwise; } else Turn 90 degrees clockwise; Lecture 5
Iterative Refinement, cont. • Refinement B (using #rooms distance) /* There are n2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper-leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ while ( you are not in lower rightmost room ) { /* Get at least one room “closer” to the lower rightmost room. */ while (you are not facing a door) Turn 90 degrees clockwise; Go through door; Turn 90 degrees counter-clockwise; } Lecture 5