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

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
1 Review for Midterm 3 CS 101 Spring Topic Coverage  Loops Chapter 4  Methods Chapter 5  Classes Chapter 6, Designing and creating classes.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Chapter 5: Control Structures II (Repetition)
Java Programming: From the Ground Up
CS1101: Programming Methodology Recitation 3 – Control Structures.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Iteration. 2 Java looping  Options while do-while for  Allow programs to control how many times a statement list is executed.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 Iteration Chapter 6 Fall 2005 CS 101 Aaron Bloomfield.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
1 Iteration Chapter 6 Fall 2006 CS 101 Aaron Bloomfield.
1 Iteration. 2 Java looping  Options while do-while for  Allow programs to control how many times a statement list is executed.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Slides by Evan Gallagher
Slides by Evan Gallagher
C++ Iterative Constructs
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Control Structures II
Iteration.
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Iteration.
Review for Midterm 3.
Review for Midterm 3.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Exceptions.
Exceptions.
Presentation transcript:

Iteration

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

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

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

While syntax and semantics

While semantics for averaging problem

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 Average 2.1

While Semantics

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: Service.java

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: valuesProcessed

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: valuesProcessed 0

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: valuesProcessed 0 4.5

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: valuesProcessed 0 4.5

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: valuesProcessed 0 4.5

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: valuesProcessed 4.5 1

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: valuesProcessed

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: valuesProcessed

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: valuesProcessed

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: valuesProcessed

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: valuesProcessed

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: valuesProcessed 5.0

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: valuesProcessed 5.0

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: valuesProcessed 5.0 average 2.5

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: valuesProcessed 5.0 average 2.5

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?

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?

Reading a file Background

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

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();

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

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

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

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

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

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

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

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

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

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

The For Statement

For statement syntax

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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); }

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

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

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, = 2, = 3, = 5, = 8, and so on Fibonacci.java

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

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:

Problem solving

Internet per 100 people for 189 entities

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

What facilitators are needed?

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

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

What constructors are needed?

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

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

What instance variables are needed?

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

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();

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();

Example usage

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

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

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; }

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

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

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; }

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(); }