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

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Loops – While, Do, For Repetition Statements Introduction to Arrays
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
CIS Computer Programming Logic
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
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.
Chapter 5 Loops.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Repetition Statements
Lesson #5 Repetition and Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding 2 David Davenport Computer Eng. Dept.,
Lesson #5 Repetition and Loops.
Chapter 6 More Conditionals and Loops
Java Coding 3 David Davenport Computer Eng. Dept.,
Methods Chapter 4: Methods Asserting Java ©Rick Mercer.
Repetition-Counter control Loop
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
CSS161: Fundamentals of Computing
Building Java Programs
Lesson #5 Repetition and Loops.
Java Coding 4 David Davenport Computer Eng. Dept.,
CS1100 Computational Engineering
Java Programming Loops
Module 4 Loops.
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Fundamentals of visual basic
3.1 Iteration Loops For … To … Next 18/01/2019.
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Looping III (do … while statement)
Chapter 3: Selection Structures: Making Decisions
Java Programming Loops
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Chapter 3: Selection Structures: Making Decisions
Lesson #5 Repetition and Loops.
Repetition Statements
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Building Java Programs
Building Java Programs
Java Coding 6 David Davenport Computer Eng. Dept.,
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 3 – part2 David Davenport Computer Eng. Dept., Over & over again! for & do-while David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr 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

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 email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

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

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

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( “*”);

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 4444444 1 212 32123 4321234 Try to apply top-down design to these problems Then implement & test in Java.

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

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

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 “ “ 1 “ “ “ “ 2 “ “ “ “ : “ “ “ “ last “ “

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

Question Given an integer, print its digits in reverse sequence, one per line. Enter integer: 372 2 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!

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!

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

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