Half-complete Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

The near-complete program is given below.

Half-complete Program

The following program computes the average for group "A". The average is computed using double precision floating point, even though the two quantities involved are both of type int. To do this, a type cast is used to convert sumA into a double so that the division is floating point division, not integer division.

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

class TestGroups
{
  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 sizeA;     // the number of students in group "A"
    int sumA = 0;   // the sum of scores for group "A"

    sizeA  = scan.nextInt();

    int count = 0; // initialize count

    while ( count < sizeA )
    {
      value = scan.nextInt();

      sumA  = sumA + value ; // add to the sum
      count = count + 1; // increment the count
    }

    if ( sizeA > 0 )
      System.out.println( "Group A average: " + ((double) sumA)/sizeA ); 
    else
      System.out.println( "Group A  has no students" );
  
    // Group "B"
  
    . . . .  more code will go here . . . . 
  }
}

QUESTION 10:

If you were writing this program on your own, what would be a nice thing to do at this point?