Java Reference
In-Depth Information
Because the data for the two groups is recorded in separate files, the input data
appears in two separate files
Input:
Output:
As shown above
Reading the input data from both files is straightforward. Suppose the data is stored in
the file
group1.txt
for group 1 and in the file
group2.txt
for group 2. After
processing the data for one group, we can process the data for the second group for
the same course, and continue until we run out of data. Processing the data for each
course is similar and uses the following process:
a. Sum the scores for the course.
b. Count the number of students in the course.
c. Divide the total score by the number of students to find the
course average.
d. Output the results.
We are only comparing the averages of the corresponding courses in each group.
The data in each file is ordered according to the course ID. To ensure that only
the averages of the corresponding courses are compared, we compare the course
IDs for each group. If the corresponding course IDs are not the same, we output
an error message and terminate the program.
This discussion suggests that we should write a method,
calculateAverage
,tofindthe
course average. We should also write another method,
printResult
, to output the data
in the form given. By passing the appropriate parameters, we can use the same methods,
calculateAverage
and
printResult
, to process each course's data for both groups.
(In the second part of the program, we modify the method
printResult
.)
The preceding discussion translates into the following algorithm:
1. Initialize the variables.
2. Get the course IDs for group 1 and group 2.
3. If the course IDs are different, print an error message and exit the
program.
4. Calculate the course average for group 1 and group 2.
5. Print the results in the form given earlier.
6. Repeat Steps 2 through 5 for each course.
7. Print the final results.
PROBLEM
ANALYSIS AND
ALGORITHM
DESIGN
The preceding discussion suggests that the program needs the following variables for
data manipulation in the method
main
:
Variables
(Method
main
)
String courseId1;
//course ID for group 1
String courseId2;
//course ID for group 2
Search WWH ::
Custom Search