Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Coding 3 – part2 David Davenport Computer Eng. Dept.,

Similar presentations


Presentation on theme: "Java Coding 3 – part2 David Davenport Computer Eng. Dept.,"— Presentation transcript:

1 Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Over & over again! for & do-while David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Last revised: 20/10/2017 ~ part2 contains only for & do-while Previously: 8/11/2016, 26/10/2016 ~ minor revisions 20/10/2016 ~ Added new animated slide for sentinel-controlled input, etc. 22/10/2012

2 IMPORTANT… Students… Instructors…
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 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
for ( init; condition; update) statement; Java has three forms of repetition statement: while, for & do-while They can all essentially equivalent… here we will concentrate on for & do-while (while is in first part!) Statement to be repeated, input, output, assignment, decision, repetition! Condition exactly as for if statements where statement is any Java statement condition is a boolean expression

4 For & Do-while …other forms of repetition in Java
In Java the for loop can do anything the while loop can (this is not necessarily the case in other languages where it is often only used with integer loop control variables). We will use both for & do-while is special cases only: For counting like loops where the loop must be done at least once, eg. data validation & menus. For & Do-while

5 Java for statements Same as while loop!
Use as short-hand counting style loop statement condition true false update init for ( init; condition; update) statement; It has advantage of explicitly reminding you about init & update (which are easy to forget when writing a while loop). In some languages, for can only use integer (enumerable) loop-control-variables (suggest treating Java for statements same, i.e. only use for counting-style loops). Notes: variable i is often defined inside the for statement, e.g. for ( int i = 0; i < 5; i++) System;out.println( “*”); System;out.println( “i = “ + i); // will not compile! … if so, it will not be visible outside, i.e. after the loop. Variables only visible inside block they are defined in.  important! i++ is shorthand notation for i = i + 1; Perhaps redo triangle drawing question from previous slide? Example: for ( i = 0; i < 5; i = i + 1) System.out.println( “*”);

6 Questions (1) Write a program that asks the user to enter an integer number N, then prints a triangle with N lines of stars or numbers. For example, if N is 4 * *** ***** ******* ******* ***** *** * 1 222 33333 1 212 32123 Try to apply top-down design to these problems Then implement & test in Java.

7 Look at and learn to use Java API
Example Ask the user for their name, print it out one character per line, then print “done”. Enter your name: David D a v i d done. Look at and learn to use Java API Note: This problem does not explicitly require for statements. It is a good exercise though, and useful for looking at Java String class (and Math class too – precursor to static methods). Possibly show the alternative for syntax too? Solution: Ask for and get name for each character in name (from first to last) print that character from name and move to next line. print “done” Alternatively: while there are characters in name do print the first character of name and move to next line delete the first character from name Note: another soln: simply insert “\n” btw each of the characters! Extend, to print out in reverse order (i.e. “d, i, v, a, D”) And then to building up as String which can be printed in one go! (both as reverseNameString & nameString) Important abilities: should be able to divide and reconstruct String in either direction should be able to divide & reconstruct an integer number in either direction. print 1st char in name “ 2nd “ “ “ “ 3rd “ “ “ “ : “ “ “ “ last “ “ “ Need to be able to: “extract a character” from a String find “number of characters” in String

8 Using Java methods… To use, need to know Method name
Inputs & their types Output type output result type answer = f( x, y, z ); Method name input parameters // in Math class public static double sin( double d) double d = Math.sin( 0.5); Reading … and applying … Java API documentation! for Math & String only (static + Strings which are weird objects!) - though lessons are generally valid. Use ClassName.method(…) for “static” methods // in String class… public String substring( int beginIndex, int endIndex) // usage… shortString = longString.substring( 1, 3); varName.method(…) for non-static methods

9 Example Ask the user for their name, print it out one character per line, then print “done”. Enter your name: David D a v i d done. for (index = 0; index < name.length(); index++) System.out.println( name.charAt( index) ); Note: This problem does not explicitly require for statements. It is a good exercise though, and useful for looking at Java String class (and Math class too – precursor to static methods). Possibly show the alternative for syntax too? Solution: Ask for and get name for each character in name (from first to last) print that character from name and move to next line. print “done” Alternatively: while there are characters in name do print the first character of name and move to next line delete the first character from name Note: another soln: simply insert “\n” btw each of the characters! Extend, to print out in reverse order (i.e. “d, i, v, a, D”) And then to building up as String which can be printed in one go! (both as reverseNameString & nameString) Important abilities: should be able to divide and reconstruct String in either direction should be able to divide & reconstruct an integer number in either direction. for index = 0 to ___________ print charAt index in name print 1st char in name “ 2nd “ “ “ “ 3rd “ “ “ “ : “ “ “ “ last “ “ “ print charAt 0 in name “ “ “ “ “ “ “ “ “ “ : “ “ “ “ last “ “

10 Example Ask the user for their name, print it out one character per line, then print “done”. Enter your name: David D a v i d done. now do it in reverse! Enter your name: David d i v a D done. Note: This problem does not explicitly require for statements. It is a good exercise though, and useful for looking at Java String class (and Math class too – precursor to static methods). Possibly show the alternative for syntax too? Solution: Ask for and get name for each character in name (from first to last) print that character from name and move to next line. Alternatively: while there are characters in name do print the first character of name and move to next line delete the first character from name Extend, to print out in reverse order (i.e. “d, i, v, a, D”) And then to building up as String which can be printed in one go! (both as reverseNameString & nameString) Important abilities: should be able to divide and reconstruct String in either direction should be able to divide & reconstruct an integer number in either direction. Finally… From either sequence of characters, construct a String, reverseName

11 Question Given an integer, print its digits in reverse sequence, one per line. Enter integer: 7 3 done. Enter an integer: 372 Reversed is: 273 Now reconstruct the reverse integer Note: This problem does not explicitly require for statements. It is a good exercise though, and useful for looking at Java String class (and Math class too – precursor to static methods). Possibly show the alternative for syntax too? Solution: Ask for and get name for each character in name (from first to last) print that character from name and move to next line. Alternatively: while there are characters in name do print the first character of name and move to next line delete the first character from name Extend, to print out in reverse order (i.e. “d, i, v, a, D”) And then to building up as String which can be printed in one go! (both as reverseNameString & nameString) Important abilities: should be able to divide and reconstruct String in either direction should be able to divide & reconstruct an integer number in either direction. Note: try doing it without using the power function!

12 Java do-while statements
Repeat 1 or more times do statement; while (condition); statement condition true false loop Note layout… “{“ on same line as “while ();” can help avoid confusion! Also, “;” is required here (in do-while statement), but not in “while” statement Danger of this if used in “while” statement and in “if” statement. Example: i = 0; do { System.out.println( “*”); i++; } while ( i < 5); Note: “}” on same line & “;” at end!

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

14 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): _ Write algorithm for display menu & perform selection tasks Translate into Java. Remember to use named constants for selections! 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


Download ppt "Java Coding 3 – part2 David Davenport Computer Eng. Dept.,"

Similar presentations


Ads by Google