Question: Objectives: Implementing ListADT and OrderedListADT interfaces with links Implementing Comparable interface Implementing Iterable interface Implementing Iterator interface with links Provided Files: ListADT OrderedListADT SinglyLinkedNode AnimalGUIStart

Objectives:

Implementing ListADT and OrderedListADT interfaces with links Implementing Comparable interface Implementing Iterable interface Implementing Iterator interface with links

Provided Files:

ListADT

OrderedListADT

SinglyLinkedNode

AnimalGUIStart

Lab Assignment:

In this lab, you are going to store Animal objects in the ordered list. The animals will be sorted by the name.

1. Write LinkedList class that implements ListADT and Iterable interfaces. Use the provided SinglyLinkedNode class to create the links. 2. Write the private inner class ListIterator inside the LinkedList class. ListIterator class must implements Iterator interface.

2. Write LinkedOrderedList class that extends LinkedList class and implements OrderedListADT.

3. Modify class Animal so it implements interface Comparable. Implement method compareTo in class Animal. This method should compare animals by their name in alphabetical order. Method compareTo will be used by the add method in the OrderredLinkedList class. Method add in the LinkedOrderdList adds animals to the list in the order specified by the method compareTo in the Animal class..

4.Use an application to use create an ordered list of animals. Create mammal and reptile objects, store them in the list, remove the animals and display the list. You can use the provided AnimalGUIStart application or you can create your own.

ANIMALGUISTART /

/* * A Swing frame for storing animals in ordered list * */ package gui;

import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import java.util.Iterator;

