IterationJava loopingAveraging valuesAveragingSlide 5Slide 6Slide 7Slide 8Program DemoWhile syntax and semanticsWhile semantics for averaging problemWhile SemanticsExecution TraceNew 2005 demotivatiors!Converting text to lower caseConverting text to strictly lowercaseSample runSlide 19Program traceSlide 21Loop Design & Reading From a FileLoop designReading a fileSlide 25Slide 26Today’s demotivatorsThe For statementThe For StatementSlide 30for statement syntaxfor vs. whileVariable declarationSlide 34Slide 35Slide 36Slide 37for loop indexingNested loopsSlide 40Another optical illusionLoop controlsThe continue keywordThe break keywordSlide 52Four HobosSlide 54ProblemAnalysisImplementationSlide 58ResultsAlternate implementationSlide 623 card poker3 Card PokerThe Card classCard class methodsThe Hand classProvided Hand methodsHand Methods to ImplementClass HandEvaluationSlide 71Becoming an IEEE authorTriangle countingThe programming assignmentSample executionSlide 76The Triangle classThe TriangleDemo classLook at that them there code…Slide 80End of this slide set?Fibonacci numbersFibonacci sequenceFibonacci sequence in natureReproducing rabbitsSlide 86Slide 87Slide 88Slide 89Slide 90The Golden RatioSlide 92Number countingSlide 94Slide 95Background: Prime numbersHow to time your codeSlide 98BigIntegersBigInteger usageSlide 1011IterationChapter 6Fall 2006CS 101Aaron Bloomfield2Java loopingOptionswhiledo-whileforAllow programs to control how many times a statement list is executed33Averaging valuesAveraging values4AveragingProblemExtract a list of positive numbers from standard input and produce their averageNumbers are one per lineA negative number acts as a sentinel to indicate that there are no more numbers to processObservationsCannot supply sufficient code using just assignments and conditional constructs to solve the problemDon’t how big of a list to processNeed ability to repeat code as needed5AveragingAlgorithmPrepare for processingGet first inputWhile there is an input to process do {Process current inputGet the next input}Perform final processing6AveragingProblemExtract a list of positive numbers from standard input and produce their averageNumbers are one per lineA negative number acts as a sentinel to indicate that there are no more numbers to processSample runEnter positive numbers one per line.Indicate end of list with a negative number.4.50.51.3-1Average 2.1public class NumberAverage {// main(): application entry pointpublic static void main(String[] args) {// set up the input// prompt user for values// get first value// process values one-by-onewhile (value >= 0) {// add value to running total// processed another value// prepare next iteration - get next value}// display resultif (valuesProcessed > 0)// compute and display averageelse// indicate no average to display}}int valuesProcessed = 0;double valueSum = 0;// set up the inputScanner stdin = new Scanner (System.in);// prompt user for valuesSystem.out.println("Enter positive numbers 1 per line.\n" + "Indicate end of the list with a negative number.");// get first valuedouble value = stdin.nextDouble();// process values one-by-onewhile (value >= 0) {valueSum += value;++valuesProcessed;value = stdin.nextDouble();}// display resultif (valuesProcessed > 0) {double average = valueSum / valuesProcessed;System.out.println("Average: " + average);} else {System.out.println("No list to average");}99Program DemoProgram DemoNumberAverage.javaNumberAverage.java10While syntax and semanticsLogical expression thatdetermines whether Actionis to be executedwhile ( Expression ) ActionAction is either a singlestatement or a statementlist within braces11While semantics for averaging problem// process values one-by-onewhile ( value >= 0 ) {// add value to running totalvalueSum += value;// we processed another value++valueProcessed;// prepare to iterate – get the next inputvalue = stdin.nextDouble();}Test expression is evaluated at thestart of each iteration of the loop.If test expression is true, these statementsare executed. Afterward, the test expressionis reevaluated and the process repeats12While SemanticsExpressionActiontruefalseExpression isevaluated at thestart of eachiteration of theloopIf Expression istrue, Action isexecutedIf Expression isfalse, programexecutioncontinues withnext statement13int valuesProcessed = 0;double valueSum = 0;double value = stdin.nextDouble();while (value >= 0) {valueSum += value;++valuesProcessed;value = stdin.nextDouble();}if (valuesProcessed > 0) {double average = valueSum / valuesProcessed;System.out.println("Average: " + average);}else {System.out.println("No list to average");}int valuesProcessed = 0;double valueSum = 0;double value = stdin.nextDouble();while (value >= 0) {valueSum += value;++valuesProcessed;value = stdin.nextDouble();if (valuesProcessed > 0) {double average = valueSum / valuesProcessed;System.out.println("Average: " + average);Execution TraceSuppose input contains: 4.5 0.5 1.3 -10valuesProcessedvalueSum 0value 4.5Suppose input contains: 4.5 0.5 1.3 -14.51Suppose input contains: 4.5 0.5 1.3 -10.55.021.36.3Suppose input contains: 4.5 0.5 1.3 -13-1Suppose input contains: 4.5 0.5 1.3 -1average 2.11414New 2005 demotivatiors!New 2005 demotivatiors!1616Converting text to lower caseConverting text to lower case17Converting text to strictly lowercasepublic static void main(String[] args) {Scanner stdin = new Scanner (System.in);System.out.println("Enter input to be converted:");String converted = "";while (stdin.hasNext()) { String currentLine = stdin.nextLine(); String currentConversion = currentLine.toLowerCase(); converted += (currentConversion + "\n");}System.out.println("\nConversion is:\n" + converted);}18Sample runA Ctrl+z wasentered. It is theWindows escapesequence forindicatingend-of-fileAn empty linewas entered1919Program DemoProgram DemoLowerCaseDisplay.javaLowerCaseDisplay.java20Program tracepublic static void main(String[] args) {Scanner stdin = new Scanner (System.in);System.out.println("Enter input to be converted:");String converted = "";while (stdin.hasNext()) { String currentLine = stdin.nextLine(); String currentConversion = currentLine.toLowerCase(); converted += (currentConversion + "\n");}System.out.println("\nConversion is:\n" + converted);}public static void main(String[] args) {Scanner stdin = new Scanner (System.in);System.out.println("Enter
View Full Document