valueSum 6.3 value -1 average 2.1 30 Execution Trace Suppose input contains 4.5 0.5 1.3 -1 3 valuesProcessed
int valuesProcessed 0
double valueSum 0
double value stdin.nextDouble())
while (value gt 0)
valueSum value
valuesProcessed
value stdin.nextDouble())
if (valuesProcessed gt 0)
double average valueSum / valuesProcessed
System.out.println("Average " average)
else
System.out.println("No list to average")
valueSum 6.3 value -1 average 2.1 31 Converting text to strictly lowercase
public static void main(String args)
Scanner stdin Scanner.create(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)
32 Sample run 33 Program trace
public static void main(String args)
Scanner stdin Scanner.create(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)
34 Program trace
public static void main(String args)
Scanner stdin Scanner.create(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)
35 Program trace
public static void main(String args)
Scanner stdin Scanner.create(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)
36 Program trace
public static void main(String args)
Scanner stdin Scanner.create(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)
37 Program trace 38 Converting text to strictly lowercase
public static void main(String args)
Scanner stdin Scanner.create(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)
39 Loop design
Questions to consider in loop design and analysis
What initialization is necessary for the loops test expression?
What initialization is necessary for the loops 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?
40 Reading a file
Background
41 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
42 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()
43 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 44 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 45 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 46 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 47 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 48 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 49 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 50 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 51 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 52 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 53 The For Statement 54 The For Statement 55 The For Statement 56 The For Statement 57 The For Statement 58 (No Transcript) 59 For statement syntax 60 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i 0 61 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i 0 62 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i 0 63 Execution Trace
for (int i 0 i lt 3 i)
System.out.println(i is " i)
System.out.println(all done")
i is 0
i 0 64 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i 1 65 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i 1 66 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i 1 67 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i 1 68 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i 2 69 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i 2 70 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i is 2
i 2 71 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i is 2
i 2 72 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i is 2
i 3 73 Execution Trace
for (int i 0 i lt 3 i)
System.out.println("i is " i)
System.out.println("all done")
i is 0
i is 1
i is 2
i 3 74 Execution Trace
for (int i 0 i lt 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 75 Nested loops
int m 2
int n 3
for (int i 0 i lt n i)
System.out.println("i is " i)
for (int j 0 j lt m j)
System.out.println(" j is " j)
76 Nested loops
int m 2
int n 3
for (int i 0 i lt n i)
System.out.println("i is " i)
for (int j 0 j lt 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
77 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 Expression false 78 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
79 Problem solving 80 Internet per 100 people for 189 entities
0.09 0.16 8.97 0.23 6.52 6.75 1.42 13.65 34.45
0.16 4.32 5.84 0.08 3.74 1.78 22.89 6.24 0.25
1.44 1.01 1.54 2.94 9.14 5.28 0.08 0.07 0.05
41.3 1.84 0.04 0.04 16.58 1.74 38.64 13.7 2.07
5.67 0.27 5.59 0.54 17.88 9.71 0.01 36.59 0.22
1.86 1.42 0.71 0.8 0.15 0.13 27.21 73.4 1.47
14.43 6.43 1.22 0.92 0.42 29.18 0.15 9.47 4.36
0.7 0.1 0.25 6.04 0.25 0.62 7.15 59.79 0.54
0.39 20.7 20.26 23.04 3.11 29.31 2.53 0.62 0.65
40.25 7.84 1.06 0.11 6.19 8.58 0.19 0.02 0.18
22.66 0.19 0.15 15.9 2.23 0.17 13.08 1.17 0.19
2.74 39.71 1.26 0.71 0.06 0.01 1.71 0.22 24.39
21.67 0.99 0.04 0.18 49.05 25.05 3.55 0.09 1.1
2.81 0.73 9.74 2.01 7.25 24.94 10.22 5.01 1.2
3.57 0.06 4.79 3.09 0.56 48.7 4.36 0.93 0.42
0.1 29.87 12.03 15.08 0.46 0.01 5.49 13.43 0.64
2.7 0.99 45.58 29.62 0.19 28.1 0.05 3.79 2.47
7.73 2.61 3.06 0.13 0.18 0.69 28.2 30.12 0.33
11.09 0.49 2.03 3.93 0.25 0.08 3.76 0.19 0.37
81 Data set manipulation
Often five values of particular interest
Minimum
Maximum
Mean
Standard deviation
Size of data set
Lets design a data set representation
82 What facilitators are needed? 83 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
84 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
85 What constructors are needed? 86 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
87 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
88 What instance variables are needed? 89 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
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.