0000 Verizon 10:56 AM a bbprod.hwcc.edu In this project, you will input student data from a file – create student objects add student objects to an array – write the student object data to standard output Download the input text file studentinput3.txt. This file contains four sets of data. Each set of data includes four lines that correspond to one student’s data. The first line is the student’s first and last name, the second line is the student’s id number, the third is the student’s year and the fourth contains the student’s major Modify your RunStudent program from project 2 to create a Student array, read the sets of data, create a student object, add the object to the array, and print the array. There are many different ways to do this, but we will do i in the most basic way The first question is: do we know how many sets of data are in the file? If we do, and if this number is not likely to change, a for loop and an array are fine to use. An array has a set size that cannot be changed. So, if we know we will only have 10 students, we can create a Student array of size 10 and use a for loop to traverse the input file. If we have a file, but do not know how many students are contained in the file, an array might not be our best bet. An ArrayList has room for 10 elements by default, but can grow and shrink. An ArrayList has methods to add elements that do the work for us if the list gets too large for the current size. But deep down inside, an ArrayList contains an array. For this project, we know that we will only have four students, so we will create an array of size 4. Since we have only four students, this means we will have four sets of data. This makes reading from the file casy: a for loop that executes four times and reads the four lines of data during each loop iteration. Reading the data from the file can be tricky It would be best to get this part of the program working first before you proceed to the rest of the program. It is always important to make sure the data structures appropriately contain the data. The last step will be to create a method in the RunStudent class that writes the content of the array to standard output.
Expert Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author Sam
*/
public class Student {
private String name;
private String idNum;
private String year;
private String major;
public Student(String name, String idNum, String year, String major) {
this.name = name;
this.idNum = idNum;
this.year = year;
this.major = major;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public String getYear() {
return year;
}
public String getMajor() {
return major;
}
@Override
public String toString() {
return “Student name: ” + name + “nID Number: ” + idNum
+ “nYear: ” + year + “nMajor: ” + major + ‘}’;
}
}
class RunStudent {
public static void main(String[] args) throws IOException{
String name;
String idNum;
String year;
String major;
int i = 0; //index
BufferedReader br = new BufferedReader(new FileReader(“studentInput3.txt”));
Student[] students = new Student[4];
while ((name = br.readLine())!= null) {
idNum = br.readLine();
year = br.readLine();
major = br.readLine();
if (year.equalsIgnoreCase(“F”))
year = “Freshman”;
else if (year.equalsIgnoreCase(“O”))
year = “Sophomore”;
else if (year.equalsIgnoreCase(“S”))
year = “Senior”;
else if (year.equalsIgnoreCase(“J”))
year = “Junior”;
students[i++] = new Student(name, idNum, year, major);
}
br.close();
printStudentArray(students);
}
public static void printStudentArray(Student[] a) {
for (Student s:a)
System.out.println(“n———————————–n”
+ s.toString()
+ “n———————————–n”);
}
}
This code is sufficent for