Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

listADT: package jsjf; import java.util.Iterator; import jsjf.exceptions.*; /** * ListADT defines the interface to a general list collection. Specific * types of lists will extend

image text in transcribed

listADT:

package jsjf;

import java.util.Iterator; import jsjf.exceptions.*; /** * ListADT defines the interface to a general list collection. Specific * types of lists will extend this interface to complete the * set of necessary operations. * * @author Lewis and Chase * @version 4.0 */ public interface ListADT extends Iterable { /** * Removes and returns the first element from this list. * * @return the first element from this list */ public T removeFirst() throws EmptyCollectionException;

/** * Removes and returns the last element from this list. * * @return the last element from this list */ public T removeLast() throws EmptyCollectionException;

/** * Removes and returns the specified element from this list. * * @param element the element to be removed from the list */ public T remove(T element) throws EmptyCollectionException, ElementNotFoundException;

/** * Returns a reference to the first element in this list. * * @return a reference to the first element in this list */ public T first() throws EmptyCollectionException;

/** * Returns a reference to the last element in this list. * * @return a reference to the last element in this list */ public T last() throws EmptyCollectionException;

/** * Returns true if this list contains the specified target element. * * @param target the target that is being sought in the list * @return true if the list contains this element */ public boolean contains(T target) throws EmptyCollectionException;

/** * Returns true if this list contains no elements. * * @return true if this list contains no elements */ public boolean isEmpty();

/** * Returns the number of elements in this list. * * @return the integer representation of number of elements in this list */ public int size();

/** * Returns an iterator for the elements in this list. * * @return an iterator over the elements in this list */ public Iterator iterator();

/** * Returns a string representation of this list. * * @return a string representation of this list */ public String toString(); }

OrderedListADT

package jsjf;

/** * OrderedListADT defines the interface to an ordered list collection. Only * Comparable elements are stored, kept in the order determined by * the inherent relationship among the elements. * * @author Lewis and Chase * @version 4.0 */ public interface OrderedListADT extends ListADT { /** * Adds the specified element to this list at the proper location * * @param element the element to be added to this list */ public void add(T element); }

SinglyLinkedNode?

package collections;

/** * A simple node for a singly-linked list * @author J. Downarowicz */ 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; }

AnimalGUIStart?

/* * AnimalGUIStart.java * Created on May 11, 2012 * A Swing frame for storing animals in ordered list * Used in lab 4 */ 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

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

Animal:

package animals;

import exceptions.*; import javax.swing.JTextArea; public class Animal { /** create Name of the Animal*/ protected String name; /** create weight of the Animal*/ protected double weight; /** create age of the Animal*/ protected int age; /** * Creats a new instance variable * @param name Name of the animal * @param weight Weight of the animal * @param age Age of the animal * @throws InvalidWeightException * @throws InvalidNameException */

public Animal(String name, double weight, int age) throws InvalidWeightException, InvalidNameException { if (name.length() 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. 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions