Question
Modify the LinkedList1Demo class to maintain a list of the top ten performers in a video game. I also suggest you change the window name
Modify the LinkedList1Demo class to maintain a list of the top ten performers in a video game. I also suggest you change the window name and labels. An entry on the list consists of a name and score, and the list is kept sorted in descending order of scores. Here is an example of such a list when it has only four elements.
Spike 120
Whiz 105
G-Man 99
JediMaster 95
The add method needs to put the entry in the proper position so that the list stays sorted by score. The list should have a maximum size of 10. After the list has 10 elements, an attempt to add a name with a score that is less than or equal to the minimum score on the list is ignored, and adding a score that is greater than the minimum score causes an entry with the minimum score to be dropped from the list. ////LinkedList1Demo
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /** This class is used to demonstrate the operations in the LinkedList1 class. */ public class LinkedList1Demo extends JFrame { private LinkedList1 ll; private JTextArea listView; private JTextField cmdTextField; private JTextField resultTextField; public LinkedList1Demo() { ll = new LinkedList1(); listView = new JTextArea(); cmdTextField = new JTextField(); resultTextField = new JTextField(); // Create a panel and label for result field JPanel resultPanel = new JPanel(new GridLayout(1,2)); resultPanel.add(new JLabel("Command Result")); resultPanel.add(resultTextField); resultTextField.setEditable(false); add(resultPanel, BorderLayout.NORTH); // Put the textArea in the center of the frame add(listView); listView.setEditable(false); listView.setBackground(Color.WHITE); // Create a panel and label for the command text field JPanel cmdPanel = new JPanel(new GridLayout(1,2)); cmdPanel.add(new JLabel("Command:")); cmdPanel.add(cmdTextField); add(cmdPanel, BorderLayout.SOUTH); cmdTextField.addActionListener(new CmdTextListener()); // Set up the frame setTitle("Linked List Demo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } /** Private class that responds to the command that the user types into the command entry text field. */ private class CmdTextListener implements ActionListener { public void actionPerformed(ActionEvent evt) { String cmdText = cmdTextField.getText(); Scanner sc = new Scanner(cmdText); String cmd = sc.next(); if (cmd.equals("add")) { if (sc.hasNextInt()) { // add index element int index = sc.nextInt(); String element = sc.next(); ll.add(index, element); } else { // add element String element = sc.next(); ll.add(element); } listView.setText(ll.toString()); pack(); return; } if (cmd.equals("remove")) { if (sc.hasNextInt()) { // remove index int index = sc.nextInt(); String res = ll.remove(index); resultTextField.setText(res); } else { // remove element String element = sc.next(); boolean res = ll.remove(element); String resText = String.valueOf(res); resultTextField.setText(resText); } listView.setText(ll.toString()); pack(); return; } if (cmd.equals("isempty")) { String resText = String.valueOf(ll.isEmpty()); resultTextField.setText(resText); return; } if (cmd.equals("size")) { String resText = String.valueOf(ll.size()); resultTextField.setText(resText); return; } } } /** The main method creates an instance of the LinkedList1Demo class which causes it to display its window. */ public static void main(String [ ] args) { new LinkedList1Demo(); } } ///LinkedList1
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /** This class is used to demonstrate the operations in the LinkedList1 class. */ public class LinkedList1Demo extends JFrame { private LinkedList1 ll; private JTextArea listView; private JTextField cmdTextField; private JTextField resultTextField; public LinkedList1Demo() { ll = new LinkedList1(); listView = new JTextArea(); cmdTextField = new JTextField(); resultTextField = new JTextField(); // Create a panel and label for result field JPanel resultPanel = new JPanel(new GridLayout(1,2)); resultPanel.add(new JLabel("Command Result")); resultPanel.add(resultTextField); resultTextField.setEditable(false); add(resultPanel, BorderLayout.NORTH); // Put the textArea in the center of the frame add(listView); listView.setEditable(false); listView.setBackground(Color.WHITE); // Create a panel and label for the command text field JPanel cmdPanel = new JPanel(new GridLayout(1,2)); cmdPanel.add(new JLabel("Command:")); cmdPanel.add(cmdTextField); add(cmdPanel, BorderLayout.SOUTH); cmdTextField.addActionListener(new CmdTextListener()); // Set up the frame setTitle("Linked List Demo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } /** Private class that responds to the command that the user types into the command entry text field. */ private class CmdTextListener implements ActionListener { public void actionPerformed(ActionEvent evt) { String cmdText = cmdTextField.getText(); Scanner sc = new Scanner(cmdText); String cmd = sc.next(); if (cmd.equals("add")) { if (sc.hasNextInt()) { // add index element int index = sc.nextInt(); String element = sc.next(); ll.add(index, element); } else { // add element String element = sc.next(); ll.add(element); } listView.setText(ll.toString()); pack(); return; } if (cmd.equals("remove")) { if (sc.hasNextInt()) { // remove index int index = sc.nextInt(); String res = ll.remove(index); resultTextField.setText(res); } else { // remove element String element = sc.next(); boolean res = ll.remove(element); String resText = String.valueOf(res); resultTextField.setText(resText); } listView.setText(ll.toString()); pack(); return; } if (cmd.equals("isempty")) { String resText = String.valueOf(ll.isEmpty()); resultTextField.setText(resText); return; } if (cmd.equals("size")) { String resText = String.valueOf(ll.size()); resultTextField.setText(resText); return; } } } /** The main method creates an instance of the LinkedList1Demo class which causes it to display its window. */ public static void main(String [ ] args) { new LinkedList1Demo(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started