Question

In: Computer Science

Task The task is to read a file, store the data into objects, and process the...

Task

The task is to read a file, store the data into objects, and process the objects. The file is formatted as pairs of lines: the first line of each pair is the name of a student, and the second line is a list of grades. This data will be stored in a Grades object. The Grades class will have several methods for processing the data.

For example, if this is the contents of the file being processed:

Alice
87 99 96 99 86 96 77 95 70 88
Bob
73 78 76 80 99 96 73 96 76 78 78 92 93 75 93
Camila
99 94 85 99 99 93 81 95 76 80 77 79 98 72 98 97 92
Diego
76 97 72 92 86 86 89 85 81 87 76 80 89

then the following should be printed:

Alice [87, 99, 96, 99, 86, 96, 77, 95, 70, 88]
        Name:    Alice
        Length:  10
        Average: 89.30
        Median:  91.5
        Maximum: 99
        Mininum: 70
Bob [73, 78, 76, 80, 99, 96, 73, 96, 76, 78, 78, 92, 93, 75, 93]
        Name:    Bob
        Length:  15
        Average: 83.73
        Median:  78.0
        Maximum: 99
        Mininum: 73
Camila [99, 94, 85, 99, 99, 93, 81, 95, 76, 80, 77, 79, 98, 72, 98, 97, 92]
        Name:    Camila
        Length:  17
        Average: 89.06
        Median:  93.0
        Maximum: 99
        Mininum: 72
Diego [76, 97, 72, 92, 86, 86, 89, 85, 81, 87, 76, 80, 89]
        Name:    Diego
        Length:  13
        Average: 84.31
        Median:  86.0
        Maximum: 97
        Mininum: 72 
    

Reading in the Data

Assume that the name of the file is data.txt . In Eclipse, the file needs to be in the top directory of the project. Reading all the data in the file can be accomplished by a sequence of code like:

Scanner in = null;
try {
    in = new Scanner(new File("data.txt"));
} catch (FileNotFoundException exception) {
    System.err.println("failed to open data.txt");
    System.exit(1);
}
while (in.hasNext()) {
    String name = in.next();
    while (in.hasNextInt()) {
        int grade = in.nextInt();
    }
}    
        

Of course, you need to add code so that Grades objects are created with the data that is read.

Testing

Each Grades object you create should be tested with the following static method.

public static void testGrades(Grades grades) {
    System.out.println(grades.toString()); 
    System.out.printf("\tName:    %s\n", grades.getName());
    System.out.printf("\tLength:  %d\n", grades.length());
    System.out.printf("\tAverage: %.2f\n", grades.average());
    System.out.printf("\tMedian:  %.1f\n", grades.median());
    System.out.printf("\tMaximum: %d\n", grades.maximum());
    System.out.printf("\tMininum: %d\n", grades.minimum());
}

This method should be included in your Lab2.java file exactly as it appears. testGrades should be called on each Grades object after all of its data has been stored in it. Note that this code requires that you implement several methods within the Grades class. Note: thetoString method should return a String and should not contain any print statements within it. See the example output above.

Solutions

Expert Solution

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) 
{
       string name;
       if(i%2==0)
       {
           System.out.print(line);
           name = line
       }
       else
       {
           String[] scores = line.split(" ");
           int[] array = Arrays.asList(scores).stream().mapToInt(Integer::parseInt).toArray();
                        System.out.println(" " + scores);
                        int sum = 0;
                        for (int num; array)
                        {
                            sum += num;
                        }
                        int l = array.length;
                        int maxim = max(array);
                        int minim = min(array);
                        int average = sum/l;
                        sort(array);
                        int median = l%2 == 0? (array[l/2] + array[l/2 -1])/2 : array[l/2 +  1];
                        System.out.println("Name: ", name);
                        System.out.println("Length: ", length);
                        System.out.println("Average: ", average);
                        System.out.println("Median: ", median);
                        System.out.println("Maximum: ", maxim);
                        System.out.println("Minimum: ", minim);

   }

}

Related Solutions

Data Engineers regularly collect, process and store data. In this task you will develop a deeper...
Data Engineers regularly collect, process and store data. In this task you will develop a deeper understanding of how C programming language can be used for collecting, processing and storing data. In this assignment you get the opportunity to build an interactive program that can manage the list of flights departing Sydney Airport. The list is stored as an array of flight_t type structures flight_t flights [MAX_NUM_FLIGHTS]; The flight_t is a structure typedef for struct flight. The struct flight contains...
language c++(Data structure) You have to read a file and store a data in character array...
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u you can use link list to store data
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return...
Your task is to open a file containing a list of dates in various formats, read...
Your task is to open a file containing a list of dates in various formats, read each date, determine if it is a valid format and print it in **ISO 8601 format**(YYYY/MM/DD) to console. The list will be terminated by a `-1`. There are **two** valid formats: - plain-language format: <Month> <Day>, <Year> where Day and Year are valid integers. ex. `March 3, 1990`. - reversed ISO format, `DD/MM/YYYY` (Note that we aren't concerned with the precise ordering of month...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of tuples, then print the list. [6 marks] Question7: Write Python code to do the following operations using request library. [12 marks] Check if the following webpage is available or not: https://edition.cnn.com/news.html [4 marks] Send a get request to the following webpage and show the result: http://api.open-notify.org/iss-pass.json [4 marks] The webpage from part 2 is expecting some parameters as shown from the results. Now create...
Write a java program that will read a file called stateinfo.txt and will store the information...
Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows. State                Capital ---------                         ----------- NSW               Sydney VIC                 Melbourne WA                 Perth TAS                 Tasmania QLD                Brisbane SA                   Adelaide The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT