Java

Assigning 500 Grades

12 points

Assign 500 grades? That would take forever! Not with the wonders of modern looping, it won't. It will take some time, however, to create the Java program to assign the grades.

The idea is to open the file names.txt (which you should download) and assign each person listed in the file a letter grade. On each row of the file is a person's first name, middle initial, last name, and marking period grade. For example, here is the first row of the file:
     "Robert A Lourenco 97"
The point of the program is to calculate a grade for this student and print out a row like this:
     "Robert A Lourenco 97 A".

import
You will need to import these classes: java.io.FileReader, java.io.FileWriter, java.util.StringTokenizer, java.io.BufferedReader, java.io.IOException and javax.swing.JOptionPane.

private instance variables
You will need:

CalculateGrades()
The constructor has a 'try - catch' structure, which is necessary in order to deal with problems or 'exceptions' that can come up while reading from or writing to a file. Exceptions are discussed in Chapter 14. Inside the 'try' section of the constuctor, create the object 'writer' and associate it with the output file you are going to create:
     writer = new FileWriter("grades.txt");

readFromFile()
Begin this method by creating the object 'reader' and associating it with the input file 'names.dat':
      reader = new FileReader(your path and input file);
Next you need to convert 'reader' to a 'BufferedReader' object, which is capable of reading an entire line of input:
     BufferedReader in = new BufferedReader(reader);

Recall that a StringTokenizer object is capable of splitting up a single line into several strings. Here is a slide with some information on the StringTokenizer. Begin by declaring a tokenizer:
     StringTokenizer tokenizer;
Also declare Strings 'line' and 'scoreStr', and int 'i'. Then set up a FOR loop that runs 500 times, once for each line of the input file 'names.dat'. Inside the loop, this code:
     line = in.readLine();
reads in 1 line of input and assigns it to 'line'. Pass the String 'line' to 'tokenizer':
     tokenizer = new StringTokenizer(line);
Then call 'tokenizer.nextToken()' 4 times to separate 'line' into 'firstName', 'middleInitial', 'lastName' and 'scoreStr'. For example:
     firstName = tokenizer.nextToken();

Use 'Integer.parseInt()' to convert the String 'scoreStr' to the integer 'score', as shown in this slide. The last thing to do inside the FOR loop is to call 'processAGrade()'.

Outside of the loop, close 'writer' and 'reader' by calling the '.close()' method on both objects.

processAGrade()
Inside the 'try' section of the 'try - catch' structure, write the student's first name, middle initial, last name and numerical grade (e.g. 79) to your output file. Use the 'writer.write()' syntax to do this. Then set up an IF - ELSE IF - ELSE structure to calculate a letter grade, based on the numerical grade. A grade 90 or better earns an 'A', 80 or better earns a 'B', 70 or better earns a 'C', 60 or better earns a 'D' and below a 60 earns an 'F'. After calculating the letter grade, print it to the output file, followed by '\n', which causes output to advance to the next line.

main()
'main' is simple: create a 'CalculateGrades' object with a call to 'new', just as you created 'writer' and 'reader' earlier. Then call the '.readFromFile()' method.

 

Starting Point

You can download CalculateGrades.java to use as a starting point in writing your program.