In: Computer Science
Problem 3:
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:
Quizzes
Midterm Exam
Final Exam
please solve it as clear and simple as you can - java netbeans
import java.util.Comparator; import java.util.LinkedList; public class StudentLinkedList { public static void printStudentList(LinkedList<Student> ll) { for(int i=0; i<ll.size(); i++) { System.out.println(ll.get(i)); } System.out.println(); } public static void deleteStudentByID(LinkedList<Student> ll, long id) { for(int i=0; i<ll.size(); i++) { if(ll.get(i).getID() == id) { ll.remove(i); System.out.println("Deleted successfully."); return; } } System.out.println("Unable to find the student with id " + id); } public static void sortListByID(LinkedList<Student> ll) { ll.sort(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return (int) (o1.getID() - o2.getID()); } }); } public static double findMarksAverage(LinkedList<Student> ll) { double totalMarks = 0; for(int i=0; i<ll.size(); i++) { int marks[] = ll.get(i).getMarks(); totalMarks += marks[0] + marks[1] + marks[2]; } return totalMarks/ll.size(); } public static double findMinMark(LinkedList<Student> ll, int markIndex) { int minMark = 100; for(int i=0; i<ll.size(); i++) { int marks[] = ll.get(i).getMarks(); if(marks[markIndex] < minMark) { minMark = markIndex; minMark = marks[markIndex]; } } return minMark; } public static void main(String[] args) { LinkedList<Student> 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); printStudentList(linkedlist); deleteStudentByID(linkedlist, 20121024); printStudentList(linkedlist); sortListByID(linkedlist); printStudentList(linkedlist); System.out.println("Average marks by all: " + findMarksAverage(linkedlist)); System.out.println("Min Quizzes marks by student: " + findMinMark(linkedlist, 0)); System.out.println("Min Midterm marks by student: " + findMinMark(linkedlist, 0)); System.out.println("Min Final exam marks by student: " + findMinMark(linkedlist, 0)); } } 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] = quizzes; 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; } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.