Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Create a program that keeps track of specific information for Students. The

ID: 3813538 • Letter: J

Question

JAVA

Create a program that keeps track of specific information for Students. The information stored should be the following:

First Name, Last Name, Major, GPA, UIN, NetID, Age, Gender,

For this simple program we will only need to store 10 students in an ArrayList. Your students should be stored in an object called Student.

You should be able to add, display, remove, save and load Students in the ArrayList.

You should submit for grading 2 files: Lab9Driver.java and Student.java

Hint: It’s MUCH easier to build your save method first, then cater your load method to how you’ve determined to save it.

Explanation / Answer

// Student.java class

import java.util.ArrayList;
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class Student {
   private String FirstName;
   private String LastName;
   private String Major;
   private String Gender;
   private double GPA;
   private int UIN;
   private int NetID;
   private int Age;
  
   public Student(String fname, String lname, String major, String gender, double gpa, int uin, int netid, int age) {
       FirstName = fname;
       LastName = lname;
       Major = major;
       Gender = gender;
       GPA = gpa;
       UIN = uin;
       NetID = netid;
       Age = age;
   }
  
   public void display()
   {
       System.out.println(this.FirstName+" "+this.LastName+" "+this.Major+" "+this.Gender+" "+this.GPA+" "+this.UIN+" "+this.NetID+" "+this.Age);
   }
  
   public String toString()
   {
       return this.FirstName+" "+this.LastName+" "+this.Major+" "+this.Gender+" "+this.GPA+" "+this.UIN+" "+this.NetID+" "+this.Age;
   }
  
   public static void saveList(ArrayList<Student> student, String filename) throws FileNotFoundException
   {
       PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
       for (Student stud : student)
       pw.println(stud.toString());
       pw.close();
   }  
  
   public static void loadList(ArrayList<Student> studentload, String filename) throws IOException
   {      
       String line="";   
       BufferedReader br = null;
       try {
           br = new BufferedReader(new FileReader(filename));
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
           while ((line = br.readLine()) != null) {
                   String suf[] = new String[10];
                   int i =0;

                   // we will parse words from a complete string and add to respective strings in new list //
                   String[] arr = line.split(" ");
                   for ( String ss : arr) {
                       if(ss!=" ")
                       {
                           suf[i] = ss; // add words from a full line in array of strings
                           System.out.println("String decoded is: "+suf[i]);
                       i++;
                       }
                       //Student back = new Student(suf[0],suf[1],suf[2],suf[3],Double.parseDouble(suf[4]),Integer.parseInt(suf[5]),Integer.parseInt(suf[6]), Integer.parseInt(suf[7]));
                       //studentload.add(back);
                       }
                   }
   }

   public static void studentInit() throws FileNotFoundException
   {
       String filename="E:\output.txt";
       try {
           FileWriter Writer = new FileWriter("output.txt");
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       Student s1 = new Student("Name1", "Surname1","Major1", "Male",    4,    1010, 1510, 24);
       Student s2 = new Student("Name2", "Surname2","Major2", "Female", 4.5,    1011, 1511, 21);
       Student s3 = new Student("Name3", "Surname3","Major3", "Male",    5,    1012, 1512, 23);
       Student s4 = new Student("Name4", "Surname4","Major4", "Male", 3.5,    1013, 1513, 21);
       Student s5 = new Student("Name5", "Surname5","Major5", "Female", 2.5,    1014, 1514, 22);
       Student s6 = new Student("Name6", "Surname6","Major6", "Male", 4,    1015, 1515, 23);
       Student s7 = new Student("Name7", "Surname7","Major7", "Male", 4.5,    1016, 1516, 21);
       Student s8 = new Student("Name8", "Surname8","Major8", "Male", 5,    1017, 1517, 20);
       Student s9 = new Student("Name9", "Surname9","Major9", "Female", 3.5,    1018, 1518, 19);
       Student s10 = new Student("Name10", "Surname10","Major10", "Male",3,    1019, 1519, 24);
       ArrayList<Student> student = new ArrayList<Student>();
       student.add(s1);
       student.add(s2);
       student.add(s3);
       student.add(s4);
       student.add(s5);
       student.add(s6);
       student.add(s7);
       student.add(s8);
       student.add(s9);
       student.add(s10);
      
       System.out.println("FirstName LastName Major GPA UIN NetID Age Gender");
       Iterator it = student.iterator();
       while(it.hasNext())
       {
           Student st = (Student)it.next();
           st.display();
       }
       student.remove(s10);
       System.out.println(" After removing 10th student ");
       it=student.iterator();
       while(it.hasNext())
       {
           Student st = (Student)it.next();
           st.display();
       }
       saveList(student, filename);
       System.out.println(" Loading from filename .... "+filename);
       ArrayList<Student> studentload = new ArrayList<Student>();
       try{
           loadList(studentload, filename);
       }
       catch(IOException e)
       {
           e.printStackTrace();
       }
   }
}

// LabDriver.java class

import java.io.FileNotFoundException;


public class LabDriver {

   public LabDriver() {
       // TODO Auto-generated constructor stub
   }

   public static void main(String args[])
   {
       try {
           Student.studentInit();
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
}