Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prepare a Class using Java for computing the average age of students found eithe

ID: 3849192 • Letter: P

Question

Prepare a Class using Java for computing the average age of students found either in a text file or in a data structure of your choice such as an array. You will also need to prepare a test program that uses the Class you designed and uses the inputs listed below to test whether the class calculates the average age correctly. See below for a description of the various text files the program should read. Come up with meaningful error messages for each input file case that does not allow the program to calculate the correct average age. The minimum and maximum age for a student are 15 and 70 respectively. The maximum number of students in a class is 11. Make sure appropriate messages are displayed. Input file 1 will be an empty file. Input file 2 will only have one valid age value. Input file 3 will have the following invalid values: 0, -5, 1,000,000, R, @, and } Input file 4 will have the following values: 13, 17, 19, 30, 45, 55 Input file 5 will have the following values 18, 19, 20, 21, 23, 18, 25, 32, 45, 66. Input file 6 will have the following values 15, 16, 18, 16, 17, 18, 19, 20, 22, 21, 33, 34, 23, 15 A sample error message received when using input file 1 is Input file 1: File is empty Make sure the program is well commented. Run all the text cases in just one pass. User keying-in of test values will not be allowed.

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
*
* @author Sam
*/
public class AverageFinder {
    static String fileName = "testFile"; //change it to meet youe needs
    public static void populate() throws FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        int[] data = new int[11]; //declare array of size 11
        int n = 0;
        String line;
        while((line = br.readLine())!=null) { // option to read more than 1 line
            String[] tokens = line.split(","); //split into tokens
            for (String token:tokens) {
                try {
                    data[n] = Integer.parseInt(token.trim()); // convert string to number
                } catch (ArrayIndexOutOfBoundsException e){ //exception when there are more than 11 items
                    System.err.println("The array can only store 11 elements. Please reduce elements");
                    return;
                } catch (NumberFormatException e) { // this error is thrown when the token is not an integer
                    System.err.println("The program as of now only accect Integers. '" + token + "' is not an Integer.");
                    return;
                }
                if (data[n] < 15 || data[n]>70) { //chk if the data is within range or not
                    System.err.println("The minimum and maximum age for a student are 15 and 70 respectively But the age of " + (n+1) + " is out of range.");
                    return;
                }
                n++;
            }
        }
        if (n == 0) { //if n is zero and no error caught earlier the file must be empty
            System.err.println("The file is empty");
            return;
        }
        double avg = getAvg(data, n); //if no error found calculate avg
        System.out.println("Avg: " + avg);
    }
  
    public static double getAvg(int[] arr, int size) { //simple method to calculate average given integer array and size
        int sum = 0;
        for (int i:arr)
            sum += i;
        return (double)sum/size;
    }
  
    public static void main(String[] args) throws FileNotFoundException, IOException {
        PrintWriter pw = new PrintWriter(fileName);
        System.out.println("File: 1");
        pw.close();
        populate();
        System.out.println("File: 2");
        pw = new PrintWriter(fileName);
        pw.print("18");
        pw.close();
        populate();
        System.out.println("File: 3");
        pw = new PrintWriter(fileName);
        pw.print("0, -5, 1,000,000, R, @, and }");
        pw.close();
        populate();
        System.out.println("File: 4");
        pw = new PrintWriter(fileName);
        pw.print("13, 17, 19, 30, 45, 55");
        pw.close();
        populate();
        System.out.println("File: 5");
        pw = new PrintWriter(fileName);
        pw.print("18, 19, 20, 21, 23, 18, 25, 32, 45, 66");
        pw.close();
        populate();
        System.out.println("File: 6");
        pw = new PrintWriter(fileName);
        pw.print("15, 16, 18, 16, 17, 18, 19, 20, 22, 21, 33, 34, 23, 15");
        pw.close();
        populate();
    }
}

I have commented the code for your ease. Let me know if you are facing any trouble understanding the code. I shall be glad to help you