public class AnimalGUIStart extends JFrame implements ActionListener {

/** * GUI component for getting the name */ private GetInputPanel namePanel = null; /** * GUI component for getting the weight */ private GetInputPanel weightPanel = null; /** * GUI component for getting the age */ private GetComboPanel agePanel = null; /** * Button for adding a reptile to the collection */ private JButton addReptileButton = null; /** * Button for adding a mammal to the collection */ private JButton addMammalButton = null; /** * Button for displaying animals in the collection */ private JButton displayAnimalsButton = null; /** * Button for removing the first element from the list */ private JButton removeFirstButton = null; /** * Button for removing the last element from the list */ private JButton removeLastButton = null; /** * GUI component for validating data */ private final JTextArea verifyArea = new JTextArea(20, 35); /** * GUI component for getting the length */ private GetComboPanel lengthPanel = null; /** * GUI component for getting the color */ private GetInputPanel colorPanel = null;

public static void main(String[] args) { AnimalGUIStart frame = new AnimalGUIStart(); frame.setSize(800, 650); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

/** * Constructor creates the GUI frame with a title */ public AnimalGUIStart() { super("Ordered list of Animals"); createGUI(); }

/** * method createGUI creates GUI objects and adds them to the frame */ private void createGUI() { Container c = this.getContentPane(); c.setLayout(new BorderLayout(5, 5)); // 5 pixels hor and vert gap

JPanel inputPanel = new JPanel(); //contains various input panels inputPanel.setLayout(new GridLayout(5, 1)); namePanel = new GetInputPanel(20, " Animal's Name: "); inputPanel.add(namePanel); weightPanel = new GetInputPanel(6, " Animal's Weight (lb): "); inputPanel.add(weightPanel); agePanel = new GetComboPanel("Animal's Age (years)", 125); inputPanel.add(agePanel); colorPanel = new GetInputPanel(20, " Mammal's color: "); inputPanel.add(colorPanel); lengthPanel = new GetComboPanel("Reptile's length (cm)", 1000); inputPanel.add(lengthPanel);

JPanel buttonPanel = new JPanel(); //contains buttons buttonPanel.setLayout(new GridLayout(1, 7, 5, 5));

addReptileButton = new JButton("Add Reptile");// To add reptile into array addMammalButton = new JButton("Add Mammal");//To add mammal into array displayAnimalsButton = new JButton("Display Animals");//To display animals removeFirstButton = new JButton("Remove First");//remove first removeLastButton = new JButton("Remove Last");//remove last addReptileButton.setToolTipText("Press to add Reptile"); addMammalButton.setToolTipText("Press to add Mammal");

buttonPanel.add(addReptileButton);//addReptileButton on the screen buttonPanel.add(addMammalButton);// add MammalButton on the screen buttonPanel.add(displayAnimalsButton);//add display button on the screen buttonPanel.add(removeFirstButton);//add remove first button buttonPanel.add(removeLastButton);//add remove last button

c.add(inputPanel, BorderLayout.NORTH); c.add(buttonPanel, BorderLayout.CENTER);

JScrollPane scrollPane = new JScrollPane(verifyArea); c.add(scrollPane, BorderLayout.SOUTH); addReptileButton.addActionListener(this); addMammalButton.addActionListener(this); displayAnimalsButton.addActionListener(this); removeFirstButton.addActionListener(this); removeLastButton.addActionListener(this); setVisible(true); }

/** * Responds to the "Display","Add" and "Remove" buttons * * @param ev ActionEvent object representing the event. */ @Override public void actionPerformed(ActionEvent ev) { Object object = ev.getSource(); if (object == addReptileButton) { //create a reptile //add reptile to the list //catch exceptions } else if (object == addMammalButton) { //create a Mammal //add mammal to the list //catch exceptions } else if (object == displayAnimalsButton) { verifyArea.setText(""); //display all animals in the list } else if (object == removeFirstButton) { //remove first animal from the list //catch EmptyQueue exception } else if (object == removeLastButton) { //remove last animal from the list //catch EmptyQueue exception } }

/** * A panel prompting for String input. It contains a label and a text field. */ class GetInputPanel extends JPanel {

private final JTextField inputField; //used for the user input

/** * Constructor sets up a label and the text field * * @param size the size of the input text field * @param prompt the message specifying expected input */ public GetInputPanel(int size, String prompt) { inputField = new JTextField(size); add(new JLabel(prompt)); add(inputField); }

/** * Gets the text from the text field * * @return Returns the text from the text field */ public String getText() { return inputField.getText(); }

/** * Converts the text field value into a number and displays an error message * when inputed data contains non digit characters * * @return the integer represented by the user input */ public double getValue() { double value = 0.0; try { value = Double.parseDouble(inputField.getText()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Invalid characters in the number", "Input Error", JOptionPane.ERROR_MESSAGE); } return value; } }

/** * This panel represents a panel with a label and a combo box. */ class GetComboPanel extends JPanel {

JLabel label; //explains the purpose of the combo box JComboBox combo; //used for the user input

/** * Constructor sets up a panel with a label and a combo box. * * @param message the text indicating the purpose of the combo box * @param numChoices the range of choices displayed in the combo box */ public GetComboPanel(String message, int numChoices) { String[] data = new String[numChoices]; for (int i = 0; i < data.length; i++) { data[i] = i + 1 + ""; } combo = new JComboBox(data); add(new JLabel(message)); add(combo); }

/** * Gets the value from the combo box * * @return value selected from the combo box */ public int getValue() { int a; a = Integer.parseInt((String) combo.getSelectedItem()); return a; } } }

SinglelinkedNode/

package collections;

/** * A simple node for a singly-linked list public class SinglyLinkedNode {

/** element stored at this node */ private T element = null; /** Link to the next node in list */ private SinglyLinkedNode next;

/** * Creates an empty node */ public SinglyLinkedNode() { next = null; element = null; }

/** * Creates a node storing the specified element * @param element element to be stored */ public SinglyLinkedNode(T element) { this.element = element; next = null; }

/** * Creates a node storing the specified element and the specified node * @param element Item to be stored * @param next Reference to the next node in the list */ public SinglyLinkedNode(T element, SinglyLinkedNode next) { this.element = element; this.next = next; }

/** * Returns the element stored at this node * @return T element stored element at this node */ public T getElement() { return element; }

/** * Set the reference to the stored element * @param element The item to be stored at this node */ public void setElement(T elem) { element = elem; }

/** * Returns the next node * @return SinglyLinkedNode reference to next node */ public SinglyLinkedNode getNext() { return next; }

/** * Sets the reference to the next node in the list * @param next The next node */ public void setNext(SinglyLinkedNode node) { next = node; } }

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!