In this assignment, the goal is to exercise Linked List implementation in a GUI
ID: 3713493 • Letter: I
Question
In this assignment, the goal is to exercise Linked List implementation in a GUI as a JavaFX Application.
First you are going to implement a Generic linked-list. You can specify the type as "T extends Number". This class must have minimum following member methods: constructor, insertItem, printList, and clearList.
insertItem: insert data to the list in the ascending order
printList : prints the list starting from the head
clearList : Assigns all items in the list to null,
In the GUI, there will be 5 numbered buttons, which represent the “data” to be inserted into the list.
When a numbered button is clicked:
its data will be inserted into mList,
the numbered button will be disabled,
the information field will be updated using mList.printList()
When Reset button is clicked:
all numbered buttons will be enabled
mList will be cleared
The information field will be updated using mList.printList()
When Close button is clicked:
The application will be closed.
2 3 4 5 mHead -> Reset CloseExplanation / Answer
Below is your code: -
NumberComparator.java
public class NumberComparator implements Comparator<Number> {
@Override
public int compare(Number o1, Number o2) {
if (o1 instanceof Short && o2 instanceof Short) {
return ((Short) o1).compareTo((Short) o2);
} else if (o1 instanceof Long && o2 instanceof Long) {
return ((Long) o1).compareTo((Long) o2);
} else if (o1 instanceof Integer && o2 instanceof Integer) {
return ((Integer) o1).compareTo((Integer) o2);
} else if (o1 instanceof Float && o2 instanceof Float) {
return ((Float) o1).compareTo((Float) o2);
} else if (o1 instanceof Double && o2 instanceof Double) {
return ((Double) o1).compareTo((Double) o2);
} else if (o1 instanceof Byte && o2 instanceof Byte) {
return ((Byte) o1).compareTo((Byte) o2);
} else if (o1 instanceof BigInteger && o2 instanceof BigInteger) {
return ((BigInteger) o1).compareTo((BigInteger) o2);
} else if (o1 instanceof BigDecimal && o2 instanceof BigDecimal) {
return ((BigDecimal) o1).compareTo((BigDecimal) o2);
} else {
throw new RuntimeException("Not implemented yet.");
}
}
}
GenLinkedList.java
//GenLinkedList.java
public class GenLinkedList<T extends Number> {
class ListNode {
// Instance variables
private T element;
private ListNode next;
/**
*
* Constructor
*
*
*
* @param element
*
* - element
*
*/
public ListNode(T element) {
this.element = element;
this.next = null;
}
@Override
public String toString() {
return "" + element;
}
}
// Instance variable for LinkedList
private ListNode head;
private int size;
private NumberComparator comp;
/**
*
* Constructor
*
*/
public GenLinkedList() {
this.head = null;
this.size = 0;
this.comp = new NumberComparator();
}
/**
*
* Adds a new node to the list in ascending order
*
*/
public void insertItem(T element) {
// Create new node
ListNode newNode = new ListNode(element);
// Check if list is null
if (this.size == 0) {
// Set newNode as the head
this.head = newNode;
} else {
// Check if element is less than head
if (this.comp.compare(element, this.head.element) < 0) {
// Set newnode.next as head
newNode.next = this.head;
// Set newNode as head
this.head = newNode;
} else {
// If list is not null
boolean added = false;
// Get the head node
ListNode node = this.head;
while (!added && (node.next != null)) {
// Compare element with element at node
int res = this.comp.compare(element, node.next.element);
if (res <= 0) { // If element is less than or equal
// to the element at node.next
// Set newNode.next as node.next
newNode.next = node.next;
// Set node.next as newNode
node.next = newNode;
added = true;
}
// Go to next node
node = node.next;
}
// Check if node was not added
if (!added) {
if (this.comp.compare(element, node.element) != 0) {
// Set node.next as newNode
node.next = newNode;
}
}
}
}
// Increment count
this.size += 1;
}
/**
*
* Prints the list
*
*/
public String printList() {
if (this.size == 0)
return "";
else {
StringBuffer sb = new StringBuffer();
ListNode node = this.head;
// Append first node
sb.append(node);
node = node.next;
while (node != null) {
sb.append(" -> " + node);
node = node.next;
}
return sb.toString();
}
}
/**
*
* Clears the list
*
*/
public void clearList() {
this.head = null;
this.size = 0;
}
}
ListPane.java
public class ListPane extends VBox implements EventHandler<ActionEvent> {
private static final int NUM_BUTTONS = 5;
private static final String DISPLAY_TEXT = "mHead -> %s";
// Instance variables
private Integer[] num;
private Button[] numBtn;
private Label displayLbl;
private Button resetBtn;
private Button closeBtn;
private GenLinkedList<Integer> numList;
/**
*
* Constructor
*
*/
public ListPane() {
this.numBtn = new Button[NUM_BUTTONS];
this.num = new Integer[NUM_BUTTONS];
this.numList = new GenLinkedList<Integer>();
// Set vBox padding and spacing
setPadding(new Insets(10, 10, 5, 10));
setSpacing(20);
// Init buttons
for (int i = 0; i < NUM_BUTTONS; i++) {
this.num[i] = new Integer(i + 1);
// Create button
this.numBtn[i] = new Button("" + (i + 1));
this.numBtn[i].setOnAction(this);
this.numBtn[i].setPrefSize(80, 30);
}
// Create button box
addNumButtonBox();
// Create label box
addLabelBox();
// Add reset and close buttons
addCtrlButtonBox();
}
/**
*
* Add number buttons
*
*/
private void addNumButtonBox() {
HBox hBox = new HBox();
// Add a region to center align buttons
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
hBox.getChildren().add(region);
// Add number buttons to the hBox
for (int i = 0; i < NUM_BUTTONS; i++)
hBox.getChildren().add(this.numBtn[i]);
// Add a region to center align buttons
Region region2 = new Region();
HBox.setHgrow(region2, Priority.ALWAYS);
hBox.getChildren().add(region2);
// Add hbox to vbox
getChildren().add(hBox);
}
/**
*
* Creates and adds display label
*
*/
private void addLabelBox() {
HBox hBox = new HBox();
// Add a region to center align label
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
hBox.getChildren().add(region);
// Add display label
displayLbl = new Label(String.format(DISPLAY_TEXT, ""));
hBox.getChildren().add(displayLbl);
// Add a region to center align label
Region region2 = new Region();
HBox.setHgrow(region2, Priority.ALWAYS);
hBox.getChildren().add(region2);
// Add hbox to vbox
getChildren().add(hBox);
}
/**
*
* Adds reset and close buttons
*
*/
private void addCtrlButtonBox() {
HBox hBox = new HBox();
hBox.setSpacing(10);
// Add a region to left align buttons
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
hBox.getChildren().add(region);
// Create reset button
resetBtn = new Button("Reset");
resetBtn.setOnAction(this);
// Add reset button
hBox.getChildren().add(resetBtn);
// Create reset button
closeBtn = new Button("Close");
closeBtn.setOnAction(this);
// Add reset button
hBox.getChildren().add(closeBtn);
// Add hbox to vbox
getChildren().add(hBox);
}
/**
*
* Updates the label text
*
*/
private void updateLabel(String text) {
this.displayLbl.setText(String.format(DISPLAY_TEXT, text));
}
/**
*
* Enables all buttons and clears the list.
*
*/
private void reset() {
for (int i = 0; i < NUM_BUTTONS; i++)
this.numBtn[i].setDisable(false);
// Clear list
this.numList.clearList();
// Update label
updateLabel("");
}
/**
*
* Handles button click
*
*/
@Override
public void handle(ActionEvent ae) {
Button btn = (Button) ae.getSource();
if (btn == closeBtn) // Close
System.exit(0);
else if (btn == resetBtn) // Reset
reset();
else {
// Check for number buttons
for (int i = 0; i < NUM_BUTTONS; i++) {
if (btn == this.numBtn[i]) {
this.numBtn[i].setDisable(true);
// Add number to the list
this.numList.insertItem(this.num[i]);
break;
}
}
// Update display label
updateLabel(this.numList.printList());
}
}
}
LinkedListGUI.java
public class LinkedListGUI extends Application {
@Override
public void start(Stage primaryStage) {
// Set resizable
primaryStage.setResizable(false);
// Set size as the size of the scene
primaryStage.sizeToScene();
// Center frame to screen
primaryStage.centerOnScreen();
// Create SpinningWheelPane
ListPane pane = new ListPane();
// Create scene
Scene scene = new Scene(pane, 450, 138);
// Add scene to stage
primaryStage.setScene(scene);
// Show stage
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}