Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed.

Similar presentations


Presentation on theme: "Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed."— Presentation transcript:

1 Iteration

2 Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed

3 Averaging 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 needed

4 Averaging 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 processing NumberAverage.java

5 While syntax and semantics

6 While semantics for averaging problem

7 Averaging 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 run Enter positive numbers one per line. Indicate end of list with a negative number. 4.5 0.5 1.3 Average 2.1

8 While Semantics

9 Execution Trace 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); } else { System.out.println("No list to average"); } Suppose input contains: 4.5 0.5 -1 Service.java

10 Execution Trace 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); } else { System.out.println("No list to average"); } Suppose input contains: 4.5 0.5 -1 0 valuesProcessed

11 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum Suppose input contains: 4.5 0.5 -1 0 valuesProcessed 0

12 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 0 valuesProcessed 0 4.5

13 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 0 valuesProcessed 0 4.5

14 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 0 valuesProcessed 0 4.5

15 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 0 valuesProcessed 4.5 1

16 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 1 valuesProcessed 4.5 0.5

17 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 1 valuesProcessed 4.5 0.5

18 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 1 valuesProcessed 4.5 0.5 5.0

19 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 1 valuesProcessed 5.0 0.5 2

20 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 2 valuesProcessed 5.0 1.3

21 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 2 valuesProcessed 5.0

22 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 2 valuesProcessed 5.0

23 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 -1 2 valuesProcessed 5.0 average 2.5

24 Execution Trace 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); } else { System.out.println("No list to average"); } valueSum value Suppose input contains: 4.5 0.5 1.3 -1 2 valuesProcessed 5.0 average 2.5

25 Loop design Questions to consider in loop design and analysis –What initialization is necessary for the loop’s test expression? –What initialization is necessary for the loop’s processing? –What causes the loop to terminate? –What actions should the loop perform? –What actions are necessary to prepare for the next iteration of the loop? –What conditions are true and what conditions are false when the loop is terminated? –When the loop completes what actions are need to prepare for subsequent program processing?

26 Sentinel For some input data sets there is no reasonable sentinel value –Examples Process a series of arbitrary numbers Processing lines of arbitrary text What can we do? –Use Scanner methods and an operating system sentinel hasNext() hasNextInt()... –We will only use hasNext() MSDOS sentinelUnix sentinelMAC sentinel –CTRL-ZCTRL-D?

27 Reading a file Background

28 Reading a file Class File –Provides a system-independent way of representing a file name Constructor File(String s) –Creates a File with name s –Name can be either an absolute pathname or a pathname relative to the current working folder

29 Echoing a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close();

30 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Set up standard input stream

31 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Determine file name

32 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Determine the associated file

33 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Set up file stream

34 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Process lines one by one

35 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Is there any text

36 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Get the next line of text

37 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Display current line

38 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Make sure there is another to process If not, loop is done

39 Reading a file Scanner stdin = Scanner.create(System.in); System.out.print("Filename: "); String filename = stdin.next(); File file = new File(filename); Scanner fileIn = Scanner.create(file); while (fileIn.hasNext()) { String currentLine = fileIn.nextLine(); System.out.println(currentLine); } fileIn.close(); Close the stream

40 The For Statement

41

42

43

44

45

46 For statement syntax

47 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i 0

48 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i 0

49 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i 0

50 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println(“i is " + i); } System.out.println(“all done"); i is 0 i 0

51 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i 1

52 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i 1

53 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i 1

54 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i 1

55 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i 2

56 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i 2

57 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i is 2 i 2

58 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i is 2 i 2

59 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i is 2 i 3

60 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i is 2 i 3

61 Execution Trace for (int i = 0; i < 3; ++i) { System.out.println("i is " + i); } System.out.println("all done"); i is 0 i is 1 i is 2 all done 3 Variable i has gone out of scope – it is local to the loop

