Quiz 6 Solution

  1. Terms and Concepts
    (a)T
    (b)T
    (c)F
    (d)T
    (e)F
    (f)F
    (g)T
    (h)F

  2. Code Writing (code in green was given)
    public void averageInput() {
    	BufferedReader stdin = new BufferedReader(new InputStreamReader(
    	                                                System.in));
    	System.out.print("How many values do you want to enter? ");
    	int numValues = Integer.parseInt(stdin.readLine());
    
    	// make sure the user enters a non-negative number of values
    	while (numValues < 0) {
    		System.out.print("Please enter a non-negative number of values: ");
    		numValues = Integer.parseInt(stdin.readLine());
    	}
    
    	// prompt for the values, computing the sum as we go
    	double sum = 0.0;
    	for (int i = 0; i < numValues; i++) {
    		System.out.print("Enter value " + (i + 1) + ": ");
    		sum += Integer.parseInt(stdin.readLine());
    	}
    
    	// calculate and report the average
    	if (numValues == 0)                          // avoid dividing by 0
    		System.out.println("No values to average");
    	else
    		System.out.println("The average is " + sum/numValues);
    } // end method averageInput
    
  3. Code Tracing
    We:the:
    in:order:a:
    union:
    =:line:
    
  4. Short Answer
    1. Use try-catch, i.e. deal with the exception.
    2. List the exception in the throws clause, i.e. pass the buck.