1 / 19

Java Coding 3

Java Coding 3. Over & over again!. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…

amma
Download Presentation

Java Coding 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Coding 3 Over & over again! David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr

  2. IMPORTANT… • Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! • Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you,David.

  3. Repetition • Java repetition statements while (condition) statement; do statement; while (condition); for ( init; condition; update) statement; • where • statement is any Java statement • condition is a boolean expression

  4. condition true statement false The while statement • Does statement while condition true • does statement 0 or more times while (condition) statement;

  5. Examples (1) • Print 5 asterisk characters • Use 5 println statements! • Use a single println statement! • Use a while loop… *****done count = 0;while ( count < 5 ) { System.out.println( “*”); count = count + 1;}System.out.println( “done”); If you print out count as well as the asterisk, what do you get?

  6. Examples (2) • Read & sum 5 values • By analogy… 53741sum is 20 sum = 0;count = 0;while ( count < 5 ) { value = scan.nextInt(); sum = sum + value; count = count + 1;}System.out.println( “sum is ” + sum);

  7. Examples (3) • Extract design patterns/templates • Counting loop… • Count, sum or product in loop… count = 0;while ( count < numberOfRepetitions ) {// process to repeat count = count + 1;} Immediately initialize to 0 (1) before loop

  8. Generic form of while • What happens if you fail to… • Initialise loop variables • Unpredictable behaviour • Update loop variables • Infinite loop! (in console app’s use Ctrl-C to exit) • In general… • condition must be falsifiable by applying update to initial values… need proof! Initialise any variables in conditionwhile (test condition variable) do statement & update condition variables;

  9. Proving loops terminate (1) • Do these print done? count = 0;while ( count < 5 ) { System.out.println( “*”); count = count - 1;}System.out.println( “done”); i = 1; while ( i != 50 ) { System.out.println( i); i = i + 2;} System.out.println( “done”);

  10. Proving loops terminate (2) • What about this one? i = scan.nextInt(); while ( i != 1 ) { if ( i % 2 == 0) i = i / 2; else i = 3 * i + 1;}System.out.println( “done”); • Proof that cannot always write program to determine whether given algorithm will halt or not. (ref. Halting Problem – Alan Turing.)

  11. Sentinel valuenon-data valuemarks end of list Reading set of data • Three approaches How many? 5 35741sum is 20 More? Y 3 More? Y 5More? Y 7More? Y 4More? Y 1More? N sum is 20 35741-1 sum is 20

  12. Sentinel-controlled input • Sum set of values terminated by -1 sum = 0 read value while value is not the sentinel do add value to sum read next value print sum • Extract another design pattern read value while value is not the sentinel process the value read next value

  13. Examples (sentinel input) • Find average of set of exam scores. • Count number of positive & negative values entered by user. Use zero to stop input. • Allow user to enter a set of positive values terminated by a zero. When the user enters zero, print a msg to indicate whether any value exceeded a threshold or not. • Read a set of positive values & report whether they were in ascending order or not. • Read a set of positive values & report their maximum. Extend to minimum.

  14. init condition false true statement update Java for statements • Same as while loop! • Use as short-hand counting style loop for ( init; condition; update) statement; Example: for ( i = 0; i < 5; i = i + 1) System.out.println( “*”);

  15. statement condition true false Java do-while statements • Repeat 1 or more times do statement; while (condition); Example: i = 0;do { System.out.println( “*”); i++; } while ( i < 5);

  16. Examples (do-while) • Data validation • e.g. Read positive value from user do ask for “a positive value” and get valuewhile value is not positive do { System.out.print( “Enter positive value: ”); value = scan.nextInt();} while ( value <= 0);// assert: value > 0

  17. Examples (do-while) • Menus - set of options for user to select from do display menu get selection from user perform selection while selection is not exit print “goodbye” ABC Trading Co.------------------ 1 – sales 2 – stock 3 – admin Select (0 to exit): _ if selection is SALES then // do sales things else if selection is STOCK then // do stock things else if selection is ADMIN then // do admin things else if selection is not EXIT then print “invalid selection” msg

  18. More practice? • Process characters in String • Use stringVariable.charAt( int pos)& stringVariable.length() • Process digits in an int • Use / & % to split it up • Compute PI & e using sequence • Compute square root (Newton Raphson) • Print table of projectile motion

More Related