Create a program that keeps track of specific information for Students. The info
ID: 3588498 • 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 LinkedList. Your students should be stored in an object called Student.You must create your own linked list and cannot use the LinkedList built into Java.
You should be able to add, display, sort (by any column you chose) and remove Students in the LinkedList.
Explanation / Answer
public class LinkedList {
static Node head; // head of list
static class Node {
Student student;
Node next;
Node(Student d) {
student = d;
next = null;
}
public Node() {
}
}
static class Student {
private String firstName;
private String lastName;
private String major;
private double gpa;
private int uin;
private int netId;
private int age;
private String gender;
public Student(String firstName, String lastName, String major, double gpa, int uin, int netId, int age,
String gender) {
this.firstName = firstName;
this.lastName = lastName;
this.major = major;
this.gpa = gpa;
this.uin = uin;
this.netId = netId;
this.age = age;
this.gender = gender;
}
}
/* This function prints contents of linked list starting from head */
public void printList() {
Node n = head;
while (n != null) {
System.out.println("FirstName: "+n.student.firstName + " " +" LastName: " +n.student.lastName +" Major: " +n.student.major + " GPA: " + n.student.gpa + " UIN: " + n.student.uin + " NetID: " + n.student.netId + " Age: " + n.student.age + " Gender: " + n.student.gender);
n = n.next;
}
}
/**
* Adds node at the end of linked list
*
* @param data
*/
public void add(Student student) {
/*
* 1. Allocate the Node & 2. Put in the data 3. Set next as null
*/
Node new_node = new Node(student);
/*
* 4. If the Linked List is empty, then make the new node as head
*/
if (head == null) {
head = new Node(student);
return;
}
/*
* 4. This new node is going to be the last node, so make next of it as
* null
*/
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
void delete(Student student) {
Node temp = head, prev = null;
// If head node itself holds the key to be deleted
if (temp != null && temp.student == student) {
head = temp.next; // Changed head
return;
}
while (temp != null && temp.student != student) {
prev = temp;
temp = temp.next;
}
if (temp == null)
return;
prev.next = temp.next;
}
public Node MergeSort(Node headOriginal) {
if (headOriginal == null || headOriginal.next == null)
return headOriginal;
Node a = headOriginal;
Node b = headOriginal.next;
while ((b != null) && (b.next != null)) {
headOriginal = headOriginal.next;
b = (b.next).next;
}
b = headOriginal.next;
headOriginal.next = null;
return merge(MergeSort(a), MergeSort(b));
}
public Node merge(Node a, Node b) {
Node temp = new Node();
Node head = temp;
Node c = head;
while ((a != null) && (b != null)) {
if (a.student.age <= b.student.age) {
c.next = a;
c = a;
a = a.next;
} else {
c.next = b;
c = b;
b = b.next;
}
}
c.next = (a == null) ? b : a;
return head.next;
}
public static void main(String[] args) {
/* Start with the empty list. */
LinkedList student = new LinkedList();
Student stud1 = new Student("suraj", "kale", "CSE", 8.5, 3456, 5050, 24, "Male");
Student stud2 = new Student("vinay", "kale", "Mech", 6.5, 2456, 1015, 26, "Male");
Student stud3 = new Student("ram", "jadhav", "Civil", 9.5, 8786, 9878, 25, "Male");
Student stud4 = new Student("rohit", "Kumar", "CSE", 7.0, 1325, 1050, 24, "Male");
Student stud5 = new Student("kiran", "jagtap", "ELEX", 6.5, 2198, 4412, 27, "Female");
Student stud6 = new Student("monika", "patil", "CSE", 2.5, 2145, 9897, 26, "Female");
Student stud7 = new Student("jayshree", "Kumar", "EXE", 7.5, 3245, 1155, 24, "Female");
Student stud8 = new Student("nishi", "Reddy", "TYP", 4.5, 2556, 3215, 24, "Female");
Student stud9 = new Student("sunita", "ambat", "MCD", 7.9, 3254, 325, 24, "Female");
Student stud10 = new Student("ishwar", "lanke", "CSE", 8.5, 2148, 263, 24, "Male");
student.add(stud1);
student.add(stud2);
student.add(studt3);
student.add(studt4);
student.add(stud5);
student.add(stud6);
student.add(stud7);
student.add(stud8);
student.add(stud9);
student.add(stud10);
System.out.println("Student linked list");
student.printList();
System.out.println("***********************************");
student.MergeSort(head);
System.out.println("Student linked list after sorting by age");
student.printList();
System.out.println("***********************************");
System.out.println("After Removed student from List");
student.delete(stud2);
student.delete(stud1);
student.printList();
}
}
%%%%%%%%%%%%%%%%
public class Students {
// instance variable
private String firstName;
private String lastName;
private String major;
private double gpa;
private String uin;
private String netID;
private int age;
private String gender;
public Students() {
}
public Students(String firstName, String lastName, String major, double gpa, String uin, String netID, int age,
String gender) {
this.firstName = firstName;
this.lastName = lastName;
this.major = major;
this.gpa = gpa;
this.uin = uin;
this.netID = netID;
this.age = age;
this.gender = gender;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getMajor() {
return major;
}
public double getGpa() {
return gpa;
}
public String getUin() {
return uin;
}
public String getNetID() {
return netID;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setMajor(String major) {
this.major = major;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public void setUin(String uin) {
this.uin = uin;
}
public void setNetID(String netID) {
this.netID = netID;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Output:
Student linked list
FirstName: suraj LastName: kale Major: CSE GPA: 8.5 UIN: 3456 NetID: 10115050 Age: 24 Gender: Male
FirstName: vinay LastName: kale Major: Mech GPA: 6.5 UIN: 2456 NetID: 1015 Age: 26 Gender: Male
FirstName: ram LastName: jadhav Major: Civil GPA: 9.5 UIN: 8786 NetID: 9878 Age: 25 Gender: Male
FirstName: rohit LastName: Kumar Major: CSE GPA: 7.0 UIN: 1325 NetID: 1050 Age: 24 Gender: Male
FirstName: kiran LastName: jagtap Major: ELEX GPA: 6.5 UIN: 2198 NetID: 4412 Age: 27 Gender: Female
FirstName: monika LastName: patil Major: CSE GPA: 2.5 UIN: 2145 NetID: 9897 Age: 26 Gender: Female
FirstName: jayshree LastName: Kumar Major: EXE GPA: 7.5 UIN: 3245 NetID: 1155 Age: 24 Gender: Female
FirstName: nishi LastName: Reddy Major: TYP GPA: 4.5 UIN: 2556 NetID: 3215 Age: 24 Gender: Female
FirstName: sunita LastName: ambat Major: MCD GPA: 7.9 UIN: 3254 NetID: 325 Age: 24 Gender: Female
FirstName: ishwar LastName: lanke Major: CSE GPA: 8.5 UIN: 2148 NetID: 263 Age: 24 Gender: Male
***********************************
Student linked list after sorting by age
FirstName: suraj LastName: kale Major: CSE GPA: 8.5 UIN: 3456 NetID: 10115050 Age: 24 Gender: Male
FirstName: rohit LastName: Kumar Major: CSE GPA: 7.0 UIN: 1325 NetID: 1050 Age: 24 Gender: Male
FirstName: jayshree LastName: Kumar Major: EXE GPA: 7.5 UIN: 3245 NetID: 1155 Age: 24 Gender: Female
FirstName: nishi LastName: Reddy Major: TYP GPA: 4.5 UIN: 2556 NetID: 3215 Age: 24 Gender: Female
FirstName: sunita LastName: ambat Major: MCD GPA: 7.9 UIN: 3254 NetID: 325 Age: 24 Gender: Female
FirstName: ishwar LastName: lanke Major: CSE GPA: 8.5 UIN: 2148 NetID: 263 Age: 24 Gender: Male
FirstName: ram LastName: jadhav Major: Civil GPA: 9.5 UIN: 8786 NetID: 9878 Age: 25 Gender: Male
FirstName: vinay LastName: kale Major: Mech GPA: 6.5 UIN: 2456 NetID: 1015 Age: 26 Gender: Male
FirstName: monika LastName: patil Major: CSE GPA: 2.5 UIN: 2145 NetID: 9897 Age: 26 Gender: Female
FirstName: kiran LastName: jagtap Major: ELEX GPA: 6.5 UIN: 2198 NetID: 4412 Age: 27 Gender: Female
***********************************
After Removed student from List
FirstName: rohit LastName: Kumar Major: CSE GPA: 7.0 UIN: 1325 NetID: 1050 Age: 24 Gender: Male
FirstName: jayshree LastName: Kumar Major: EXE GPA: 7.5 UIN: 3245 NetID: 1155 Age: 24 Gender: Female
FirstName: nishi LastName: Reddy Major: TYP GPA: 4.5 UIN: 2556 NetID: 3215 Age: 24 Gender: Female
FirstName: sunita LastName: ambat Major: MCD GPA: 7.9 UIN: 3254 NetID: 325 Age: 24 Gender: Female
FirstName: ishwar LastName: lanke Major: CSE GPA: 8.5 UIN: 2148 NetID: 263 Age: 24 Gender: Male
FirstName: ram LastName: jadhav Major: Civil GPA: 9.5 UIN: 8786 NetID: 9878 Age: 25 Gender: Male
FirstName: monika LastName: patil Major: CSE GPA: 2.5 UIN: 2145 NetID: 9897 Age: 26 Gender: Female
FirstName: kiran LastName: jagtap Major: ELEX GPA: 6.5 UIN: 2198 NetID: 4412 Age: 27 Gender: Female
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.