JAVA Create a program that keeps track of specific information for Students. The
ID: 3828725 • 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 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Objects;
/**
*
* @author Sam
*/
public class StudentList {
static Student head;
public static void main(String[] args) throws IOException {
head = null;
String firstName, lastName, major, gpa, uin, netID, age, gender;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i=0; i<3; i++) {
System.out.println("Enter firstName, lastName, major, gpa, uin, netID, age, gender");
firstName = br.readLine();
lastName = br.readLine();
major = br.readLine();
gpa = br.readLine();
uin = br.readLine();
netID = br.readLine();
age = br.readLine();
gender = br.readLine();
head = new Student(firstName, lastName, major, gpa, uin, netID, age, gender, head);
}
sortFirstName();
display();
System.out.println("===============================================================================");
sortLastName();
display();
System.out.println("===============================================================================");
sortAge();
display();
System.out.println("===============================================================================");
sortGPA();
display();
System.out.println("===============================================================================");
sortGender();
display();
System.out.println("===============================================================================");
sortMajor();
display();
System.out.println("===============================================================================");
sortNetID();
display();
System.out.println("===============================================================================");
sortUIN();
display();
System.out.println("===============================================================================");
}
private static void display(){ //to display the list
Student temp = head;
while (temp!=null){
System.out.println(temp);
temp = temp.getNext();
}
}
private static void remove(Student student){ //to remove a node from the list
if (student.equals(head)){
head = head.getNext();
return;
}
Student temp = head;
while (temp.getNext()!=null){
if (student.equals(temp.getNext())){
temp.setNext(temp.getNext().getNext());
return;
}
temp = temp.getNext();
}
}
private static void sortFirstName() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) { //while the list is not empty
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareFirstName(max) > 0)
max = temp;
temp = temp.getNext();
} //till here we mark the node with max value
remove(max); // we remove the max item from the actual list and add it to another list
max.setNext(tempHead);
tempHead = max;
}
head = tempHead; //we return the second list
}
private static void sortLastName() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareLastName(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
private static void sortAge() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareAge(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
private static void sortGPA() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareGPA(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
private static void sortGender() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareGender(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
private static void sortMajor() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareMajor(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
private static void sortNetID() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareNetID(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
private static void sortUIN() {
Student tempHead = null;
Student temp;
Student max;
while (head != null) {
temp = head.getNext();
max = head;
while (temp!=null) {
if (temp.compareUIN(max) > 0)
max = temp;
temp = temp.getNext();
}
remove(max);
max.setNext(tempHead);
tempHead = max;
}
head = tempHead;
}
}
class Student {
private String firstName, lastName, major, gpa, uin, netID, age, gender;
private Student next;
public Student(String firstName, String lastName, String major, String gpa, String uin, String netID, String age, String gender, Student next) {
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.next = next;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getMajor() {
return major;
}
public String getGpa() {
return gpa;
}
public String getUin() {
return uin;
}
public String getNetID() {
return netID;
}
public String getAge() {
return age;
}
public String getGender() {
return gender;
}
public Student getNext() {
return next;
}
public void setNext(Student next) {
this.next = next;
}
int compareFirstName(Student other){ //simply compare anoher first name
return firstName.compareTo(other.firstName);
}
int compareLastName(Student other){
return lastName.compareTo(other.lastName);
}
int compareMajor(Student other){
return major.compareTo(other.major);
}
int compareGPA(Student other){
return gpa.compareTo(other.gpa);
}
int compareUIN(Student other){
return uin.compareTo(other.uin);
}
int compareNetID(Student other){
return netID.compareTo(other.netID);
}
int compareAge(Student other){
return age.compareTo(other.age);
}
int compareGender(Student other){
return gender.compareTo(other.gender);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Student other = (Student) obj;
if (!Objects.equals(this.firstName, other.firstName)) {
return false;
}
if (!Objects.equals(this.lastName, other.lastName)) {
return false;
}
if (!Objects.equals(this.major, other.major)) {
return false;
}
if (!Objects.equals(this.gpa, other.gpa)) {
return false;
}
if (!Objects.equals(this.uin, other.uin)) {
return false;
}
if (!Objects.equals(this.netID, other.netID)) {
return false;
}
if (!Objects.equals(this.age, other.age)) {
return false;
}
if (!Objects.equals(this.gender, other.gender)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Student{" + "firstName=" + firstName + ", lastName=" + lastName + ", major=" + major + ", gpa=" + gpa + ", uin=" + uin + ", netID=" + netID + ", age=" + age + ", gender=" + gender + '}';
}
}
370 lines of code!! aah. I commented few methods. Other are almost similar. I hope you will appreciate the code. The code is actually long since i tried to k,eep things simple and havnt used arrays. if incase you need any clarification on this pelase comment below.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.