Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #"— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 # 12 2014

2 © Peter Andreae COMP 102 12: 2 Menu CartoonStrip answers Understanding input More on Loops Admin Test Monday, 5-6pm Abdust - Gibbs: HM LT205 Giles - Pasa: KK LT303 Patel - Zhu: MC LT103 Students in PHYS114: MC LT103 6-7pm.

3 © Peter Andreae COMP 102 12: 3 CartoonStrip: core public void animate(){ CartoonFigure face1 = new CartoonFigure(150, 100, "yellow"); face1.lookRight(); face1.lookLeft(); face1.frown(); face1.speak("Is anyone here?"); CartoonFigure face2 = new CartoonFigure(450, 100, “blue"); face2.smile(); face2.lookLeft(); face2.speak("Hello"); face1.lookRight(); : face2: face1: CartoonFigure-16CartoonFigure-17 face1: CartoonFigure-16 face2: CartoonFigure-17

4 © Peter Andreae COMP 102 12: 4 CartoonStrip: Completion public void threeDancers(){ CartoonFigure fig1 = new CartoonFigure( 150, 100, “blue"); CartoonFigure fig2 = new CartoonFigure( 150, 100, “blue"); this.dance( fig1 ); this.dance( fig2 ); } public void dance( CartoonFigure fig ){ fig.smile(); fig.lookLeft(); fig.walk(20); fig.lookRight(); fig.walk(40); fig.lookLeft(); fig.walk(20); }

5 © Peter Andreae COMP 102 12: 5 Text Input: Asking for one value To ask user for a single value: UI.askString("Enter name: ") UI.askInt("How old are you: ") UI.askDouble("How tall are you: ") All of these: clear all the current input, print out the prompt read the value they want from the user (repeating the question if the answer is the wrong type) clear all remaining input eg How old are you: 57 years old ↩

6 © Peter Andreae COMP 102 12: 6 Text Input: reading multiple values When you type into the text pane: nothing happens until you type a newline (“enter”) Then all the characters on the line are put into a buffer that the program can access The program can access the buffer using the “UI.next…” methods: UI.next()reads next “token” as a string UI.nextInt()reads next “token” as an integer UI.nextDouble()reads next token as a double UI.nextLine() reads up to next ↩ as a string all the methods move the cursor forward, past what was read. This 20 pt text has 3 ↩ ↩ ↩ This 20 pt text has 3 ↩ numbers, 46.32 words, and 6% spam. ↩  “This”  20  ERROR!  “pt text has 3” This 20 pt text has 3 numbers, 46.32 words, and 6% spam

7 © Peter Andreae COMP 102 12: 7 Text Input: reading multiple values If there is no input yet, the UI.next…() methods will just wait. ⇒ Always print a prompt to the user before you try to read! It is not safe to call UI.nextInt() or UI.nextDouble() unless you can be certain the next token is an integer / double! How can you tell? UI.hasNextInt()  booleantrue if next token is an integer UI.hasNextDouble()  booleantrue if next token is a double UI.hasNext()  booleantrue if there is a next token (always true for text pane)

8 © Peter Andreae COMP 102 12: 8 next vs. nextLine() next(), nextInt(), nextDouble() picks up any spaces, discards them, and picks up next value (till it reaches a space), then returns the value next() returns it as a String nextInt() returns it as an int, nextDouble() returns it as a double. nextLine() Picks up all the values (including spaces) until it reaches end-of-line character, throws away end-of-line, and returns all the values (including spaces) as a String.

9 © Peter Andreae COMP 102 12: 9 Input with "next" methods MethodWhat it doesReturns next()Read and return next token of user’s inputString hasNext()Returns true if there is another token in the user input. Waits for the user to type something if necessary. boolean nextInt() nextDouble() Read the next token of the user's input. Return it as a integer if it is a number. Throws an exception if it is not a number. int double hasNextInt() hasNextDouble() Returns true if next token in the input is an int / double. Waits for user to type something if necessary. boolean nextBoolean()Read the next token of the user's input. Return true if it is "yes", "y", or "true", return false if it is "no", "n", or "false" (case insensitive). Throws an exception if it is anything else. boolean nextLine()Read the remaining characters of the user's input up to (but not including) the next end-of-line and return them as a string. Reads and throws away the end-of-line character. If there are no characters on the line, then it returns an empty string (""). String

10 © Peter Andreae COMP 102 12: 10 next methods...how they work /** sum up all numbers entered by user */ : UI.print(“Enter numbers: end with ‘done’:”); double sum = 0; while (UI.hasNextDouble() ) {//peeking at next value or “token” double amt = UI.nextDouble(); //getting the next value and move pointer sum = sum + amt; } UI.next(); UI.printf(“Total of all numbers entered: %.2f %n”, sum); Enter numbers: end with ‘done’: 40 60 30 50 done Enter numbers: end with ‘done’: 40 60 30 50 done * MENU *

11 © Peter Andreae COMP 102 12: 11 Reading words from user public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); // loop, stopping when you get to 'the' // read next token // increment count UI.printf(“You had %d words before 'the'. %n", count); }

12 © Peter Andreae COMP 102 12: 12 Reading words from user: BAD public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); while ( ! word.equalsIgnoreCase("the”) ) { String word = UI.next(); count = count + 1; } UI.printf(“You had %d words before 'the'. %n", count); } Too late for the condition!!!

13 © Peter Andreae COMP 102 12: 13 Reading words from user: BAD public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); String word = UI.next(); while ( ! word.equalsIgnoreCase("the”) ) { count = count + 1; } UI.printf(“You had %d words before 'the'. %n", count); } never reads the next word !!!

14 © Peter Andreae COMP 102 12: 14 Reading words from user: Fixed public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); String word = UI.next(); while ( ! word.equalsIgnoreCase("the”) ) { count = count + 1; word = UI.next(); } UI.printf(“You had %d words before 'the'. %n", count); } read next word at end of loop ("increment") read first word before loop

15 © Peter Andreae COMP 102 12: 15 Alternate design: using break. public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); while ( true ) { String word = UI.next(); if ( word.equalsIgnoreCase("the”) ){ break; } count = count + 1; } UI.printf(“You had %d words before 'the'. %n", count); } Note: Textbook does not like this style; I do Only use when the test has to be in the middle of the loop Typically only use with a while (true) {….. The condition is an exit condition, not a keep going condition gets out of the enclosing while loop

16 © Peter Andreae COMP 102 12: 16 Designing loops Is the number of steps determined at the beginning? int i = 0;int i = 1; while ( i < number) {while ( i <= number) {  do actions 〉 i = i + 1;i++;} Note, can count from 0 or from 1! Otherwise 〈 intialise 〉 while ( 〈 condition-to-do it again 〉 ) { 〈 body 〉 〈 increment 〉 } Shorthand for i = i+1

17 © Peter Andreae COMP 102 12: 17 Designing loops Does exiting the loop depend on the actions? if soor set up for the test while ( true ) { while ( test ) {set up for the test do actionsif ( exit-test ) { set up for the next test break; } do actions } Write out the steps for a couple of iterations including the tests to determine when to quit/keep going Identify the section that is repeated preferably starting with the test Wrap it with a while ( ) { } Identify the condition for repeating.


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #"

Similar presentations


Ads by Google