Java Reference
In-Depth Information
PROGRAMMING EXAMPLE:
Student Grade
Write a program that reads a student's first and last names followed by five test scores.
The program should output the student's first name, last name, the five test scores,
and the average test score. Output the average test score with two decimal places.
The data to be read is stored in a file named
test.txt
; the output should be stored
in a file named
testavg.out
.
Input:
A file containing the student's first name, last name, and the five test scores
Output:
The student's first name, last name, five test scores, and the average of the five
test scores, saved to a file
To find the average of the five test scores, you add the test scores and divide the sum
by 5. The input data is in the following form: the student's first name, followed by
the last name, followed by the five test scores. Therefore, we read the student's first
name, followed by the last name, followed by the five test scores. This problem
analysis translates into the following algorithm:
PROBLEM
ANALYSIS
AND
ALGORITHM
DESIGN
1. Get the student's first name, last name, and the five test scores.
2. Output the student's first name, last name, and the five test scores.
3. Calculate the average.
4. Output the average.
You output the average test score in the fixed-decimal format with two decimal places.
VARIABLES
The program needs to read a student's first name, last name, and five test scores.
Therefore, you need two variables to store the student's first name and last name, and
five variables to store the five test scores. To find the average, you must add the five
test scores and then divide the sum by
5
. Thus, you also need a variable to store the
average test score. Furthermore, because the input data is in a file and the output is to
be stored in a file, you must declare and initialize the appropriate variables. The
program needs at least the following variables:
double
test1, test2, test3, test4, test5;
//variables to store
//five test scores
double
average;
//variable to store average test score
String firstName;
//variable to store the first name
String lastName;
//variable to store the last name
Scanner inFile =
new
Scanner(
new
FileReader("test.txt"));
PrintWriter outFile =
new
PrintWriter("testavg.out");
Search WWH ::
Custom Search