100 likes | 273 Vues
1 Nov. HW 1 comments Backtracking. HW 1. Original problem + 3 modifications Modified data structure Basic program structure List of operations and elaborated program structure Produce Java code and verify your design! There will be some logical errors, but easy to correct
E N D
1 Nov HW 1 comments Backtracking
HW 1 • Original problem + 3 modifications • Modified data structure • Basic program structure • List of operations and elaborated program structure • Produce Java code and verify your design! • There will be some logical errors, but easy to correct • Put the appropriate overall header, overall trailer and also header and trailer information • Overall header – class and main method headers • Overall trailer – ‘}’ - indicates end of class • Header – identifier declarations used in main • Trailer – empty • JWB encloses the generated code with beginning and ending braces (‘{‘ and ‘}’)
Java Headers and Trailers Overall header public class MultTable { public static void main(String[] args) Overall Trailer } //end class Header int rowNo, colNo, elementNo, MAX; Trailer - (empty)
Backtracking In the forms of iteration and selection used in JSP, the condition test precedes execution of a part. • we use read-ahead rule to handle serial input files if pattern is a single character or record • we use multiple read ahead rule if pattern is some fixed number, n, of characters (or records) • use backtracking if there is no fixed number, n
Backtracking Problem: A cardfile of punched cards is sorted into ascending sequence of values of a key which appears in each card. • 1st card for each group with a common key value is a header card • Other records are detail cards. • Each detail card carries an integer amount • It is required to produce a report showing the totals of amount for all keys
Backtracking Modified problem: file has a record-type field: • 'header‘ - indicates group-id of the set of records that follow • 'detail' - indicates that record belongs to a group and contains data • other – if not of type ‘header’ or ‘detail’, record is an error • Program should detect the following errors: • header missing • detail cards have some other identifier (not ‘detail’) • Groups containing errors should be listed as an errorlist (not totaled)
Backtracking Recognition problem: We cannot recognize a good group with any predetermined fixed read ahead We tackle the recognition problem using ‘backtracking’ in 3 stages: • Assume (posit) that a group will be good (ignore the recognition difficulty) and design the full program accordingly. • Verify each data along the way – if any data contradicts the assumption of good data, issue a quit statement that branches to the second selection (badgroup) c. Modify the program text resulting from stage b. to ensure that side-effects are repealed where necessary