Question
PART1: Objectives: After finishing the lab4 part 1, you can: -handle the stack structure when applying push and pop operations to add or fetch and
PART1: Objectives: After finishing the lab4 part 1, you can:
-handle the stack structure when applying push and pop operations to add or fetch and delete the nodes
-handle the queue structure when applying enque and deque operations to add or fetch and delete the nodes. Requirement Statement:
Develop the pseudo-code and draw the UML of data type classes of the project named Create the project SP2017LAB4_PART1 that using the Restricted Data Structure (EmployeeStack or EmployeeQueue) and the driver class should be SP2017LAB4_RestrictedStructureDemoWithEmployee) The EmployeeStack or EmployeeQueue will keep employees or employees with title. The application will allow users to select to work with either Stack or Queue. When finishing with one, the application allow users to continue to select and work with the other structure until users want to exit
For Stack or Queue, do the following steps in order:
1. Define the structure
2. Allow users to insert 5 nodes (either Employee or TitleEmployee)
3. Show all 5 employees/titleEmployees that are inserted
4. Delete 2 nodes
5. Display the node at the top of the Stack or at the front of the Queue The node in this lab is either Employee_LastName object or TitleEmployee_LastName object.
-class Employee_yourLastname that maintains employee id, last name, first name, department and salary. The class Employee should have no-argument constructor, parameter constructor, mutator methods, accessor method and method toString to display the information of one employee
-class TitleEmployee_yourLastName that inherits from class Employee_yourLastName and has two more fields, title (supervisor, manager, etc) and bonus. The method toString will print the same information of one employee plus the title and the bonus
(UML) EmployeeStack -data:Employee[ ] -top: int -size: int +EmployeeStack() +EmployeeStack(int n) +push(Employee newNode): boolean +pop (): Employee +peek(): Employee +showAll(): void
PART 2: Evaluate the expression OBJECTIVES After finish this lab you can:
-Apply the Stack to evaluate the infixed notation expression
REQUIREMENTS: Read an infixed expression from the keyboard or a file, for example, (1 + 2) * 4 3
(UML) EmployeeQueue -data:Employee[ ] -size: int -numOfNodes: int -front: int -rear: int +EmployeeQueue() +EmployeeQueue(int n) +enque(Employee newNode): boolean +deque(): Employee +peek(): Employee +showAll(): void
Use two Stack structures to evaluate the expression (see the Instruction below) and print out the result in the following format, for example: (1 + 2) * 4 3 = 9
WHAT WE NEED TO KNOW TO DO THE PROGRAM -Review the generic class -Review about Stack structure
HOW TO DO THE LAB
-Write a class GenericStack that can apply for any data type. You can use the code of GenericStack in the book for your reference (You do not need GenericNode and deepCopy for this lab)
-Write a controlling class (driver class) named SP2017LAB4_InfixedExpressionEvaluation_yourLastName that has at least two user defined functions, the method oneOperationProcess and the emthod oneExpressionProcess
User Defined function oneOperatorProcess This method accepts two paramenters, Integer Stack and Character Stack In the body of the method 0neoperatorProcess
-pop the top operator in the stack of operators
-pop the two operands on the top of stack of operands
-Depend on the operator, calculate the result and push the result back to the stack of operand. The result is calculated with operand1, operand2 and operator that are popped from stack of operands and from stack of operators
User Defined function oneExpressionProcess that hold all the code to evaluate one expression METHOD main
-In the method main: display the menu to allow users can choose where the input expression is from, the keyboard or a file
-Define one Integer Stack that is used to store the integer operands
-Define one Character Stack that is used to store the operators ( + - * / or )
-Read one infixed expression from the keyboard or a file
-Using StringTokenizer or split of String to split input expression into the tokens (operands and operators) and store them to stack of operands and stact of operators by applying the following rules:
phase1: Scanning expression (read expression)
the program will scan the expression from left to right to extract operands, operators and the parenthesis
if the extracted item is a number, push it to the stackForOperands
if the extracted item is a + or operator:
- check if the stackForOperators is empty, push it into the stack
- if the stackForOperators is not empty, process all the operators (+, -, *, / ) in the stackForOperators from top one by one until there is no operator (+, -, * or /) at top then push the extracted operator to stackForOperators
if the extracted item is a * or / operator:
- check if the stackForOperators is empty, push it into the stack
- if the stackForOperators is not empty, process all the operators (*, / ) in the stackForOperators from top one by one until there is no operator (* or /) at top then push the extracted operator to stackForOperators
if the extracted item is an open parenthesis (, push it to stackForOperators
if the extracted item is an close parenthesis ), process all operators in the stackForOperators from the top until top is an open parenthesis (; then pop the open parenthesis ( off from the stackForOperators
phase2: Clearing stack
Process all the operators left in the stackForOperators from top until stackForOperators is empty
Display the result as the required format: The final result will be the last number in the stackForOpereands
//This is the code for part 1: //(I need help with Part 2)
public class SP2017LAB4_RestrictedStructureDemoWithEmployee {
public static void main(String args[]) { /* * Creating a stack and a queue of size 5 */ EmployeeStack stack = new EmployeeStack(5); EmployeeQueue queue = new EmployeeQueue(5);
Employee emp1 = new Employee(1, "Odin", "Zubaeyr", "IoT", 1300000); Employee emp2 = new Employee(2, "Odin", "Azhar", "WW-Transport", 110000); Employee emp3 = new Employee(3, "Wright", "Sam", "IWM", 60000); TitleEmployee emp4 = new TitleEmployee(1, "Odin", "Bilal", "IoT", 1000000, "Manager", 100000); TitleEmployee emp5 = new TitleEmployee(1, "Nelofer", "Ayesha", "IoT", 1200000, "CEO", 100000);
/* Adding employees to stack and queue */ stack.push(emp1); stack.push(emp2); stack.push(emp3); stack.push(emp4); stack.push(emp5);
queue.enQueue(emp1); queue.enQueue(emp2); queue.enQueue(emp3); queue.enQueue(emp4); queue.enQueue(emp5);
queue.printAll(); stack.printAll(); /* Deleting two nodes from eacch */ queue.deQueue(); queue.deQueue(); stack.pop(); stack.pop();
System.out.println("Node at top of the stack is, "+stack.peek()); System.out.println("Node at front of the queue is," + queue.peek()); } }
class Employee {
protected int employeeID; protected String lastName; protected String firstName; protected String department; protected double salary;
public Employee() { super(); } public Employee(int employeeID, String lastName, String firstName, String department, double salary) { super(); this.employeeID = employeeID; this.lastName = lastName; this.firstName = firstName; this.department = department; this.salary = salary; } public int getEmployeeID() { return employeeID; }
public void setEmployeeID(int employeeID) { this.employeeID = employeeID; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Employee [employeeID=" + employeeID + ", lastName=" + lastName + ", firstName=" + firstName + ", department=" + department + ", salary=" + salary + "]"; } }
class TitleEmployee extends Employee {
protected String title; protected double bonus;
public TitleEmployee() { super(); } public TitleEmployee(int employeeID, String lastName, String firstName, String department, double salary, String title, double bonus) { super(employeeID, lastName, firstName, department, salary); this.title = title; this.bonus = bonus; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public String toString() { return "TitleEmployee [title=" + title + ", bonus=" + bonus + ", employeeID=" + employeeID + ", lastName=" + lastName + ", firstName=" + firstName + ", department=" + department + ", salary=" + salary + "]"; } } class EmployeeStack { protected int size; protected int top; protected Employee[] data;
public EmployeeStack() { this.top = -1; this.size = 1000; data = new Employee[this.size]; }
public EmployeeStack(int size) { this.top = -1; this.size = size; data = new Employee[this.size]; }
public boolean push(Employee newNode) { if (top + 1 >= size) { return false; } top++; data[top] = newNode; return true; }
public Employee pop() { if (top
public Employee peek() { if (top
public void printAll() { System.out.println("The contents of the stack are : "); for (int i=0; i
protected int size; protected Employee[] data; protected int front; protected int rear; protected int numOfNodes; public EmployeeQueue() { this.rear = 0; this.front = 0; this.size = 1000; this.numOfNodes = 0; data = new Employee[this.size]; } public EmployeeQueue(int size) { this.rear = 0; this.front = 0; this.size = size; this.numOfNodes = 0; data = new Employee[this.size]; } public boolean enQueue(Employee employee) { if (isQueueFull()) { return false; } data[rear] = employee; rear = (rear+1)%size; numOfNodes++; return true; }
public Employee deQueue() { if (isEmpty()) { return null; } Employee employee = data[front]; data[front] = null; front = (front+1)%size; numOfNodes--; return employee; } public Employee peek() { if (isEmpty()) { return null; } return data[front]; } public void printAll() { System.out.println("The contents of the queue are : "); if (rear > front) { for (int i=front; i public boolean isQueueFull() { int difference = rear - front; if (difference == -1 || difference == (size-1)) { return true; } return false; } public boolean isEmpty() { if (rear == front) { return true; } return false; } } (I need help with Part 2)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started