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

This must use the skeleton code provided. Overview You will be writing a Java pr

ID: 652106 • Letter: T

Question

This must use the skeleton code provided.

Overview

You will be writing a Java program that produces a simple formatted report. The program reads a file with a specific input format and produces a summary report of the data in that file.

Objectives

Practice with programming fundamentals

Variables - Declaration and Assignment

Primitive types

Simple keyboard input and text display output

Branching: if-else if-else syntax, nested if-else syntax

Nested while loops

Methods - simple functions and procedures

ArrayLists - collections of variables

File I/O

Works towards the following Course Goals:

Competency with using basic coding features of a high-level imperative programming language

Competency with writing computer programs to implement given simple algorithms

Familiarity with designing simple text-oriented user interfaces

Project 11 Description

For this lab you will write a Java program that produces a simple formatted report. The program will prompt the user to enter a file name. This file must contain information in a specific format (detailed below). Each "block" of the file contains information for one player in a competition -- the name of the player followed by a number of different scores that that player achieved. The program should find each player's average score, median score and best and worst scores and display them in a line on the final summary report. The program should also determine which player has the highest average score and which player has the lowest average score. (HINT: Consider storing information in ArrayLists to make the final summaries easier to produce).

For this assignment you should start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. The skeleton provided for this assignment is much more "skeletal" than previous assignments. For this assignment you MUST add at least two extra methods beyond the methods defined in the skeleton. These methods must not be trivial one or two line methods, but a real breakdown of the code. Consider adding methods to write the formatted report, or helper methods for reading the input file or computing necessary values. Feel free to add any methods you find useful, and make sure that you add comments indicating what they do following the form of the rest of the comments in the code.

Project11.java

Input File Format

Your program needs to read files in a specific format. Each player is designated by a block of lines that follow the following format:

Where the elipses (...) indicate more scores are possible, though not required. A minimum of one score per player is required. Multiple blocks appended together form the whole file. For example, the following sample shows four players and their associated scores:

Note that different players will have different numbers of scores, but after the last score for a player there will always be a line with a negative number. Your code must be able to take this into account and not assume that every player will have the same number of scores.

A sample text file containing example scores can be found at the link below. Make sure your code works with ANY input file we might use that follows the same format. We will test your code with files other than the one provided below.

project11Input.txt


Output Report Format

Your program should generate a text file that follows a specific reporting format. For the sample file given above, here is a sample output report:

Final Overall Scores Report

Name                    Mean Median   Max   Min
------------------    ------ ------ ---- ----
Rory Williams             86      88    92    78
James Barnes              74      76    91    54
Sarah Jane Smith          88      86    95    82
Jack Sparrow              21      15    54     0

Total number of participants: 4
Highest average score: Sarah Jane Smith (88)
Lowest average score: Jack Sparrow (21)

Note that for this assignment you must be able to compute the Mean (average) of a list of scores and the Median of the same list of scores. The Mean can be found by adding together all of the scores and then dividing by the total number of scores (so Rory Williams's Mean is computed as (88+92+78)/3 = 86). A simple algorithm for finding the median of a list of numbers is:

Sort the list

If the list has an odd number of elements

The median is the value in the exact center of the sorted list

If the list has an even number of elements

The median is the average of the two elements in the middle of the list

So Rory Williams's Median can be computed by putting his scores in order: [78, 88, 92] and seeing that 88 is in the center of the list. Jack Sparrow's median can be found by putting his scores in order: [0,13,18,54] and taking the average of the middle two elements (13+18)/2 = 31/2 = 15 in integer math.

HINT: You may want to use the method Collections.sort() to sort your ArrayList of scores.

HINT 2: The formatting of the report is important. Make sure that your output lines up appropriately regardless of the scores that the players get. In particular, make sure that single digit scores and scores of 100 line up appropriately. You may want to use a formatted output method such as printf() or format() to make your report print appropriately.

Project 11 Sample Output

This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.  

Enter an input file name: project11Input.txt
Enter an output file name: project11Output.txt

The console output for this assignment is very boring - the real output for this assignment will be in the creation of the output file as discussed above. Make sure that your program behaves appropriately if the input file does not exist or the output file cannot be written to, and that if a user attempts to use the same name for the output file that they provided as an input file, the program warns them and makes them select a new output file name.

Explanation / Answer

public class Project11

{

    public static void main(String[] args) throws IOException

    {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter an input file name: ");

        String input = in.nextLine();

        File inputFile = new File(input);

        List<Integer> List = readNextSeries(inputFile);

        int median = getMedian(List);

        int mean = getAverage(List);

        int max = getMaximum(List);

        int min = getMinimum(List);

        System.out.print("Enter an output file name: ");

        String out = in.nextLine();

        PrintWriter outputFile = new PrintWriter(out);

        System.out.println("Median = " + median);

        System.out.println("Mean = " + mean);

        System.out.println("Max = " + max);

        System.out.println("Min = " + min);

        outputFile.println(median);

        outputFile.println(mean);

        outputFile.println(max);

        outputFile.println(min);

        outputFile.close();

    }

    private static List<Integer> readNextSeries(File f)

    {

        ArrayList<Integer> List = new ArrayList<Integer>();

        Try

     {

            Scanner fileScan = new Scanner(f);

            while (fileScan.hasNextInt())

         {

                int value = Integer.parseInt(fileScan.next());

                List.add(value);

            }

        }

catch (FileNotFoundException e)

{}

        return List;

    }

    private static int getMedian(List<Integer> inList)

   {

        int Middle = inList.size() / 2;

        if (inList.size() % 2 == 1)

      {

            return inList.get(Middle);

        }

        else

      {

            return (inList.get(Middle - 1) + inList.get(Middle)) / 2;

        }

    }

    private static int getAverage(List<Integer> inList)

   {

        int total = 0;

        int average = 0;

        for(int element:inList)

     {

            total += element;

        }

        average = total / inList.size();

        return average;

    }

    private static int getMaximum(List<Integer> inList)

   {

        int largest = inList.get(0);

        for (int i = 1; i < inList.size(); i++)

     {

            if (inList.get(i) > largest)

          {

                largest = inList.get(i);

            }

        }

        return largest;

    }

    private static int getMinimum(List<Integer> inList)

{

        int smallest = inList.get(0);

        for (int i = 1; i < inList.size(); i++)

     {

            if (inList.get(i) < smallest)

         {

                smallest = inList.get(i);

            }

        }

        return smallest;

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote