Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java application called HiringApp that you will use to hire and fire wor

ID: 3588127 • Letter: W

Question

Write a java application called HiringApp that you will use to hire and fire workers for a company. The application HiringApp must use the LinkedStack and LinkedQueue2 classes that are covered in the class. The rules of hiring and firing are described as follows:

1) If you are asked to fire somebody at a time when the firm has no employees, you should notify your supervisor (print a message).

2) If you are asked to fire somebody when the firm has 1 or more employees, you must fire the most recently hired.

3) You are to keep a list of applicants and the order in which they applied.

4) When you are asked to hire someone, if anybody has been fired, the most recently fired must be re-hired.

5) If there is nobody who has been fired, then the person who applied earliest is to be hired.

6) If there is nobody available for hiring, then you must notify your supervisor (print a message).

The program should have a simple menu that allows you to specify the three actions:

• Accept application

• Hire

• Fire

• Quit

"Accept application" should prompt you for the name of an applicant and add that person's information to an appropriate data structure.

"Hire" should choose the appropriate person to hire, print his or her name, and appropriately update the internal data structures.

"Fire" should choose the appropriate person to fire, print his or her name, and appropriately update the internal data structures.

"Quit" should exit the program.

---LinkedStack.java---

import java.util.EmptyStackException;

// Class to implement interface StackInt<E> as a linked list.

public class LinkedStack<E> implements StackInt<E> {

   // Insert inner class Node<E> here (see Listing 2.1)

   /** A Node is the building block for a single-linked list. */

   private static class Node<E> {

       // Data Fields

       /** The reference to the data. */

       private E data;

       /** The reference to the next node. */

       private Node<E> next;

       // Constructors

// Creates a new node with a null next field.

       private Node(E dataItem) {

           data = dataItem;

           next = null;

       }

// Creates a new node that references another node

       private Node(E dataItem, Node<E> nodeRef) {

           data = dataItem;

           next = nodeRef;

       }

   }

   // End of inner class Node<E>

   // Data Fields

   /** The reference to the first stack node. */

   private Node<E> topOfStackRef = null;

   /**

   * Insert a new item on top of the stack.

   *

   * @post The new item is the top item on the stack. All other items are one

   * position lower.

   * @param obj

   * The item to be inserted

   * @return The item that was inserted

   */

   @Override

   public E push(E obj) {

       topOfStackRef = new Node<E>(obj, topOfStackRef);

       return obj;

   }

   /**

   * Remove and return the top item on the stack.

   *

   * @pre The stack is not empty.

   * @post The top item on the stack has been removed and the stack is one item

   * smaller.

   * @return The top item on the stack

   * @throws EmptyStackException

   * if the stack is empty

   */

   @Override

   public E pop() {

       if (empty()) {

           throw new EmptyStackException();

       } else {

           E result = topOfStackRef.data;

           topOfStackRef = topOfStackRef.next;

           return result;

       }

   }

   /**

   * Return the top item on the stack.

   *

   * @pre The stack is not empty.

   * @post The stack remains unchanged.

   * @return The top item on the stack

   * @throws EmptyStackException

   * if the stack is empty

   */

   @Override

   public E peek() {

       if (empty()) {

           throw new EmptyStackException();

       } else {

           return topOfStackRef.data;

       }

   }

   /**

   * See whether the stack is empty.

   *

   * @return true if the stack is empty

   */

   @Override

   public boolean empty() {

       return topOfStackRef == null;

   }

   public LinkedStack(E[] array) {

       // initialize stack

       this();

       // push elements, with last element pushed last

       for (E e : array) {

           push(e);

       }

   }

   public LinkedStack() {

   }

