DOC PREVIEW
UVA CS 101 - Iteration

This preview shows page 1-2-3-4-5-6-41-42-43-44-45-46-83-84-85-86-87-88 out of 88 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 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 88 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 88 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

IterationJava loopingAveragingSlide 4While syntax and semanticsWhile semantics for averaging problemSlide 7While SemanticsExecution TraceSlide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Loop designSentinelReading a fileSlide 28Echoing a fileSlide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39The For StatementSlide 41Slide 42Slide 43Slide 44Slide 45For statement syntaxSlide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Nested loopsWhat’s the outputSlide 64Slide 65The do-while statementPicking off digitsProblem solvingInternet per 100 people for 189 entitiesData set manipulationWhat facilitators are needed?Implication on facilitatorsSlide 73What constructors are needed?ConstructorsOther methodsWhat instance variables are needed?Instance variablesExample usageSlide 80Slide 81Methods getMinimum() and getMaximum()Method getSize()Method getAverage()DataSet constructorsFacilitator clear()Facilitator add()Facilitator load()IterationJava looping•Options–while–do-while–for•Allow programs to control how many times a statement list is executedAveraging•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 neededAveraging•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•Algorithm–Prepare for processing–Get first input–While there is an input to process do {•Process current input•Get the next input–}–Perform final processingNumberAverage.javaWhile syntax and semanticsLogical expression thatdetermines whether Actionis to be executed — ifExpression evaluates totrue, then Action isexecuted; otherwise, theloop is terminatedwhile ( Expression ) ActionAction is either a singlestatement or a statementlist within braces. The actionis also known as the body ofthe loop. After the body isexecuted, the test expressionis reevaluated. If theexpression evaluates to true,the body is executed again.The process repeats until thetest expression evaluates tofalseWhile semantics for averaging problem// process values one-by-onewhile (value >= 0) { // add value to running total valueSum += value; // processed another value ++valuesProcessed; // prepare to iterate -- get the next input value = stdin.nextDouble());}Test expression is evaluated at thestart of each iteration of the loop.Its value indicates whether there is anumber to processIf test expression is true, these statementsare executed. Afterward, the test expressionis reevaluated and the process repeatsAveraging•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.1While SemanticsExpr essi onAct i ontruefalseExpression isevaluated at thestart of eachiteration of theloopIf Expression istrue, Action isexecutedIf Expression isfalse, programexecutioncontinues withnext statementExecution Traceint 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");}Suppose input contains: 4.5 0.5 -1Service.javaExecution Traceint 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");}Suppose input contains: 4.5 0.5 -10valuesProcessedExecution Traceint 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");}valueSumSuppose input contains: 4.5 0.5 -10valuesProcessed0Execution Traceint 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");}valueSumvalueSuppose input contains: 4.5 0.5 -10valuesProcessed04.5Execution Traceint 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");}valueSumvalueSuppose input contains: 4.5 0.5 -10valuesProcessed04.5Execution Traceint 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");}valueSumvalueSuppose input contains: 4.5 0.5 -10valuesProcessed04.54.5Execution Traceint 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");}valueSumvalueSuppose input contains: 4.5 0.5 -10valuesProcessed4.54.51Execution Traceint valuesProcessed = 0;double valueSum =


View Full Document

UVA CS 101 - Iteration

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 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?