29. Code an application program that keeps track of student information at your college. Include
their names, identification numbers, and grade point averages in a fully encapsulated,
homogeneous sorted singly linked list structure. When launched, the user will be asked to input
the initial number of students and the initial data set. Once this is complete, the user will be
presented with the following menu: Enter: 1 to insert a new student\'s information, 2 to fetch and
output a student\'s information, 3 to delete a student\'s information, 4 to update a student\'s
information, 5 to output all the student information in sorted order, and 6 to exit the program.
The program should perform an unlimited number of operations until the user enters a 6 to exit
the program. If the user requests an operation on a node not in the structure, the program output
should be “node not in structure.” Otherwise, the message “operation complete” should be
output. in java
Solution
Dear Asker,
Following 3 classes consist the colution to this problem. This will give you an idea to approach
this problem:
//Student Class
package studentinfo;
public class Student implements Comparable {
private String name;
private String id;
private double gpa;
public Student(String id, String name, double gpa) {
this.name = name;
this.id = id;
this.gpa = gpa;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the gpa
*/
public double getGpa() {
return gpa;
}
/**
* @param gpa the gpa to set
*/
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public int compareTo(Student o) {
if (this.id.compareTo(o.id) < 0) {
return -1;
} else if (this.id.compareTo(o.id) > 0) {
return 1;
} else if (this.gpa < o.gpa) {
return -1;
} else if (this.gpa > o.gpa) {
return 1;
}
return 0;
}
public boolean equals(Student o) {
if (this.name == o.name && this.id == o.id && this.gpa == o.gpa) {
return true;
}
return false;
}
}
//Student container class
package studentinfo;
import java.util.Collections;
import java.util.LinkedList;
public class StudentContainer {
LinkedList studentList;
public StudentContainer() {
this.studentList = new LinkedList();
}
public boolean hasStudent(String id, String name, double gpa) {
return this.studentList.contains(new Student(id, name, gpa));
}
public boolean insertStudent(String id, String name, double gpa) {
if (!this.isValidString(id) || !this.isValidString(name) || !this.isValidGpa(gpa)) {
return false;
}
this.studentList.add(new Student(id, name, gpa));
return true;
}
public boolean deleteStudentById(String id) {
Student toRemove = null;
for (Student e : studentList) {
if (e.getId() == id) {
toRemove = e;
break;
}
}
if (toRemove != null) {
this.studentList.remove(toRemove);
return true;
}
return false;.
29. Code an application program that keeps track of student informat.pdf
1. 29. Code an application program that keeps track of student information at your college. Include
their names, identification numbers, and grade point averages in a fully encapsulated,
homogeneous sorted singly linked list structure. When launched, the user will be asked to input
the initial number of students and the initial data set. Once this is complete, the user will be
presented with the following menu: Enter: 1 to insert a new student's information, 2 to fetch and
output a student's information, 3 to delete a student's information, 4 to update a student's
information, 5 to output all the student information in sorted order, and 6 to exit the program.
The program should perform an unlimited number of operations until the user enters a 6 to exit
the program. If the user requests an operation on a node not in the structure, the program output
should be “node not in structure.” Otherwise, the message “operation complete” should be
output. in java
Solution
Dear Asker,
Following 3 classes consist the colution to this problem. This will give you an idea to approach
this problem:
//Student Class
package studentinfo;
public class Student implements Comparable {
private String name;
private String id;
private double gpa;
public Student(String id, String name, double gpa) {
this.name = name;
this.id = id;
this.gpa = gpa;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
2. * @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the gpa
*/
public double getGpa() {
return gpa;
}
/**
* @param gpa the gpa to set
*/
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public int compareTo(Student o) {
if (this.id.compareTo(o.id) < 0) {
return -1;
} else if (this.id.compareTo(o.id) > 0) {
return 1;
} else if (this.gpa < o.gpa) {
3. return -1;
} else if (this.gpa > o.gpa) {
return 1;
}
return 0;
}
public boolean equals(Student o) {
if (this.name == o.name && this.id == o.id && this.gpa == o.gpa) {
return true;
}
return false;
}
}
//Student container class
package studentinfo;
import java.util.Collections;
import java.util.LinkedList;
public class StudentContainer {
LinkedList studentList;
public StudentContainer() {
this.studentList = new LinkedList();
}
public boolean hasStudent(String id, String name, double gpa) {
return this.studentList.contains(new Student(id, name, gpa));
}
public boolean insertStudent(String id, String name, double gpa) {
if (!this.isValidString(id) || !this.isValidString(name) || !this.isValidGpa(gpa)) {
return false;
}
this.studentList.add(new Student(id, name, gpa));
return true;
}
public boolean deleteStudentById(String id) {
Student toRemove = null;
for (Student e : studentList) {
if (e.getId() == id) {
4. toRemove = e;
break;
}
}
if (toRemove != null) {
this.studentList.remove(toRemove);
return true;
}
return false;
}
public void printStudentById(String id) {
Student toRemove = this.getStudentById(id);
if (toRemove != null) {
this.printStudentInfo(toRemove);
} else {
System.out.println("No student node found");
}
}
public void updateStudentById(String id, String name, double gpa) {
Student s = this.getStudentById(id);
if (s != null) {
if (this.isValidGpa(gpa) && this.isValidString(name)) {
s.setGpa(gpa);
s.setName(name);
}
} else {
System.out.println("No student node found with id :" + id);
}
}
private boolean isValidGpa(double gpa) {
return gpa >= 0 && gpa <= 4;
}
private boolean isValidString(String s) {
return s != null && !"".equals(s);
}
private Student getStudentById(String id) {
5. for (Student e : studentList) {
if (e.getId().equals(id)) {
return e;
}
}
return null;
}
private void printStudentInfo(Student s) {
System.out.println("Student's info:"
+ " id " + s.getId()
+ " name " + s.getName()
+ " gpa " + s.getGpa());
}
public void printSortedList() {
if (!this.studentList.isEmpty()) {
Collections.sort(studentList);
studentList.forEach((e) -> {
this.printStudentInfo(e);
});
}
}
}
//StudentInfo class, containing the main method
package studentinfo;
import java.util.Scanner;
public class StudentInfo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
StudentContainer studentList = new StudentContainer();
int choice;
Scanner input = new Scanner(System.in);
do {
System.out.println("Please enter your choice (1-6): "
6. + "1 to insert a new student's information, "
+ "2 to fetch and output a student's information, "
+ "3 to delete a student's information, "
+ "4 to update a student's information, "
+ "5 to output all the student information in sorted order, "
+ "6 to exit the program. ");
choice = input.nextInt();
String id, name;
double gpa;
switch (choice) {
case 1:
System.out.println("Enter Student's info (id, name and grade, in that order, on different
lines):");
id = input.next().trim();
name = input.next().trim();
gpa = input.nextDouble();
studentList.insertStudent(id, name, gpa);
break;
case 2:
System.out.println("Enter student id:");
id = input.next().trim();
studentList.printStudentById(id);
break;
case 3:
System.out.println("Enter student id:");
id = input.next().trim();
studentList.deleteStudentById(id);
break;
case 4:
System.out.println("Enter student id, new name and new gpa in that order, in different lines:");
id = input.next().trim();
name = input.next().trim();
gpa = input.nextDouble();
studentList.updateStudentById(id, name, gpa);
break;
case 5: