You are on page 1of 3

(This program is intended for you to use basic knowledge of Java operations to perform a real world function.

Comments can and should be used for clarification.) Write a Java program that can be used by a teacher to keep track of student grades. Part 1: Display the student's name, current average, and each grade from the information stored in text file "students.txt". This output should be displayed in a format that makes sense to the reader (readable, informative). This output should be to the console. Part 2: Do part 1, and then ask the user if they want to add grades for a new exam. If so: - Add a new exam grade to each student. The program should prompt the user to manually enter a grade for each student one at a time. - Save the new grade to the end of each student's grades in the text file "students.txt" (in other words, overwrite the file) - Then display the file again (as part 1). Remember to close the file for writing before opening it for reading. Remember to close the file for reading before opening it for writing. == Input == For ease of testing, there will be at most 10 students. For ease of storage, each student will have at most 20 grades. There will be no newline character at the end of the file. Sample input (as file):

Shameeza 60 76 68 89 78 Gidon 59 77 97 89 65 Oni 96 89 65 78 90 Marisa 87 90 56 77 56 Yaffa 67 75 57 88 67 Yan 50 67 56 65 67 Arah 59 98 65 54 67 Ethan 66 87 65 89 60 SuHyun 98 76 56 89 69 Jean 68 90 76 87 99

== Required knowledge == File input: Use Scanner.


Scanner filein = new Scanner(new File(filename)); while( filein.hasNextLine() ){ //do something with filein.nextLine(); } filein.close();

File output: Use PrintWriter or PrintStream (both work mostly the same). Note: System.out is a PrintStream, so you can use it similarly.
PrintStream fileout = new PrintStream(filename); fileout.println("Hello World!"); fileout.close();

== Program submission == The name of your file must be Assignment2.java (with upper/lower

case exact) Include your name in a comment in the first line of the program. == Hints == Hint: It may be helpful to make 3 methods, one to calculate the average and one for each option. Following part 2, simply call part 1. Hint: To overwrite a file that you had just used for input, apply the infile object to the OutputStream. Hint: Before testing the overwriting process, make sure that you have saved the original .txt input file so you can try again if an error occurs. Hint: It may be helpful to create 2 arrays for each line taken from input, one to hold Strings, where each element holds one word (name), and one to hold int, where the Strings that represent grades are parsed to int values. The second array may be used to calculate average. Hint: There is a wrapper class for arrays as well. For example, we may use Arrays.toString(arr); to print an entire array named arr directly.

You might also like