DOC PREVIEW
UVA CS 101 - Iteration

This preview shows page 1-2-3-4-5-6-43-44-45-46-47-48-87-88-89-90-91-92 out of 92 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 92 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 loopingOptionswhiledo-whileforAllow programs to control how many times a statement list is executed33Averaging valuesAveraging values4AveragingProblemExtract a list of positive numbers from standard input and produce their averageNumbers are one per lineA negative number acts as a sentinel to indicate that there are no more numbers to processObservationsCannot supply sufficient code using just assignments and conditional constructs to solve the problemDon’t how big of a list to processNeed ability to repeat code as needed5AveragingAlgorithmPrepare for processingGet first inputWhile there is an input to process do {Process current inputGet the next input}Perform final processing6AveragingProblemExtract a list of positive numbers from standard input and produce their averageNumbers are one per lineA negative number acts as a sentinel to indicate that there are no more numbers to processSample 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 DemoNumberAverage.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 DemoLowerCaseDisplay.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

UVA CS 101 - Iteration

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download Iteration
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Iteration and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Iteration 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?