Do a modified version of 20.2 - fill in the sort and reverse methods in the atta
ID: 3688591 • Letter: D
Question
Do a modified version of 20.2 - fill in the sort and reverse methods in the attached file. (Your method only has to sort/reverse a Linked List that is passed in, not have any GUI user input).?
Use Java to answer this question. Test program is below.
import java.util.LinkedList;
public class TestLinkedListLab {
private static double score = 0;
private static LinkedList<Integer> intList1 = new LinkedList<Integer>();
private static LinkedList<Integer> intList2 = new LinkedList<Integer>();
private static LinkedList<String> stringList1 = new LinkedList<String>();
private static LinkedList<String> stringList2 = new LinkedList<String>();
private static LinkedList<Integer> correctIntList1 = new LinkedList<Integer>();
private static LinkedList<Integer> correctIntList2 = new LinkedList<Integer>();
private static LinkedList<String> correctStringList1 = new LinkedList<String>();
private static LinkedList<String> correctStringList2 = new LinkedList<String>();
public static void main(String[] args) {
initializeLists();
LinkedListLab.sort(intList1);
adjustScore(intList1.equals(correctIntList1), "Integer Sort", 2.5);
LinkedListLab.reverse(intList2);
adjustScore(intList2.equals(correctIntList2), "Integer Reverse", 2.5);
LinkedListLab.sort(stringList1);
adjustScore(stringList1.equals(correctStringList1), "String Sort", 2.5);
LinkedListLab.reverse(stringList2);
adjustScore(stringList2.equals(correctStringList2), "String Reverse", 2.5);
System.out.println("Total points (out of 10): " + score);
}
public static void adjustScore(boolean test, String successMessage, double points) {
if(test) {
System.out.println("Passed test: " + successMessage + ": " + points);
score += points;
} else {
System.out.println("Failed test: " + successMessage);
}
}
public static void initializeLists() {
intList1.add(5);
intList1.add(217);
intList1.add(8);
intList1.add(2);
intList1.add(7);
intList2.add(5);
intList2.add(217);
intList2.add(8);
intList2.add(2);
intList2.add(7);
stringList1.add("s");
stringList1.add("e");
stringList1.add("y");
stringList1.add("d");
stringList1.add("b");
stringList2.add("s");
stringList2.add("e");
stringList2.add("y");
stringList2.add("d");
stringList2.add("b");
correctIntList1.add(2);
correctIntList1.add(5);
correctIntList1.add(7);
correctIntList1.add(8);
correctIntList1.add(217);
correctIntList2.add(7);
correctIntList2.add(2);
correctIntList2.add(8);
correctIntList2.add(217);
correctIntList2.add(5);
correctStringList1.add("b");
correctStringList1.add("d");
correctStringList1.add("e");
correctStringList1.add("s");
correctStringList1.add("y");
correctStringList2.add("b");
correctStringList2.add("d");
correctStringList2.add("y");
correctStringList2.add("e");
correctStringList2.add("s");
}
}
(Store numbers in a linked list) Write a program that lets the user enter num- bers from a graphical user interface and displays them in a text area, as shown in Figure 20.17a. Use a linked list to store the numbers. Do not store duplicate num- bers. Add the buttons Sort, Shuffle, and Reverse to sort, shuffle, and reverse the list.Explanation / Answer
Hi below i have written the program for your reference,
import javafx.application.Application
import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.KeyCode; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.util.Collections; import java.util.LinkedList; /** * (Store numbers in a linked list) * Write a program that lets the user enter numbers from a graphical user * interface and displays them in a text area, as shown in Figure 20.17a. * Use a linked list to store the numbers. Do not store duplicate numbers. * Add the buttons Sort, Shuffle, and Reverse to sort, shuffle, and reverse the list. * */ public class Exercise_02 extends Application { @Override public void start(Stage primaryStage) throws Exception { primaryStage.setScene(new Scene(new NumberListPane())); primaryStage.setTitle("Store numbers in linked list"); primaryStage.show(); } private class NumberListPane extends BorderPane { private Label lblNumber; private Label lblNotANumber; private TextField tfNumber; private TextArea taField; private LinkedList<Integer> list; private Button btnSort; private Button btnShuffle; private Button btnReverse; NumberListPane() { list = new LinkedList<>(); // Top Node lblNumber = new Label("Enter a number:"); lblNotANumber = new Label(); lblNotANumber.setVisible(false); tfNumber = new TextField(); tfNumber.setPrefColumnCount(4); HBox hBox = new HBox(10, lblNumber, tfNumber, lblNotANumber); hBox.setAlignment(Pos.CENTER); hBox.setPadding(new Insets(5)); setTop(hBox); // End top node // Center Node taField = new TextArea(); ScrollPane scrollPane = new ScrollPane(taField); scrollPane.setPrefHeight(100); setCenter(scrollPane); // End center Node // Bottom Node btnSort = new Button("Sort"); btnReverse = new Button("Reverse"); btnShuffle = new Button("Shuffle"); hBox = new HBox(20, btnSort, btnReverse, btnShuffle); hBox.setAlignment(Pos.BASELINE_CENTER); hBox.setPadding(new Insets(5)); setBottom(hBox); // End bottom node // Listeners tfNumber.setOnKeyPressed(e -> { if (e.getCode() == KeyCode.ENTER) { int number; try { number = Integer.parseInt(tfNumber.getText()); if (lblNotANumber.isVisible()) lblNotANumber.setVisible(false); if (!list.contains(number)) { list.add(number); appendTextArea(); } else { lblNotANumber.setText("That's duplicate number!"); lblNotANumber.setVisible(true); } } catch (NumberFormatException ex) { lblNotANumber.setText("Not a number!"); lblNotANumber.setVisible(true); } } }); btnShuffle.setOnMouseClicked(e-> { Collections.shuffle(list); refreshTextArea(); }); btnSort.setOnMouseClicked(e-> { Collections.sort(list); refreshTextArea(); }); btnReverse.setOnMouseClicked(e-> { Collections.reverse(list); refreshTextArea(); }); } private void refreshTextArea() { String string = ""; for (Integer s : list) { string += s + " "; } taField.setText(string); } private void appendTextArea() { taField.appendText(" "+list.getLast()); } } public static void main(String[] args) { Application.launch(args); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.