62 Nested loops int m = 2; int n = 3; for (int i = 0; i < n; ++i) { System.out.println("i is " + i); for (int j = 0; j < m; ++j) { System.out.println(" j is " + j); }

63 What’s the output int counter1 = 0; int counter2 = 0; int counter3 = 0; int counter4 = 0; int counter5 = 0; for (int i = 0; i < 5; ++i) { ++counter1; for (int j = 0; j < 10; ++j) { ++counter2; for (int k = 0; k < 2; ++k) { ++counter3; } ++counter4; } ++counter5; } System.out.println(counter1 + " " + counter2 + " " + counter3 + " " + counter4 + " " + c ounter5); Counting.java

64 Nested loops int m = 2; int n = 3; for (int i = 0; i < n; ++i) { System.out.println("i is " + i); for (int j = 0; j < m; ++j) { System.out.println(" j is " + j); } i is 0 j is 0 j is 1 i is 1 j is 0 j is 1 i is 2 j is 0 j is 1

65 What’s the output Implement a program that displays the first n numbers in the Fibonacci sequence, where n is a user-specified integer value The sequence starts with the following numbers: 1, 1, 2, 3, 5, 8, 13, 21 After the initial two 1s, each number in the sequence is the sum of the two previous numbers. For example, 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8, and so on Fibonacci.java

66 The do-while statement Syntax do Action while (Expression) Semantics –Execute Action –If Expression is true then execute Action again –Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true false Expression

67 Picking off digits Consider System.out.print("Enter a positive number: "); int number = stdin.nextInt()); do { int digit = number % 10; System.out.println(digit); number = number / 10; } while (number != 0); Sample behavior Enter a positive number: 1129 9 2 1 1

68 Problem solving

69 Internet per 100 people for 189 entities 0.090.168.970.236.526.751.4213.6534.45 0.164.325.840.083.741.7822.896.240.25 1.441.011.542.949.145.280.080.070.05 41.31.840.040.0416.581.7438.6413.72.07 5.670.275.590.5417.889.710.0136.590.22 1.861.420.710.80.150.1327.2173.41.47 14.436.431.220.920.4229.180.159.474.36 0.70.10.256.040.250.627.1559.790.54 0.3920.720.2623.043.1129.312.530.620.65 40.257.841.060.116.198.580.190.020.18 22.660.190.1515.92.230.1713.081.170.19 2.7439.711.260.710.060.011.710.2224.39 21.670.990.040.1849.0525.053.550.091.1 2.810.739.742.017.2524.9410.225.011.2 3.570.064.793.090.5648.74.360.930.42 0.129.8712.0315.080.460.015.4913.430.64 2.70.9945.5829.620.1928.10.053.792.47 7.732.613.060.130.180.6928.230.120.33 11.090.492.033.930.250.083.760.190.37 25.860.220.277.7837.231.750.941.86.09 3.1718.67.40.10.8645.0711.157.29

70 Data set manipulation Often five values of particular interest –Minimum –Maximum –Mean –Standard deviation –Size of data set Let’s design a data set representation

71 What facilitators are needed?

72 Implication on facilitators public double getMinimum() –Returns the minimum value in the data set. If the data set is empty, then Double.NaN is returned, where Double.NaN is the Java double value representing the status not-a-number public double getMaximum() –Returns the maximum value in the data set. If the data set is empty, then Double.NaN is returned

73 Implication on facilitators public double getAverage() –Returns the average value in the data set. If the data set is empty, then Double.NaN is returned public double getStandardDeviation() –Returns the standard deviation value of the data set. If the data set is empty, then Double.NaN is returned Left to the interested student public int getSize() –Returns the number of values in the data set being represented

74 What constructors are needed?

75 Constructors public DataSet() –Initializes a representation of an empty data set public DataSet(String s) –Initializes the data set using the values from the file with name s public DataSet(File file) –Initializes the data set using the values from the file Left to interested student

76 Other methods public void addValue(double x) –Adds the value x to the data set being represented public void clear() –Sets the representation to that of an empty data set public void load(String s) –Adds the vales from the file with name s to the data set being represented public void load(File file) –Adds the vales from the file to the data set being represented Left to interested student

77 What instance variables are needed?

78 Instance variables private int n –Number of values in the data set being represented private double minimumValue –Minimum value in the data set being represented private double maximumValue –Maximum value in the data set being represented private double xSum –The sum of values in the data set being represented

79 Example usage DataSet dataset = new DataSet("age.txt"); System.out.println(); System.out.println("Minimum: " + dataset.getMinimum()); System.out.println("Maximum: " + dataset.getMaximum()); System.out.println("Mean: " + dataset.getAverage()); System.out.println("Size: " + dataset.getSize()); System.out.println(); dataset.clear(); dataset.load("stature.txt"); System.out.println("Minimum: " + dataset.getMinimum()); System.out.println("Maximum: " + dataset.getMaximum()); System.out.println("Mean: " + dataset.getAverage()); System.out.println("Size: " + dataset.getSize()); System.out.println(); dataset.clear();

80 Example usage dataset.load("foot-length.txt"); System.out.println("Minimum: " + dataset.getMinimum()); System.out.println("Maximum: " + dataset.getMaximum()); System.out.println("Mean: " + dataset.getAverage()); System.out.println("Size: " + dataset.getSize()); System.out.println(); dataset.clear(); System.out.println("Minimum: " + dataset.getMinimum()); System.out.println("Maximum: " + dataset.getMaximum()); System.out.println("Mean: " + dataset.getAverage()); System.out.println("Size: " + dataset.getSize()); System.out.println();

81 Example usage

82 Methods getMinimum() and getMaximum() Straightforward implementations given correct setting of instance variables public double getMinimum() { return minimumValue; } public double getMaximum() { return maximumValue; }

83 Method getSize() Straightforward implementations given correct setting of instance variables public int getSize() { return n; }

84 Method getAverage() Need to take into account that data set might be empty public double getAverage() { if (n == 0) { return Double.NaN; } else { return xSum / n; }

85 DataSet constructors Straightforward using clear() and load() public DataSet() { clear(); } public DataSet(String s) throws IOException { clear(); load(s); }

86 Facilitator clear() public void clear() { n = 0; xSum = 0; minimumValue = Double.NaN; maximumValue = Double.NaN; }

87 Facilitator add() public void addValue(double x) { xSum += x; ++n; if (n == 1) { minimumValue = maximumValue = x; } else if (x < minimumValue) { minimumValue = x; } else if (x > maximumValue) { maximumValue = x; }

88 Facilitator load() public void load(String s) throws IOException { // get a reader for the file Scanner fileIn = Scanner.create( new File(s) ); // add values one by one while (fileIn.hasNext()) { double x = fileIn.nextDouble); addValue(x); } // close up file fileIn.close(); }


Download ppt "Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed."

Similar presentations


Ads by Google