double total = 0; while (in.hasNextDouble()) { double input = in.nextDouble(); total = total + input; }
int upperCaseLetters = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { upperCaseLetters++; } }
What is wrong with this algorithm to find the average of a set of scores?
int total = 0; int count = 0; while (in.hasNextInt()) { int input = in.nextDouble(); total = total + input; count++; } double average = total / count;
boolean found = false; char ch = '?'; int position = 0; while (!found && position < str.length()) { ch = str.charAt(position); if (Character.isLowerCase(ch)) { found = true; } else { position++; } }
What are the values of found
, ch
, and
position
when the algorithm has finished processing the string
FRED
?
boolean valid = false; double input; while (!valid) { System.out.print("Please enter a positive value < 100: "); input = in.nextDouble(); if (0 < input && input < 100) { valid = true; } else { System.out.println("Invalid input."); } }
double input = in.nextDouble(); while (in.hasNextDouble()) { double previous = input; input = in.nextDouble(); if (input == previous) { System.out.println("Duplicate input"); } }
What happens with the algorithm "Comparing Adjacent Values" when only one input is provided?
System.out.print("Enter value, Q to quit: "); String input = in.next(); if (input.equalsIgnoreCase("Q")) We are done else { double x = Double.parseDouble(input); . . . }
boolean done = false; while (!done) { Print prompt String input = read input; if (end of input indicated) done = true; else { Process input } }
import java.util.Scanner; /** This program computes the average and maximum of a set of input values. */ public class DataAnalyzer { public static void main(String[] args) { Scanner in = new Scanner(System.in); DataSet data = new DataSet(); boolean done = false; while (!done) { System.out.print("Enter value, Q to quit: "); String input = in.next(); if (input.equalsIgnoreCase("Q")) done = true; else { double x = Double.parseDouble(input); data.add(x); } } System.out.println("Average = " + data.getAverage()); System.out.println("Maximum = " + data.getMaximum()); } }
/** Computes information about a set of data values. */ public class DataSet { private double sum; private double maximum; private int count; /** Constructs an empty data set. */ public DataSet() { sum = 0; count = 0; maximum = 0; } /** Adds a data value to the data set @param x a data value */ public void add(double x) { sum = sum + x; if (count == 0 || maximum < x) maximum = x; count++; } /** Gets the average of the added data. @return the average or 0 if no data has been added */ public double getAverage() { if (count == 0) return 0; else return sum / count; } /** Gets the largest of the added data. @return the maximum or 0 if no data has been added */ public double getMaximum() { return maximum; } }
Program Run:
Enter value, Q to quit: 10 Enter value, Q to quit: 0 Enter value, Q to quit: -1 Enter value, Q to quit: Q Average = 3.0 Maximum = 10.0
public void add(double x) { sum = sum + x; if (maximum < x) maximum = x; count++; }
What is the effect of this change?