   public E peekNextTop() {

       // Variable item of type E is defined which is used to hold the top next element

       // of the stack

       E item = null;

       // method empty checks to see if the stack is empty, if so throw exception

       if (empty()) {

           throw new EmptyStackException();

       }

       // if stack is not empty, check that the ref of the top element of the stack is

       // null or not. If so, then it is sure that the stack contains only one element,

       // and hence item is set to null

       else if (topOfStackRef == null) {

           item = null;

           // if the topOfStackRef is not null, then it contains more than one element, adn

           // the ref of element next to the top is saved in a separate Node instance

           // nextToTop

       } else if (topOfStackRef != null) {

           Node<E> nextToTop = topOfStackRef.next;

           item = nextToTop.data;

       }

       //the value in item gets returned if no exception occurs;

       return item;

   }

   // End of class content for Students.

}

/* </listing> */

----ListQueue2----

import java.util.Queue;

import java.util.NoSuchElementException;

public class ListQueue2<E> {

// Data Fields

/** Reference to front of queue. */

private Node<E> front;

/** Reference to rear of queue. */

private Node<E> rear;

/** Size of queue. */

private int size;

// Insert inner class Node<E> here (see Listing 2.1)

/** A Node is the building block for a single-linked list. */

private static class Node<E> {

// Data Fields

/** The reference to the data. */

private E data;

/** The reference to the next node. */

private Node<E> next;

// Constructors

/**

   * Creates a new node with a null next field.

   * @param dataItem The data stored

   */

private Node(E dataItem) {

data = dataItem;

next = null;

}

/**

   * Creates a new node that references another node.

   * @param dataItem The data stored

   * @param nodeRef The node referenced by new node

   */

private Node(E dataItem, Node<E> nodeRef) {

data = dataItem;

next = nodeRef;

}

} //end class Node

// Methods

/**

   * Insert an item at the rear of the queue.

   * @post item is added to the rear of the queue.

   * @param item The element to add

   * @return true (always successful)

   */

public boolean offer(E item) {

// Check for empty queue.

if (front == null) {

rear = new Node<E>(item);

front = rear;

} else {

// Allocate a new node at end, store item in it, and

// link it to old end of queue.

rear.next = new Node<E>(item);

rear = rear.next;

}

size++;

return true;

}

/**

   * Remove the entry at the front of the queue and return it

   * if the queue is not empty.

   * @post front references item that was second in the queue.

   * @return The item removed if successful, or null if not

   */

public E poll() {

E item = peek(); // Retrieve item at front.

if (item == null) {

return null;

}

// Remove item at front.

front = front.next;

size--;

return item; // Return data at front of queue.

}

/**

   * Return the item at the front of the queue without removing it.

   * @return The item at the front of the queue if successful;

   * return null if the queue is empty

   */

public E peek() {

if (size == 0) {

return null;

} else {

return front.data;

}

}   

public int size() {

return size;   

}   

public boolean empty() {

       if (size == 0) {

           return true;

       }

       else

           return false;

}

}

/*</listing>*/

Explanation / Answer

import java.util.EmptyStackException;

// Class to implement interface StackInt<E> as a linked list.

public class LinkedStack<E> implements StackInt<E> {

   // Insert inner class Node<E> here (see Listing 2.1)

   /** A Node is the building block for a single-linked list. */

   private static class Node<E> {

       // Data Fields

       /** The reference to the data. */

       private E data;

       /** The reference to the next node. */

       private Node<E> next;

       // Constructors

// Creates a new node with a null next field.

       private Node(E dataItem) {

           data = dataItem;

           next = null;

       }

// Creates a new node that references another node

       private Node(E dataItem, Node<E> nodeRef) {

           data = dataItem;

           next = nodeRef;

       }

   }

   // End of inner class Node<E>

   // Data Fields

   /** The reference to the first stack node. */

   private Node<E> topOfStackRef = null;

   /**

   * Insert a new item on top of the stack.

   *

   * @post The new item is the top item on the stack. All other items are one

   * position lower.

   * @param obj

   * The item to be inserted

   * @return The item that was inserted

   */

   @Override

   public E push(E obj) {

       topOfStackRef = new Node<E>(obj, topOfStackRef);

       return obj;

   }

   /**

   * Remove and return the top item on the stack.

   *

   * @pre The stack is not empty.

   * @post The top item on the stack has been removed and the stack is one item

   * smaller.

   * @return The top item on the stack

   * @throws EmptyStackException

   * if the stack is empty

   */

