Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue

Lab Assignment :

In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals.

1. Write a LinkedQueue class that implements the QueueADT.java interface using links.

2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable to count number of nodes.

3. Use the SinglyLinkedNode class to implement the queue with links.

4 Methods dequeue and first in your LinkedQueue class must throw an EmptyQueueException when queue is empty. You must write the EmptyQueueException class.

5.Use an application to create a queue of animals. Create mammal and reptile objects, store them in the queue, remove the animals, request first animal and display the queue. You can use the provided AnimalGUI application or you can create your own.

Provided Files:

QueueADT

SinglyLinkedNode

AnimalGUI

Animal, Reptile, Mammal classes

QueueADT:

package collections; import exceptions.*;

public interface QueueADT{ /** * Adds one element to the rear of this queue. * @param element the element to be added to the rear of this queue */ public void enqueue (T element);

/** * Removes and returns the element at the front of this queue. * @return the element at the front of this queue * @throws EmptyQueueException if an empty collection exception occurs */ public T dequeue()throws EmptyQueueException ;

/** * Returns without removing the element at the front of this queue. * @return the first element in this queue * @throws EmptyQueueException if an empty collection exception occurs */ public T first()throws EmptyQueueException ; /** * Returns true if this queue contains no elements. * @return true if this queue is empty, false otherwise */ public boolean isEmpty();

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

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

SinglyLinkedNode:

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

AnimalGUI:

package gui;

import animals.*; import collections.*; import exceptions.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class AnimalGUIStart extends JFrame implements ActionListener {

//DECLARE LINKEDQUEUE OBJECT /** * GUI component for getting the name */ private GetInputPanel namePanel; /** * GUI component for getting the weight */ private GetInputPanel weightPanel; /** * GUI component for getting the age */ private GetComboPanel agePanel; /** * GUI component for getting the number of legs */ private GetComboPanel lengthPanel; /** * GUI component for getting the color */ private GetInputPanel colorPanel; /** * Button for adding a reptile to the collection */ private JButton addReptileButton; /** * Button for adding a reptile to the collection */ private JButton addMammalButton; /** * Button for viewing the next animal in the queue */ private JButton nextAnimalButton; /** * Button for displaying animals in the collection */ private JButton displayAnimalsButton; /** * A temporary GUI component for validating data */ private JTextArea verifyArea ; /** * Button for removing an animal from the collection */ private JButton removeAnimalButton;

/** * Creates a new instance of AddAnimalFrame which is contains panels and other * GUI components */ public AnimalGUIStart() { super("Queue of Animals"); createGUI(); }

/** * main() instantiates an object * * @param argv */ static public void main(String[] argv) { AnimalGUIStart animalFrame = new AnimalGUIStart(); animalFrame.setSize(450, 550); animalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

/** * A method to create GUI components */ private void createGUI() { Container c = this.getContentPane(); c.setLayout(new BorderLayout(5, 5));

JPanel inputPanel = new JPanel(); //contains GUI to input animals info 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); lengthPanel = new GetComboPanel("Reptile's Length (cm)", 1000); inputPanel.add(lengthPanel); colorPanel = new GetInputPanel(20, "Mammal's skin/fur color"); inputPanel.add(colorPanel);

JPanel buttonPanel = new JPanel(); //contains buttons buttonPanel.setLayout(new GridLayout(1, 5, 5, 5)); addReptileButton = new JButton("Add Reptile"); addMammalButton = new JButton("Add Mammal"); nextAnimalButton = new JButton("Next Animal"); displayAnimalsButton = new JButton("Display Animals"); removeAnimalButton = new JButton("Remove Animal"); addReptileButton.setToolTipText("Press to add Reptile"); addMammalButton.setToolTipText("Press to add Mammal"); buttonPanel.add(addReptileButton); buttonPanel.add(addMammalButton); buttonPanel.add(removeAnimalButton); buttonPanel.add(nextAnimalButton); buttonPanel.add(displayAnimalsButton);

c.add(inputPanel, BorderLayout.NORTH); c.add(buttonPanel, BorderLayout.CENTER); verifyArea = new JTextArea(18, 25); c.add(verifyArea, BorderLayout.SOUTH); JScrollPane scrollPane = new JScrollPane(verifyArea); c.add(scrollPane, BorderLayout.SOUTH); addReptileButton.addActionListener(this); addMammalButton.addActionListener(this); displayAnimalsButton.addActionListener(this); nextAnimalButton.addActionListener(this); removeAnimalButton.addActionListener(this);

setVisible(true); pack(); }

/** * Responds to the "Display" and "Add" buttons * * @param ev The button press event */ @Override public void actionPerformed(ActionEvent ev) { Object object = ev.getSource(); if (object == addReptileButton) { //create a Mammal //store mammal in the queue //catch exceptions } else if (object == addMammalButton) { //create a Mammal //store mammal in the queue //catch exceptions } else if (object == removeAnimalButton) { //remove animal //catch EmptyQueue exception } else if (object == nextAnimalButton) { //get the first animal //catch EmptyQueue exception } else { //display animals in the queue } }

/** * 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); JLabel label = new JLabel(prompt); add(label); 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 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 ageCombo; //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) { label = new JLabel(message); String[] age = new String[numChoices];

for (int i = 0; i < age.length; i++) { age[i] = i + 1 + ""; } ageCombo = new JComboBox(age);

add(label); add(ageCombo); }

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

Animal, Reptile, Mammal classes:

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() < 2) { throw new InvalidNameException("The name must be at least two character"); } this.name = name; if (weight <= 0) { throw new InvalidWeightException("The wight must be greater than zero"); } this.weight=weight; this.age = age; } /** * @param name */ public void setName(String name) { this.name = name; } /** * gets the name of the animal * @return the string contains name */ public String getName() { return name; } /** * @param weight */ public void setWeight(double weight) { this.weight = weight; } /** * gets the weight of the animal * @return the string contains weight */ public double getWeight() { return weight; } /** * @param age */ public void setAge(int age) { this.age = age; } /** * gets the age of the animal * @return the string contains age */ public int getAge() { return age; } /** * display the information about the animal in the textArea * @param output a text area for display */ public void display(JTextArea output) { output.append("name" + name + "weight" + weight + "age" + age); } /** * @return a string containing contents of the object's fields */ @Override public String toString() { return (" Animal Name: " + name + "\t weight:" + weight + "\t age:" + age); } }

import exceptions.InvalidNameException; import exceptions.InvalidWeightException; import javax.swing.JTextArea; public class Mammal extends Animal {

private String hairColor;

/** * * @param name * @param weight * @param age * @param hairColor * @throws InvalidWeightException * @throws InvalidNameException */ public Mammal(String name, double weight, int age, String hairColor) throws InvalidWeightException, InvalidNameException { super(name, weight, age); this.hairColor = hairColor; }

/** * @param hairColor */ public void setColor(String hairColor) { this.hairColor = hairColor; }

/** * gets the color of the mammal * * @return the color of the mammal */ public String getColor() { return hairColor; }

/** * Displays a Mammal in the textArea * * @param output a text area to display an information about the animal */ @Override public void display(JTextArea output) { super.display(output); output.append("\t hair/fur color: " + hairColor); }

/** * Returns a string representation of the Mammal object * * @return a string containing contents of the object's fields */ @Override public String toString() { return (super.toString() + "\t hair/fur color: " + hairColor); } }

import exceptions.InvalidNameException; import exceptions.InvalidWeightException; import javax.swing.JTextArea; public class reptile extends Animal {

private int length;

/** * * @param name * @param weight * @param age * @param length * @throws InvalidWeightException * @throws InvalidNameException */ public reptile(String name, Double weight, int age, int length) throws InvalidWeightException, InvalidNameException { super(name, weight, age); this.length = length; } /** * * @param length */ public void setLength(int length) { this.length = length; } /** * * @return the length of reptile */ public int getLength() { return this.length; } /** * * @param output a text area to display an information about the animal */ @Override public void display(JTextArea output) { super.display(output); output.append("length" + length); } /** * * @return a string representation of the reptile object */ @Override public String toString() { return (super.toString() + "length" + this.length); } }

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

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

1. Critically discuss treatment approaches for violent offenders.

Answered: 1 week ago

Question

Define offboarding. Why is it important?

Answered: 1 week ago