Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 3 Lnai 9853

Authors: Bettina Berendt ,Bjorn Bringmann ,Elisa Fromont ,Gemma Garriga ,Pauli Miettinen ,Nikolaj Tatti ,Volker Tresp

1st Edition

3319461303, 978-3319461304

More Books

Students also viewed these Databases questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

What is quality of work life ?

Answered: 1 week ago