Part A: Using the templatized Stack class developed in class last week as a mode
ID: 3662326 • Letter: P
Question
Part A: Using the templatized Stack class developed in class last week as a model, create a templatized Queue class. The methods should at least include: public boolean isEmpty() public void enqueue(T data) public T dequeue() An attempt to dequeue from an empty queue should throw a QueueException Part B: You are in charge of hiring and firing workers for a company. The rules are the following: 1) If you are asked to fire someody at a time when the firm has no employees, you should notify your supervisor. 2) If you are asked to fire sombody 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 hired, then the person who applied earliest is to be hired. 6) If there is nobody available for hiring, then you must notify your supervisor. Write a computer program to model this situation. 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 and a 9 digit i.d. number for an applicant, (store both in Strings) 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 i.d. number, and appropriately update the internal data structures. "Fire" should choose the appropriate person to fire, print his or her name and i.d, and appropriately update the internal data structures. "Quit" should exit the program. Hints: 1) You will want to create a simple class called Person that hold two strings, one for the name and one for the social security number. 2) You should instantiate two Stack<Person>'s and one Queue<Person>. With these you should be able to write the program. Sample dialog: Action (1 to accept, 2 to hire, 3 to fire):3 Memo to supervisor: There is nobody to fire Action (1 to accept, 2 to hire, 3 to fire):1 What is the applicants name: Fred Flintstone What is Fred Flintstone's ID:111111111 Action (1 to accept, 2 to hire, 3 to fire):1 What is the applicants name: Barney Rubble What is Fred Flintstone's ID:222222222 Action (1 to accept, 2 to hire, 3 to fire):1 What is the applicants name: Bambam Flintstone What is Fred Flintstone's ID:123456789 Action (1 to accept, 2 to hire, 3 to fire):2 Fred Flintstone 111111111 hired Action (1 to accept, 2 to hire, 3 to fire):2 Barney Rubble 222222222 hired Action (1 to accept, 2 to hire, 3 to fire):3 Barney Rubble 222222222 fired Action (1 to accept, 2 to hire, 3 to fire):2 Barney Rubble 222222222 hired Action (1 to accept, 2 to hire, 3 to fire):2 Bambam Flintstone 123456789 hired Action (1 to accept, 2 to hire, 3 to fire):2 Memo to Supervisor:There is nobody to hire Question to answer: How could you save the current state of the data before you quit, and read it back in when you start again? Does saving a linked data structure to disk present any particular problem?
Explanation / Answer
import java.uti.*;
import java.io.*;
public interface Queue
{
void enqueue(Object item);
Object dequeue();
int size();
Boolean isEmpty();
}
public class nQueue implements Queue
{
private class Node
{
public Object data;
public Node nextN;
public Node(Object data, Node nextN)
{
this.data = data;
this.nextN = nextN;
}
Node header = null;
Node tailer = null;
public void enqueue(Object item)
{
Node newN= new Node(item,null);
if(isEmpty())
{
header= newN;
}
else
{
tailer.next= newN;
}
tailer = newN;
}
public Object dequeue()
{
if(isEmpty())
{
throw new QueueException();
}
Object item =header.data;
if(header == tailer)
{
tailer= null;
}
header= head.next;
return item;
}
public Boolean isEmpty()
{
return header == null;
}
public int size()
{
int counter=0;
for(Node node=header; node != null; node = node.next;)
{
counter= counter+1;
}
return counter;
}
}
}
Part B:
import java.io.*;
import java.util.*;
class Person
{
public String name;
public String ssn;
public Person (String Name, String socialNum)
{
this.name = Name;
this.ssn = socialNum;
}
}
class App
{
Scanner s= new Scanner(System.in);
private Queue<Person> applicant = new Queue<Person>();
public Stack<Person> hire =new Stack<Person>();
public Stack<person> fire = new Stack<Person>();
public void hire( ) throws StackException
{
if(!applicant.isEmpty())
{
Person hireApplicant = hire.push(applicant.dequeue());
System.out.println("The hired person is:"+hireApplicant.name+"with ID is:"+hireApplicant.ssn);
}
else if(!fire.isEmpty())
{
Person firedEmp= applicant.dequeue();
System.out.println("The hired person is:"+firedEmp.name+"with ID is:"+firedEmp.ssn);
}
else
System.out.println("No one can be hired");
}
public void fire() throws StackException
{
if(!hire.isEmpty())
{
Person tobeFire = fire.push(hire.pop());
System.out.println("The fired person is:"+tobeFire.name +"with id:"+ tobeFire.ssn);
fire.push(hire.pop());
}
else
System.out.println("no one can be fired as there are no employees left ");
}
public void AppyForJob() throws QueueException
{
System.out.println("please enter Applicants name");
String applicantName= s.nextLine();
System.out.println("please enter Applicants Social security number");
String applicantSSN= s.nextLine();
Person p =new Person(applicantName, applicantSSN);
applicant.enqueue(p);
}
}
public class Test
{
public static void main(String [] args) throws QueueException, StackException
{
Scanner s= new Scanner(System.in);
App a= new App();
boolean testing=true;
while(testing)
{
System.out.println("press 1 for Accept Applicant");
System.out.println("Press 2 for hire");
System.out.println("Press 3 for fire");
System.out.println("Press 4 for Quit");
int todo = s.nextInt();
if( todo ==1)
{
a.ApplyForJob();
}
else if( todo ==2)
{
a.hire();
}
else if(todo==3)
{
a.fire();
}
else if(todo==4)
{
System.exit(0);
}
else
System.out.println("please enter correct choice again in range 1-4");
}
}
}