   @Override

   public E pop() {

       if (empty()) {

           throw new EmptyStackException();

       } else {

           E result = topOfStackRef.data;

           topOfStackRef = topOfStackRef.next;

           return result;

       }

   }

   /**

   * Return the top item on the stack.

   *

   * @pre The stack is not empty.

   * @post The stack remains unchanged.

   * @return The top item on the stack

   * @throws EmptyStackException

   * if the stack is empty

   */

   @Override

   public E peek() {

       if (empty()) {

           throw new EmptyStackException();

       } else {

           return topOfStackRef.data;

       }

   }

   /**

   * See whether the stack is empty.

   *

   * @return true if the stack is empty

   */

   @Override

   public boolean empty() {

       return topOfStackRef == null;

   }

   public LinkedStack(E[] array) {

       // initialize stack

       this();

       // push elements, with last element pushed last

       for (E e : array) {

           push(e);

       }

   }

   public LinkedStack() {

   }

   public E peekNextTop() {

       // Variable item of type E is defined which is used to hold the top next element

       // of the stack

       E item = null;

       // method empty checks to see if the stack is empty, if so throw exception

       if (empty()) {

           throw new EmptyStackException();

       }

       // if stack is not empty, check that the ref of the top element of the stack is

       // null or not. If so, then it is sure that the stack contains only one element,

       // and hence item is set to null

       else if (topOfStackRef == null) {

           item = null;

           // if the topOfStackRef is not null, then it contains more than one element, adn

           // the ref of element next to the top is saved in a separate Node instance

           // nextToTop

       } else if (topOfStackRef != null) {

           Node<E> nextToTop = topOfStackRef.next;

           item = nextToTop.data;

       }

       //the value in item gets returned if no exception occurs;

       return item;

   }

   // End of class content for Students.

}

/* </listing> */

----ListQueue2----

import java.util.Queue;

import java.util.NoSuchElementException;

public class ListQueue2<E> {

// Data Fields

/** Reference to front of queue. */

private Node<E> front;

/** Reference to rear of queue. */

private Node<E> rear;

/** Size of queue. */

private int size;

// Insert inner class Node<E> here (see Listing 2.1)

/** A Node is the building block for a single-linked list. */

private static class Node<E> {

// Data Fields

/** The reference to the data. */

private E data;

/** The reference to the next node. */

private Node<E> next;

// Constructors

/**

   * Creates a new node with a null next field.

   * @param dataItem The data stored

   */

private Node(E dataItem) {

data = dataItem;

next = null;

}

/**

   * Creates a new node that references another node.

   * @param dataItem The data stored

   * @param nodeRef The node referenced by new node

   */

private Node(E dataItem, Node<E> nodeRef) {

data = dataItem;

next = nodeRef;

}

} //end class Node

// Methods

/**

   * Insert an item at the rear of the queue.

   * @post item is added to the rear of the queue.

   * @param item The element to add

   * @return true (always successful)

   */

public boolean offer(E item) {

// Check for empty queue.

if (front == null) {

rear = new Node<E>(item);

front = rear;

} else {

// Allocate a new node at end, store item in it, and

// link it to old end of queue.

rear.next = new Node<E>(item);

rear = rear.next;

}

size++;

return true;

}

/**

   * Remove the entry at the front of the queue and return it

   * if the queue is not empty.

   * @post front references item that was second in the queue.

   * @return The item removed if successful, or null if not

   */

public E poll() {

E item = peek(); // Retrieve item at front.

if (item == null) {

return null;

}

// Remove item at front.

front = front.next;

size--;

return item; // Return data at front of queue.

}

/**

   * Return the item at the front of the queue without removing it.

   * @return The item at the front of the queue if successful;

   * return null if the queue is empty

   */

public E peek() {

if (size == 0) {

return null;

} else {

return front.data;

}

}   

public int size() {

return size;   

}   

public boolean empty() {

       if (size == 0) {

           return true;

       }

       else

           return false;

}

}

/*</listing>*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote