Question

In: Computer Science

In java, Write a program that reads a data file containing final exam scores for a...

In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined.

The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process each score until the end of file is encountered. Once all data has been processed, display the statistics.

The input file should consist of integer values between 1 and 100. We will assume that the data file contains only valid values. The data file can be created using jGrasp by using File | New | Plain Text. The file should be stored in the same folder as the .java file.

Hint: The smallest and largest values can be determined as the data is read if the variables are initialized to opposite values. For example, the variable holding the largest score is initialized to 0, and then reset as needed after each data item is read.

The Scanner class should be used to read the file. End user input/output may be accomplished using either a) Scanner & print methods, OR (b) JOptionPane methods. All end user input and output must use the same I/O technique.

The throws clause should be added to the main method header:

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

Example run 1:

Enter the file name to be read: scores1.dat

15 scores were read:

14 passing scores

1 failing scores

The score ranges from 50 to 100

The average is 79.80

Example run 2:

Enter the file name to be read: scores2.dat

20 scores were read:

17 passing scores

3 failing scores

The scores ranges from 52 to 99

Solutions

Expert Solution

//Java code

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the file name to be read: ");
        String fileName = input.nextLine();

        //Open the file
        try
        {
            int max=0,min=0,counter =0,passingCounter=0,failingCounter=0;
            double sum=0,avg=0;
            Scanner scanFile = new Scanner(new File(fileName));
            while (scanFile.hasNextLine())
            {
                int num = scanFile.nextInt();
                if(counter==0)
                {
                    min =num;
                    max=num;
                }
                else
                {
                    if(min>num)
                        min = num;
                    if(max<num)
                        max = num;
                }
                if(num<60)
                {
                    failingCounter++;
                }
                else
                {
                    passingCounter++;
                }
                sum+=num;
                counter++;
            }
            //find average
            avg = sum/counter;
            //Print statics
            System.out.print(counter+" scores were read.\n"+passingCounter+" passing scores\n"+failingCounter+" failing scores\nthe score ranges from "+min+" to "+max+"\nThe average is "+String.format("%.2f",avg)+"\n");
        }
        catch (FileNotFoundException e)
        {
            System.err.println("ERROR!! file not found.");
        }
    }
}

//file

//Output

//If you need any help regarding this solution ......... please leave a comment ....... thanks


Related Solutions

Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
In C++ Write a program to store exam scores into a file. The program will ask...
In C++ Write a program to store exam scores into a file. The program will ask the user how many exam scores to enter. Then it will ask the user for each exam score and store them in a file called scores.dat The file output should have the following format: Exam 1: 97 Exam 2: 85
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
n-class: Write a program to store exam scores into a file. The program will ask the...
n-class: Write a program to store exam scores into a file. The program will ask the user how many exam scores to enter. Then it will ask the user for each exam score and store them in a file called scores.dat The file output should have the following format . Exam 1: 97 Exam 2: 85 C++ Coding please
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
An investigator collected data on midterm exam scores and final exam scores of elementary school students;...
An investigator collected data on midterm exam scores and final exam scores of elementary school students; results can summarized as follows. Average SD -------------------------------------------------- Boys' midterm score 70 20 Boys' final score 65 23 girls' midterm score 75 20 girls' final score 80 23 The correlation coefficient between midterm score and final score for the boys was about 0.70; for the girls, it was about the same. If you take the boys and the girls together, the correlation between midterm...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT