Create a program that keeps track of specific information for Students. The info
ID: 3854430 • Letter: C
Question
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: Lab6Driver.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.
Answer should be in java
Load from user.
Explanation / Answer
import java.io.*;
import java.util.ArrayList;
class Student implements Serializable
{
String fname, lname, gender, major;
int netid, age;
double gpa, uin;
Student(String f, String l,String gender,String m, int id, int a, double g, double u)
{
fname=f; lname=l; this.gender=gender; major=m;
netid= id; age=a;
gpa=g; uin=u;
}
public String toString()
{
return fname+" "+lname+" "+major+" Age "+age+", Gender "+gender+" GPA: "+gpa+", NetId: "+ netid+", Uin: "+uin+" ";
}
}
public class StudentDriver
{
public static void addStudent(ArrayList ar, Student s)
{
ar.add(s);
}
public static void display(ArrayList arr)
{
for(int i=0;i<arr.size();i++)
{
System.out.println(arr.get(i));
}
}
public static boolean remove(ArrayList arr, int netid)
{
Student s;
for(int i=0;i<arr.size();i++)
{
s=(Student)arr.get(i);
if(s.netid==netid)
{
arr.remove(i);
return true;
}
}
return false;
}
public static void save(ArrayList arr)
{
try{
FileOutputStream fos= new FileOutputStream("myfile");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(arr);
oos.close();
fos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public static ArrayList load()
{
ArrayList<Student> arr= new ArrayList<Student>();;
try{
FileInputStream fos= new FileInputStream("myfile");
ObjectInputStream oos= new ObjectInputStream(fos);
arr=(ArrayList)oos.readObject();
oos.close();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
return arr;
}
public static void main(String s[])
{
ArrayList<Student> arr = new ArrayList<Student>();
addStudent(arr, new Student("Asif","Khan","Male","Major", 1, 20, 99.7, 90));
addStudent(arr, new Student("Iqbal","Khan","Male","Major2", 2, 20, 99.7, 90));
addStudent(arr, new Student("Junaid","Sheikh","Male","Major", 3, 20, 99.7, 90));
addStudent(arr, new Student("naseer","baba","Male","Major", 4, 20, 99.7, 90));
System.out.println("***************Students**************** ");
display(arr);
save(arr);
System.out.println("***************Saving Students**************** ");
remove(arr,2);
display(arr);
System.out.println("***************Loading Students**************** ");
arr=load();
display(arr);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.