8.3 Programming ReadWrite - Write in Java and include comments Write an applicat
ID: 3920266 • Letter: 8
Question
8.3 Programming ReadWrite - Write in Java and include comments
Write an application which will write five student ID numbers and GPAs to a “rw” file called “Stu.dat” and then allow you to display the GPA of any student upon entering their ID number, for any number of students.
Directions
Import the classes necessary to support your application.
Create a class called ReadWrite. This class has no properties or behaviors.
Create a main method which will include the following:
Add an appropriate throws statement in the main method.
Create a reference to a text file called “Stu.dat” with “rw” access.
Include try and catch blocks for exception handling.
Use a loop to interactively assign student ID numbers and their GPA scores.
With the use of a second loop, display the GPA for specific student ID numbers.
Use a sentinel to determine when you wish to stop the program.
Include a finally block within your program.
Note: User defined methods are not required but may be used if you prefer.
Grading Rubric
Task
Points
Throws clause added in main method
0.25
Create a reference to the RandomAccessFile class
1
Include try and catch blocks for exception handling
1
Use a loop to interactively assign student ID numbers and their GPA scores
1
Use of a second loop to display the GPA for specific student ID numbers
1
Use of a sentinel to determine when you wish to stop the program
0.25
Include a finally block within your program
0.5
Proper documentation
1
Program works effectively
1
Total
7
Task
Points
Throws clause added in main method
0.25
Create a reference to the RandomAccessFile class
1
Include try and catch blocks for exception handling
1
Use a loop to interactively assign student ID numbers and their GPA scores
1
Use of a second loop to display the GPA for specific student ID numbers
1
Use of a sentinel to determine when you wish to stop the program
0.25
Include a finally block within your program
0.5
Proper documentation
1
Program works effectively
1
Total
7
Explanation / Answer
Program:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;
public class ReadWrite {
public static void main(String[] args) throws IOException {
HashMap<Integer, Float> hm = new HashMap();
File extractFile = new File("Student.dat");
FileWriter fileWriter = new FileWriter(extractFile, true);
PrintWriter printWriter = new PrintWriter(
new BufferedWriter(fileWriter));
// to accept all inputs for StudentID and GPA Score
try{
for(int i=0; i<5; i++){
Scanner in = new Scanner(System.in);
System.out.println("Enter a StudentID for Student " + i );
int studenID = in.nextInt();
System.out.println("You entered StudentID " + studenID);
System.out.println("Enter a GPA score of Student " + i );
float GPAScore = in.nextFloat();
System.out.println("You entered GPA score "+ GPAScore);
hm.put(studenID, GPAScore);
printWriter.println("StudentID : " + studenID +" GPAScore : " + GPAScore );
}
} finally {
// Make sure to close the file when done
printWriter.close();
}
// to display Values From studen.dat File
InputStream is = new FileInputStream("Student.dat");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String Line=null;
while((Line = br.readLine()) != null)
{
// prints line
System.out.println(Line);
}
}
}
Output:
Enter a StudentID for Student 0
1
You entered StudentID 1
Enter a GPA score of Student 0
1.1
You entered GPA score 1.1
Enter a StudentID for Student 1
2
You entered StudentID 2
Enter a GPA score of Student 1
2.2
You entered GPA score 2.2
Enter a StudentID for Student 2
3
You entered StudentID 3
Enter a GPA score of Student 2
3.3
You entered GPA score 3.3
Enter a StudentID for Student 3
4
You entered StudentID 4
Enter a GPA score of Student 3
4.4
You entered GPA score 4.4
Enter a StudentID for Student 4
5
You entered StudentID 5
Enter a GPA score of Student 4
5.5
You entered GPA score 5.5
StudentID : 1 GPAScore : 1.1
StudentID : 2 GPAScore : 2.2
StudentID : 3 GPAScore : 3.3
StudentID : 4 GPAScore : 4.4
StudentID : 5 GPAScore : 5.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.