go to previous page   go to home page   go to next page highlighting

Answer:

  1. What is an advantage of this form of input file?
    • You don't have to count up how many integers are to be averaged.
  2. What is a disadvantage of this form of input file?
    • The integers must all be positive or zero.

Problems with Sentinels

Sometimes a programmer picks a sentinel value that will "never occur in the data." Of course, years later the program is used in a new situation where that special value does occur in the data and problems arise.

Here is part A of the program that will work with this type of input data file. Of course, there are some blanks for you to fill.


import java.io.*;
import java.util.Scanner;

public class TestGroupsSentinel
{
  public static void main ( String[] args ) throws IOException
  {
    int value;     // the value of the current integer
    
    // Prompt for and open the input file   
    Scanner user = new Scanner( System.in );
    System.out.print("File name? ");
    String fileName = user.next().trim();
    Scanner scan = new Scanner( new File(fileName) );  

    // Group "A"
    int sumA   = ______;      // initialize the sum of scores for group "A"
    int countA = ______;      // initialize the group A count
    
    while ( (value = scan.nextInt()) != -1 )
    {
      sumA   = ______ ; // add to the sum
      countA = countA + 1;        // increment the count
    }

    if ( countA > ______ )
      System.out.println( "Group A average: " + ((double) sumA)/countA ); 
    else
      System.out.println( "Group A  has no students" );


QUESTION 15:

Of course, you are eager to fill in those blanks.


go to previous page   go to home page   go to next page