OK been messing with this and I am in a bit of a pickle here. Below are the JAVA
ID: 3831365 • Letter: O
Question
OK been messing with this and I am in a bit of a pickle here. Below are the JAVA codes I have and I am honestly frustrated. The majority of everything is there but I need someone to spruce these up so they actually work as the directions say. Please help I need this to function by this evening! See Directions Below
==================================================================
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.
======================
This should have student class and required methods: but it only prints a sorted list, does not allow the user to adjust anything.
=====================
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(n.student.firstName + " " + n.student.lastName + " " + n.student.age);
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;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change temp.next
while (temp != null && temp.student != student) {
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if (temp == null)
return;
// Unlink the node from linked list
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 student1 = new Student("Ram", "Kumar", "CSE", 7.5, 345666, 10115050, 24, "Male");
Student student2 = new Student("Great", "Danton", "CSE", 6.5, 245666, 10105050, 22, "Male");
Student student3 = new Student("Casey", "Neistat", "ECE", 9.5, 878623, 9873282, 25, "Male");
Student student4 = new Student("Rajesh", "Kumar", "CSE", 7.0, 1325, 115050, 24, "Male");
Student student5 = new Student("Stacey", "Glenn", "ELEX", 7.5, 2198132, 214412, 22, "Female");
Student student6 = new Student("Mary", "Stephens", "CSE", 2.5, 21424, 987897, 23, "Female");
Student student7 = new Student("Gray", "Kumar", "EXE", 7.5, 325, 10115050, 24, "Male");
Student student8 = new Student("Chetan", "Reddy", "TYP", 4.5, 255666, 3215, 24, "Female");
Student student9 = new Student("Sample", "Kumar", "MCD", 7.9, 325, 325, 24, "Male");
Student student10 = new Student("Boom", "Kumar", "CSE", 8.5, 2148, 263, 24, "Female");
student.add(student1);
student.add(student2);
student.add(student3);
student.add(student4);
student.add(student5);
student.add(student6);
student.add(student7);
student.add(student8);
student.add(student9);
student.add(student10);
System.out.println("Student linked list before sorting");
student.printList();
System.out.println("***********************************");
student.MergeSort(head);
System.out.println("Student linked list after sorting by age");
student.printList();
System.out.println("***********************************");
student.delete(student2);
student.delete(student1);
student.printList();
}
}
======================
Student Class defined completely
======================
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;
}
Explanation / Answer
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.