![]() |
Lab 12
|
In particular, we will read information about several students from a file -provided to us-, then we will filter these data according to some criteria -by lecture section, as an example- and finaly write the results back to a file.
You will develop this application mostly from scratch. There is very little
helping code prepared for you. However, do download the folder lab12_programs
from the download directory. You will find there a file containing some input data,
and a starting version for the WellesleyStudents.java
program.
Student s1 = new Student("Alexander", "Angie", "2011", "lec02", "lab03");
Student s2 = new Student("Alexander;Angie;2011;lec02;lab03;");
Notice the special delimiters (";") within the string argument above! To extract
the individual parts within such a string, you need to use the String instance
method split()
.
getFirstName()
, which returns the first name of the invoking Student instance
getLastName()
, which returns the last name of the invoking Student instance
getLab()
, which returns the lab section of the invoking Student instance
getLecture()
, which returns the lecture section of the invoking Student instance, and
getYear()
, which returns the year the invoking Student instance belongs to.
toString()
instance method, which returns
a String representation of the invoking object instance. This method is very useful for
testing purposes, and not only. We recommend that you implement the toString() method every
time you define a class!
For example, here is a printed representation of an instance of the Student class: Add a main() method to your Student.java file, so you can test your code as
you develop it. As always, you should test your code incrementally, as you
add it to the Student.java
file.
Student[] students; //array to hold the Student instances
int numRecords = 32; //number of objects it will hold
fromFileToArray()
, which you are asked to implement. Before you try
to implement this method, you may want to study the definition of the method
fromFileToScreen()
shown below.
fromFileToScreen()
It takes a file name as input, reads it line by line,
and outputs the lines in the console. It can be used for testing purposes and practice with
reading files in Java. The definition of this method is provided to you ("Read from a file" below).
Testing code is given in the main() method.
printArray()
A helper method that prints the input array to the console,
one object per line. It uses the toString()
on the Student objects.
Only the non-null objects in the input array are printed. See "Printing an Array" below. Testing code is given in the main() method.
fromFileToArray()
This instance method reads the data from the input file into an array, which is returned
by the method. This is the signature of this method: getlabGroup()
This instance method takes a lab section (String) as input, and selects the Student records that
match the input. It saves the results in an array, which is returned at the end.
This is the header of this method: fromArrayToFile()
This instance method takes an array of Student objects, and the name of a file, as inputs.
It writes the array data into the file, one object per line. If the array contains null objects,
these objects should not be written into the file. This is the header of the method:getyearGroup()
This instance method takes the year (String) as input and returns an array with all the
student instances that match the input. This is the header of this method: getyearAndLabGroup()
This instance method takes the year and the lab section (both as Strings) as input and returns an array with all the
student instances that match both of the inputs. This is the header of this method: fromArrayToFileLineNum()
This instance method works like the method fromArrayToFile()
above, however it adds line numbers to each line in the file. Here is a sample of what the contents of the file will look like:
/* line 0*/ Kathy Chen year 2011 is in lec01 and lab01
/* line 1*/ Miquel Geller year 2010 is in lec01 and lab01
/* line 2*/ Olivia Linder year 2010 is in lec01 and lab01
/* line 3*/ Elizabeth Park year 2011 is in lec01 and lab01
fromFileToScreen()
is given to you below. Study this definition,
to refresh yourself on some of the details on how to read a file in Java.
public void fromFileToScreen(String inFile) {
try {
BufferedReader reader = new BufferedReader(new FileReader(inFile));
String line = reader.readLine(); //read the first line of the file
while (line != null) { //line becomes null at end of file
System.out.println(line);
line = reader.readLine(); //read the next line of the file
}
reader.close();
} catch (IOException e) {
System.out.println(e);
}
}
public void printArray(Student[] a) {
for (int i=0; i < a.length; i++) {
if (a[i] != null)
System.out.println(a[i]); //will be using the toString() on Student
}
}