CS100J Lecture 18 Previous Lecture Programming concepts This Lecture

Slides:



Advertisements
Similar presentations
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Advertisements

CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Matrices Jordi Cortadella Department of Computer Science.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
The for loop.
CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Windows Programming Lecture 03. Pointers and Arrays.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
Arrays Chapter 7.
CSG3F3/ Desain dan Analisis Algoritma
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Data Structures I (CPCS-204)
CSSE 230 Day 25 Skip Lists.
Two-Dimensional Arrays
Department of Computer Science
CS100J Lecture 19 Previous Lecture This Lecture
For loops, nested loops and scopes
EGR 115 Introduction to Computing for Engineers
CSE 143 Lecture 19 More Recursive Backtracking
Java Software Structures: John Lewis & Joseph Chase
Case Study 2 – Marking a Multiple-choice Test
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Java How to Program, Late Objects Version, 10/e
Board: Objects, Arrays and Pedagogy
Nested Loop Review and Two-Dimensional Arrays
Two-Dimensional Arrays
Review of Arrays and Pointers
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Building Java Programs
More 2 D Array.
Two-Dimensional Arrays
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Arrays Week 2.
Presented By: David Miller
Exercise: Dice roll sum
Week 4 Lecture-2 Chapter 6 (Methods).
Scope of variables class scopeofvars {
Building Java Programs
CSE 143 Lecture 18 More Recursive Backtracking
Building Java Programs
Building Java Programs
Continue with Life, Magic and Catch -up
EECE.2160 ECE Application Programming
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Building Java Programs
CS100J Lecture 14 Previous Lecture
CS100J Lecture 15 Previous Lecture This Lecture Sorting
Building Java Programs
Building Java Programs
CSE 143 Lecture 18 More Recursive Backtracking
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Building Java Programs
Presentation transcript:

CS100J Lecture 18 Previous Lecture Programming concepts This Lecture Two-dimensional arrays Java Constructs Constructors for two-dimensional arrays Initializers for arrays final Reading Lewis & Loftus Section 6.3 Savitch, Section 6.5 This Lecture Two dimensional arrays. Reasonable size problem (a past assignment). Stepwise refinement. Use of comments as high-level specifications: as high-level commands, as representation invariants. Incremental development and testing. Use of sentinels. Static declarations. Local declarations, scope, and the reuse of names. Heuristic algorithms. CS 100 Lecture 18

Knight’s Tour X Kn Chess is played on an 8-by-8 board. A knight can move 2 in one direction (horizontal or vertical) and 1 in the other direction (vertical or horizontal). For example, Kn can move to each square marked X. Problem. Write a program that tries to find a "knight's tour", e.g., starting in the upper left corner, the knight visits each of the 64 squares exactly once. Kn X CS 100 Lecture 18

Possible Solution 1 22 3 18 25 30 13 16 4 19 24 29 14 17 34 31 23 2 21 26 35 32 15 12 20 5 56 49 28 41 36 33 57 50 27 42 61 54 11 40 6 43 60 55 48 39 64 37 51 58 45 8 53 62 47 10 44 7 52 59 46 9 38 63 CS 100 Lecture 18

Representation Rule of Thumb. Avoid using literal constants in the code; define and use symbolic constants. Static class declarations visible to all methods. class Knight { /* Chess board B is N-by-N int array, for N == 8. */ final static int N = 8; static int [][] B = new int [N][N]; /* Unvisited squares are Blank. */ static final int Blank = 0; /* Board subscripts range from lo to hi. */ static final int lo = 0; static final int hi = 7; /* UNDEFINED, an illegal coordinate. */ static final int UNDEFINED = -1; } CS 100 Lecture 18

Main /* Try to find a Knight's Tour. */ static void main(String args[]) { /* Set B to all Blank. */ Initialize(); /* Set B to arrangement of the integers 1,2,3,... representing consecutive positions of the knight during the tour. Squares that the knight cannot reach remain Blank. */ Solve(); /* Print B in an 8-by-8 grid. */ Display(); } CS 100 Lecture 18

Initialize /* Set B to all Blank. */ static void Initialize() { for (int r = lo; r <= hi; r++) for (int c = lo; c <= hi; c++) B[r][c] = Blank; } CS 100 Lecture 18

Solve /* Set B to arrangement of the integers 1,2,3,... representing consecutive positions of the knight during the tour. Squares that the knight cannot reach remain Blank. */ static void Solve() { /* This procedure "stub" permits us to test the main program, Initialize, and Display routines. */ } CS 100 Lecture 18

Display /* Print B in an 8-by-8 grid. */ static void Display() { for (int r = lo; r <= hi; r++) { for (int c = lo; c <= hi; c++) System.out.print(B[r][c] + ” ”); System.out.println(); } Rule of Thumb. Develop your program using small procedures. Test it incrementally. CS 100 Lecture 18

Sample Intermediate State while ( ________________________) { } 6 43 0 0 48 39 0 37 1 22 3 18 25 30 13 16 4 19 24 29 14 17 34 31 23 2 21 26 35 32 15 12 20 5 0 49 28 41 36 33 0 0 27 42 0 0 11 40 0 0 45 8 0 0 47 10 44 7 0 0 46 9 38 0 CS 100 Lecture 18

Pattern /* Start at the beginning */ . . . while ( /* not past the end */ ) { /* Process the current “place” */ /* Go on to the next place (or to “past the end”). */ } CS 100 Lecture 18

Solve { /* Initial configuration. */ int move = 0; // moves so far. int r = lo; // current row of Kn. int c = lo; // current column of Kn. while (r != UNDEFINED) { /* Visit <r,c>. */ move++; B[r][c] = move; /* Let <r,c> be coordinates of an unvisited "neighbor" of current <r,c>, or <UNDEFINED,UNDEFINED> if current square has no unvisited neighbors. */ . . . } CS 100 Lecture 18

Choosing a Neighbor to Visit /* Let <r,c> be coordinates of an unvisited "neighbor" of current <r,c>, or <UNDEFINED,UNDEFINED> if current square has no unvisited neighbors. */ int unvisitedR = UNDEFINED; int unvisitedC = UNDEFINED; for (int k = 0; k < 8; k++) { int Nrow = _____________________________; int Ncol = _____________________________; if ( B[Nrow][Ncol] == Blank ){ unvisitedR = Nrow; unvisitedC = Ncol; } r = unvisitedR; c = unvisitedC; CS 100 Lecture 18

Neighbors // 0 1 2 3 4 5 6 7 int deltaR[] = {-1, -2, -2, -1, 1, 2, 2, 1}; int deltaC[] = { 2, 1, -1, -2, -2, -1, 1, 2}; Kn 1 7 2 3 4 5 6 CS 100 Lecture 18

Boundary Conditions x x x x x x x x x x x x x x x x x x x x x x x x where X can be any non-zero value Must revise Initialize method to fill in border. (Exercise) 0 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x CS 100 Lecture 18

Representation Revisited Static class declarations visible to all methods. /* Chess board B is N-by-N int array, for N == 12. */ final int N = 12; int [][] B = new int [N][N]; /* Unvisited squares are Blank. */ final int Blank = 0; /* Board subscripts range from lo to hi. */ final int lo = 2; final int hi = 9; /* UNDEFINED, an illegal coordinate. */ final int UNDEFINED = -1; /* <deltaR[k],deltaC[k]> is <row,col> offset to neighbor k. */ final static int deltaR[] = {-1,-2,-2,-1, 1, 2,2,1}; final static int deltaC[] = {2 , 1,-1,-2,-2,-1,1,2}; CS 100 Lecture 18

Improvement Using Heuristic Go where the options are running out, i.e., go to the unvisited neighbor with the fewest unvisited neighbors. /* Let <r,c> be coordinates of an unvisited "neighbor" of current <r,c>, or <UNDEFINED,UNDEFINED> if current square has no unvisited neighbors. */ int fewest = 9; // min unvisited neighbors int neighborR = UNDEFINED; int neighborC = UNDEFINED; for (int k = 0; k < 8; k++) { int Nrow = r + deltaR[k]; int Ncol = c + deltaC[k]; if ( B[Nrow][Ncol] == Blank ){ int n = unvisited(Nrow, Ncol); if (n < fewest ) { neighborR = Nrow; neighborC = Ncol; fewest = n; } r = neighborR; c = neighborC; CS 100 Lecture 18

Unvisited /* == the number of neighbors of square <r,c> that are unvisited. */ static int unvisited(int r, int c) { int count = 0; // # unvisited neighs. for (int k = 0; k < 8; k++) { int Nrow = r + deltaR[k]; int Ncol = c + deltaC[k]; if ( B[Nrow][Ncol] == Blank ) count++; } return count; CS 100 Lecture 18