In: Computer Science
Modify StudentLinkedList class by adding the following methods:
printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.
deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.
sortListByID(): sort the linkedlist according to students IDs.
findMarksAverage(): find the average of all marks for all students in the list.
findMinMark(int markIndex): find the student with the minimum mark in a specific index:
o 0: Quizzes
o 1: Midterm Exam
o 2: Final Exam
import java.util.LinkedList;
public class StudentLinkedList {
public static void main(String[] args) {
LinkedList linkedlist = new LinkedList();
linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38));
linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35));
linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40));
linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30));
linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32));
linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39));
linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30));
linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30));
linkedlist.removeFirst(); linkedlist.removeLast();
linkedlist.add(0, new Student("John Don", 20131025, 11, 31, 31));
linkedlist.remove(2);
}
class Student{
private String name;
private Long ID;
private int [] marks = new int [3];
public Student(String name, long ID, int quizzes, int mid, int fin) {
this.name = name; this.ID = ID;
marks[0] = quizes;
marks[1] = mid;
marks[2] = fin;
}
public String getName() {
return name;
}
public Long getID() {
return ID;
}
public int[] getMarks() {
return marks;
}
@Override public String toString() { String temp = "student: " + "name = " + name + ", ID = " + ID + ", marks = {" + marks[0] + ", " + marks[1] + ", " + marks[2] + "}"; return temp; }
}
Student.java
class Student{
// instance variables
private String name;
private Long ID;
private int [] marks = new int [3];
// parameterized constructor
public Student(String name, long ID, int quizzes, int
mid, int fin) {
this.name = name;
this.ID = ID;
marks[0] = quizzes;
marks[1] = mid;
marks[2] = fin;
}
// getter methods
public String getName() {
return name;
}
public Long getID() {
return ID;
}
public int[] getMarks() {
return marks;
}
@Override
public String toString() {
String temp = "student: " + "name =
" + name
+ ", ID = " + ID
+ ", marks = {" + marks[0] + ", " + marks[1] +
", " + marks[2] + "}";
return temp;
}
}
StudentLinkedList.java
import java.util.LinkedList;
public class StudentLinkedList {
// method to print students list
public static void
printStudentList(LinkedList<Student> list) {
for (Student student : list)
System.out.println(student);
}
// method to delete the student from list by using
id
public static void
deleteStudentByID(LinkedList<Student> list,long id) {
for (Student student : list)
if(student.getID()==id)
list.remove(student);
System.out.println("Student with
"+id+" ID deleted successfully");
}
// method to find average marks of every student
public static void
findMarksAverage(LinkedList<Student> list) {
for (Student student : list)
{
int
total=0;
for (int marks :
student.getMarks())
total+=marks;
System.out.println("Average marks of student "+student.getName()+":
"+(total/3));
}
}
// method to find the minimum marks in specified
exam
public static void
findMinMark(LinkedList<Student> list,int markIndex) {
int minimim=0;
for (Student student : list)
{
int[]
temp=student.getMarks();
if(temp[markIndex]<minimim)
minimim=temp[markIndex];
}
if(markIndex==0)
System.out.println("Minimum marks in quizzes: "+minimim);
else
if(markIndex==1)
System.out.println("Minimum marks in midterm exam:
"+minimim);
else
System.out.println("Minimum marks in final exam: "+minimim);
}
public static void main(String[] args) {
// create LinkedList with Student
types
LinkedList<Student>
linkedlist = new LinkedList<Student>();
// add student objects into
list
linkedlist.add(new Student("Ahmed
Ali", 20111021, 18, 38, 38));
linkedlist.add(new Student("Sami
Kamal", 20121021, 17, 39, 35));
linkedlist.add(new Student("Salem
Salim", 20131021, 20, 40, 40));
linkedlist.add(new Student("Rami
Mohammed", 20111031, 15, 35, 30));
linkedlist.add(new Student("Kim
Joe", 20121024, 12, 32, 32));
linkedlist.addFirst(new
Student("Hadi Ali", 20111025, 19, 38, 39));
linkedlist.addLast(new
Student("Waleed Salim", 20131025, 10, 30, 30));
linkedlist.set(0, new
Student("Khalid Ali", 20111027, 15, 30, 30));
linkedlist.add(0, new Student("John
Don", 20131025, 11, 31, 31));
printStudentList(linkedlist); //
print student list
System.out.println(); // new
line
deleteStudentByID(linkedlist,
20121024); // delete student whose id is 20121024
System.out.println();
findMarksAverage(linkedlist); //
find average marks
System.out.println();
findMinMark(linkedlist, 0); // find
minimum marks in quizzes
System.out.println();
findMinMark(linkedlist, 1); // find
minimum marks in mid term exam
System.out.println();
findMinMark(linkedlist, 2); // find
minimum marks in final exam
System.out.println();
printStudentList(linkedlist); //
print student list
}
}
Output