Question: IN JAVA, PLEASE WRITE A CODE USING ECLIPSE AND THE FOLLOWING CODE BELOW, Also please show the output Write a program that maintains the top

IN JAVA, PLEASE WRITE A CODE USING ECLIPSE AND THE FOLLOWING CODE BELOW, Also please show the output Write a program that maintains the top 15 scores for the students of a class that has more than 15 students , implementing the add and remove methods using a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the student at index i. class DoublyNode {
Student data;
DoublyNode next;
DoublyNode prev;
public DoublyNode(Student data){
this.data = data;
this.next = null;
this.prev = null;
}
}
class TopScoresDoublyList {
private DoublyNode head;
private int size;
public TopScoresDoublyList(){
this.head = null;
this.size =0;
}
public void add(Student student){
DoublyNode newNode = new DoublyNode(student);
if (head == null || student.score > head.data.score){
newNode.next = head;
if (head != null){
head.prev = newNode;
}
head = newNode;
} else {
DoublyNode current = head;
while (current.next != null && student.score <= current.next.data.score){
current = current.next;
}
newNode.next = current.next;
newNode.prev = current;
if (current.next != null){
current.next.prev = newNode;
}
current.next = newNode;
}
size++;
// Keep only the top 15 scores
if (size >15){
remove(15);
}
}
public void remove(int index){
if (index <=0|| index > size){
System.out.println("Invalid index to remove.");
return;
}
DoublyNode current = head;
for (int i =1; i < index; i++){
current = current.next;
}
if (current.prev != null){
current.prev.next = current.next;
} else {
head = current.next;
}
if (current.next != null){
current.next.prev = current.prev;
}
size--;
}
public void display(){
DoublyNode current = head;
while (current != null){
System.out.println(current.data.name +": "+ current.data.score);
current = current.next;
}
}
}
public class Main {
public static void main(String[] args){
TopScoresDoublyList scoresList = new TopScoresDoublyList();
// Adding students
scoresList.add(new Student("John",95));
scoresList.add(new Student("Alice",89));
scoresList.add(new Student("Bob",92));
scoresList.add(new Student("Charlie",85));
scoresList.add(new Student("David",98));
scoresList.add(new Student("Emma",91));
scoresList.add(new Student("Frank",87));
scoresList.add(new Student("Grace",96));
scoresList.add(new Student("Henry",93));
scoresList.add(new Student("Ivy",90));
scoresList.add(new Student("Jack",94));
scoresList.add(new Student("Kelly",88));
scoresList.add(new Student("Liam",97));
scoresList.add(new Student("Mia",84));
scoresList.add(new Student("Noah",99));
scoresList.add(new Student("Olivia",82));
scoresList.add(new Student("Sophia",100));
System.out.println("Top 15 Scores:");
scoresList.display();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!