Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

ListADT.java

package jsjf;

import java.util.Iterator; import jsjf.exceptions.*; public interface ListADT extends Iterable { public T removeFirst() throws EmptyCollectionException; public T removeLast() throws EmptyCollectionException; public T remove(T element) throws EmptyCollectionException, ElementNotFoundException; public T first() throws EmptyCollectionException; public T last() throws EmptyCollectionException; public boolean contains(T target) throws EmptyCollectionException; public boolean isEmpty(); public int size(); public Iterator iterator(); public String toString(); }

SinglyLinkedNode.java

package collections; public class SinglyLinkedNode { private T element = null; private SinglyLinkedNode next; public SinglyLinkedNode() { next = null; element = null; } public SinglyLinkedNode(T element) { this.element = element; next = null; } public SinglyLinkedNode(T element, SinglyLinkedNode next) { this.element = element; this.next = next; } public T getElement() { return element; } public void setElement(T elem) { element = elem; } public SinglyLinkedNode getNext() { return next; } public void setNext(SinglyLinkedNode node) { next = node; } }

OrderedListADT.java

package jsjf; public interface OrderedListADT extends ListADT { public void add(T element); }

AnimalGUIStart.java

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 { private GetInputPanel namePanel = null; private GetInputPanel weightPanel = null; private GetComboPanel agePanel = null; private JButton addReptileButton = null; private JButton addMammalButton = null; private JButton displayAnimalsButton = null; private JButton removeFirstButton = null; private JButton removeLastButton = null; private final JTextArea verifyArea = new JTextArea(20, 35); private GetComboPanel lengthPanel = null; private GetInputPanel colorPanel = null;

public static void main(String[] args) { AnimalGUIStart frame = new AnimalGUIStart(); frame.setSize(800, 650); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public AnimalGUIStart() { super("Ordered list of Animals"); createGUI(); }

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); }

@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 } }

class GetInputPanel extends JPanel {

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

public GetInputPanel(int size, String prompt) { inputField = new JTextField(size); add(new JLabel(prompt)); add(inputField); }

public String getText() { return inputField.getText(); }

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; } }

class GetComboPanel extends JPanel {

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

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); }

public int getValue() { int a; a = Integer.parseInt((String) combo.getSelectedItem()); return a; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions