Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 as a linked list.

public class LinkedStack implements StackInt {

// Insert inner class Node here (see Listing 2.1)

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

private static class Node {

// Data Fields\

/** The reference to the data. */

private E data;

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

private Node 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 nodeRef) {

data = dataItem;

next = nodeRef;

}

}

// End of inner class Node

// Data Fields

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

private Node 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(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 nextToTop = topOfStackRef.next;

item = nextToTop.data;

}

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

return item;

}

// End of class content for Students.

}

/* */

----ListQueue2----

import java.util.Queue;

import java.util.NoSuchElementException;

public class ListQueue2 {

// Data Fields

/** Reference to front of queue. */

private Node front;

/** Reference to rear of queue. */

private Node rear;

/** Size of queue. */

private int size;

// Insert inner class Node here (see Listing 2.1)

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

private static class Node {

// Data Fields

/** The reference to the data. */

private E data;

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

private Node 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 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(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(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;

}

}

/**/

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

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

1. PricewaterhouseCoopers

Answered: 1 week ago

Question

3. SCC Soft Computer

Answered: 1 week ago