Java Object Orientated Programming (I/O Stream) Marks.txt file located here -> h
ID: 3713214 • Letter: J
Question
Java Object Orientated Programming (I/O Stream)
Marks.txt file located here -> http://www.mediafire.com/file/1avo33p9whjz9j2/Marks.txt
"Notice that the first two lines are headers. The first line contains the label for eachassignment/exam, while the second contains the highest possible marks for each
assignment/exam. Each line to follow indicates a student, showing its student id, mark values for 4 assignments, and mark values for midterm and final exams. The data is separated by spaces (you have to consider that some of the information in the file might be missing). Also, some values might be listed as "n/a" meaning not available. Your task is to read in this file into
appropriate objects as described below.
Create a class called Student . The Student class should store a student keeping track of his/her student id, 4 assignments, midterm and final exams. Student ID should be an int and all the mark values should be of type float.
In the Student class, create a static method called loadFrom(BufferedReader aFile) which creates and returns a new Student object which will be read in from a line of the Marks.txt file. This method should assume that the file has been opened and should not close the file afterwards. It should also read only 1 line of the file. You will need to figure out how to parsethe line from the file to extract all the data. (hint: look at the Java API documentation under the String and StringTokenizer classes for appropriate methods). Be careful... remember that
some of the items are blank. You may assume (as in the given mark file) that ALL students have a student id. Create helper methods to simplify your steps logically. Make sure to test your
methods thoroughly."
Explanation / Answer
public class Student {
int ID;
float a1, a2, a3, a4, mid, final;
Student(int ID, float a1, float a2, float a3, float a4, float mid, float final){
ID =id;
a1 = a1;
a2 = a2;
a3 = a3;
a4 = a4;
mid = mid;
final = final;
}
Student static loadFrom(BufferedReader aFile) {
Object[] arr = new Object[6];
int i = 0;
String line = aFile.readLine();
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreToken()) {
String token = st.nextToken();
arr[i++] = token;
}
Student s = new Student((Integer)arr[0], (Float)arr[1], (Float)arr[2], (Float)arr[3], (Float)arr[4], (Float)arr[5] );
return s;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.