This assignment explores reading and writing binary files Create two simple asci
ID: 3839245 • Letter: T
Question
This assignment explores reading and writing binary files
Create two simple ascii text files. One text file will store first names, the other file will store last names. There should be a minimum of 20 names in each file. The names should be randomly created and stored in the files. The files should have the same number of names. Do not sort the names in the files. The length of the names should be 15 characters or less and may contain spaces. These names will be stored in character arrays in
Create a class named Student
The class should have 4 fields: first name, last name, student number and GPA
The fields for the first and last names should be character arrays of size 16
You may create multiple overloaded constructors but you must create a constructor that takes 4 parameters: student number, first name, last name, and GPA
Create appropriate set and get functions
Create a function named getStudentRecord that will return a formatted string containing the state of a Student object. The string returned by the function should
not contain any newline characters (so that the record can be displayed on a single line).
You will need to create functions (non-member functions) that generate random numbers (C++11 standard random number generation) for a GPA and a student number. GPA's should be in the range of 1.00 - 4.00 (data type double). Student numbers should be in the range of 1000 - 4000 (data type int) with no duplicates!
Create an empty vector that you will use to store Student objects.
In a loop:
Create a minimum of 20 Student objects by reading a first name and a last name from your text files and using your functions to create a student number and a
GPA.
Store the Student object in your vector.
After you have loaded the vector with Student objects write the objects to a BINARY file.
Open the binary file of Student objects and in a loop:
read a Student object
display it to the screen using an output statement. Each student record should be display on a single line
Explanation / Answer
void printit(List prnt, File outputFile) throws IOException { byte[] outputBytes = new byte[prnt.size()]; int i = 0; for (Integer element : prnt) outputBytes[i++] = (byte)element; FileOutputStream outputStream =new FileOutputStream(outputFile); outputStream.write(b); outputStream.close(); java.nio.file.Files.write (outputFile.toPath(), outputBytes); } void getfyle(File inputFile) throws IOException { FileInputStream inputStream = new FileInputStream(inputFile); byte[] inputBytes = new byte[(int)inputFile.length()]; inputStream.read(inputBytes); inputStream.close(); byte[] inputBytes = java.nio.file.Files.readAllBytes (inputFile.toPath()); for (byte b: inputBytes) System.out.println (b&0xFF); ArrayList list = new ArrayList(inputBytes.length); for (byte b: inputBytes) list.add (b&0xFF); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.