Objectives: The focus of this assignment is to create and use a recursive method
ID: 3862492 • Letter: O
Question
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. I just need the bolded methods.
Program Description:
This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm.
A total of seven classes are required.
Employee (From previous assignment)
HourlyEmployee (From previous assignment)
SalaryEmployee (From previous assignment)
CommissionEmployee (From previous assignment)
EmployeeManager (Altered from previous assignment)
EmployeeDriver (Altered from previous assignment, provided)
InvalidCharacterException (provided)
Required changes to Employee Manager in bold
EmployeeManager
EmployeeManager
- employees : Employee[]
- employeeMax : final int = 10
-currentEmployees : int
<<constructor>> EmployeeManager
+ addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double)
+ removeEmployee( index : int)
+ listAll()
+ listHourly()
+ listSalary()
+ listCommision()
+ resetWeek()
+ calculatePayout() : double
+ getIndex( empNum : int ) : int
+ annualRaises()
+ holidayBonuses() : double
+ increaseHours( index : int, amount : double)
+ increaseSales( index : int, amount : double)
+ findAllBySubstring(find : String) : Employee[]
- RabinKarp(name : String, find : String) : int
- stringHash(s : String) : int
- charNumericValue(c : char) : int
- RabinKarpHashes(s : String, hashes : int[], pos : int, length : int) : int
- linearSearchRecursive(nameHashes : int[], findHash : int, pos : int) : int
Data Members
- Employee[] employees – Collection of Employee objects
- final int employeeMax = 10 – Maximum number of Employees allowed
- int currentEmployees – Current number of Employees in collection
Methods
public EmployeeManager()
Constructor, creates the Employee array, sets currentEmployees to 0.
public void addEmployee(int, String, String, char, char, int, Boolean, double)
Takes an int representing the type of Employee to be added (1 – Hourly, 2 – Salary, 3 – Commission) as well as the required data to create that Employee. If one of these values is not passed output the line, “Invalid Employee Type, None Added”, and exit the method. If an Employee with the given Employee Number already exists do not add the Employee and output the line, “Duplicate Not Added”, and exit the method. If the array is at maximum capacity do not add the new Employee, and output the line, "Cannot add more Employees".
public void removeEmployee(int)
Removes an Employee located at the given index from the Employee array.
public void listAll()
Lists all the current Employees. Outputs there are none if there are none.
public void listHourly()
Lists all the current HourlyEmployees. Outputs there are none if there are none.
public void listSalary()
Lists all the current SalaryEmployees. Outputs there are none if there are none.
public void listCommission()
Lists all the current CommissionEmployees. Outputs there are none if there are none.
public void resetWeek()
Resets the week for all Employees.
public double calculatePayout()
Returns the total weekly payout for all Employees.
public int getIndex(int)
Given an Employee Number, returns the index of that Employee in the array, if the Employee doesn’t exist retuns -1.
public void annualRaises()
Applies annual raise to all current Employees.
public double holidayBonuses()
Outputs and returns the total holiday bonus of all Employees.
public void increaseHours(int, double)
Increase the hours worked of the Employee at the given index by the given double amount.
public void increaseSales(int, double)
Increase the sales of the Employee at the given index by the given double amount.
public Employee[] findAllBySubstring(String find)
This method will return an array of all the Employees in the EmployeeManager that contain the substring passed. Create a new Employee array with the size of the number of current Employees. For every Employee call upon the RabinKarp method giving the search string as the concatenation of that Employee’s first and last name (no spaces). If the substring is found in the Employee add that Employee to the new array. After all have been checked, return the array.
private int charNumericValue(char c)
Given a character, returns the numeric value of the character, starting with A – 0 up to Z – 25. This should treat upper and lower case the same; that is passing it ‘A’ will return 0, passing it ‘a’ will also return 0. If a letter is not passed this method should create and throw an InvalidCharacterException as provided.
private int stringHash(String s)
Given a string, return the hash value of the entire String. Use a base 26 number system to create the hash as described in class. This will be needed only to find the hash of the substring that is being searched for and the base case for finding all substring hashes in the search string.
private int RabinKarpHashes(String s, int[] hashes, int pos, int length)
Finds the hash values of all substrings of size length in the String s, starting at index pos and down. These values are stored in the passed hashes array. This method must be recursive, using the technique as described in the Rabin-Karp lecture.
private int linearSearchRecursive(int[] data, int key, int pos)
This is a recursive linear search. Return the position of key in the data array, or -1 if it is not present. This method must be recursive.
private int RabinKarp(String name, String find)
Does the preprocessing of finding the hash for the substring, find using the stringHash method and the hashes of substrings in the search string using RabinKarpHashes method. Calls upon linearSearchRecursive to determine if the substring hash is in the collection of hashes and returns the result.
Other Notes:
· Classes from the previous assignment will retain the same package structure. The new Exception is declared to be in its own package called “exceptions”.
· Your EmployeeManager must import the InvalidCharacterException in order to use it
· Compile using: javac –d . *.java
EmployeeManager
- employees : Employee[]
- employeeMax : final int = 10
-currentEmployees : int
<<constructor>> EmployeeManager
+ addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double)
+ removeEmployee( index : int)
+ listAll()
+ listHourly()
+ listSalary()
+ listCommision()
+ resetWeek()
+ calculatePayout() : double
+ getIndex( empNum : int ) : int
+ annualRaises()
+ holidayBonuses() : double
+ increaseHours( index : int, amount : double)
+ increaseSales( index : int, amount : double)
+ findAllBySubstring(find : String) : Employee[]
- RabinKarp(name : String, find : String) : int
- stringHash(s : String) : int
- charNumericValue(c : char) : int
- RabinKarpHashes(s : String, hashes : int[], pos : int, length : int) : int
- linearSearchRecursive(nameHashes : int[], findHash : int, pos : int) : int
Explanation / Answer
note: due to limited character i cant able to post all files
EmployeeManager.java
import employeeType.subTypes.HourlyEmployee;
import employeeType.subTypes.SalaryEmployee;
import employeeType.subTypes.CommissionEmployee;
import employeeType.employee.Employee;
import dataStructures.ArrayList;
import dataStructures.LinkedList;
import dataStructures.Queue;
import exceptions.EmptyListException;
import exceptions.InvalidCharacterException;
import exceptions.InvalidEmployeeNumberException;
import exceptions.InvalidSizeException;
import exceptions.MaximumCapacityException;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Formatter;
import java.util.FormatterClosedException;
public class EmployeeManager
{
private ArrayList<Employee> employees;
private final int employeeMax = 10;
private LinkedList<Employee> hourlyList;
private LinkedList<Employee> salaryList;
private LinkedList<Employee> commissionList;
private Queue<Employee> vacationRequests;
public EmployeeManager()
{
try
{
employees = new ArrayList<Employee>(3);
}
catch (InvalidSizeException E)
{
try
{
employees = new ArrayList<Employee>(employeeMax);
}
catch (InvalidSizeException e)
{
}
}
hourlyList = new LinkedList<Employee>();
salaryList = new LinkedList<Employee>();
commissionList = new LinkedList<Employee>();
vacationRequests = new Queue<Employee>();
}
public String addEmployee(int type, String fn, String ln, char m, char g, int en, boolean ft, double amount) throws InvalidEmployeeNumberException, MaximumCapacityException
{
String s = "";
for (int x = 0; x < employees.lengthIs(); x++)
{
if (employees.getItem(x).getEmployeeNumber() == en)
{
System.out.print("error 12");
s = "error";
return s;
}
}
if (type == 1)
{
HourlyEmployee E = new HourlyEmployee (fn,ln,m,g,en,ft,amount);
employees.addItem(E);
hourlyList.insertAtBack(E);
s = "hourly";
}
if (type == 2)
{
SalaryEmployee E = new SalaryEmployee (fn,ln,m,g,en,ft,amount);
employees.addItem(E);
salaryList.insertAtBack(E);
s = "salary";
}
if (type == 3)
{
CommissionEmployee E = new CommissionEmployee (fn,ln,m,g,en,ft,amount);
employees.addItem(E);
commissionList.insertAtBack(E);
s = "commission";
}
return s;
}
public void removeEmployee(int index)
{
Employee temp = employees.getItem(index);
if (employees.getItem(index) instanceof HourlyEmployee)
{
employees.removeItem(index);
hourlyList.findAndRemove(temp);
/*
if(index > 0)
{
hourlyList.removeFromBack();
}
else
{
hourlyList.removeFromFront();
}
*/
}
else if (employees.getItem(index) instanceof SalaryEmployee)
{
employees.removeItem(index);
salaryList.findAndRemove(temp);
/*
if(index > 0)
{
salaryList.removeFromBack();
}
else
{
salaryList.removeFromFront();
}
*/
}
else if (employees.getItem(index) instanceof CommissionEmployee)
{
employees.removeItem(index);
commissionList.findAndRemove(temp);
/*if(index > 0)
{
commissionList.removeFromBack();
}
else
{
commissionList.removeFromFront();
}*/
}
}
public String listAll()
{
String x = "";
if(employees.isEmpty() == true)
{
x = "No Employees";
}
for(int y = 0; y < employees.lengthIs(); y++)
{
x += employees.getItem(y).toString() + " ";
}
return x;
}
public String listHourly()
{
String a = "";
if (hourlyList.isEmpty() == true)
{
a = "No Hourly Employees";
}
for (int x = 0; x < hourlyList.lengthIs(); x++)
{
a += hourlyList.getItem(x).toString() + " ";
}
return a;
}
public String listSalary()
{
String b = "";
if(salaryList.isEmpty() == true)
{
b = "No Salary Employees";
}
else
{
for (int x = 0; x < salaryList.lengthIs(); x++)
{
b += salaryList.getItem(x).toString() + " ";
}
}
return b;
}
public String listCommission()
{
String c = "";
if(commissionList.isEmpty() == true)
{
c = "No Commission Employees";
}
for (int x = 0; x < commissionList.lengthIs(); x++)
{
c += commissionList.getItem(x).toString() + " ";
}
return c;
}
public void resetWeek()
{
for (int a = 0; a < employees.lengthIs(); a++)
{
employees.getItem(a).resetWeek();
}
}
public double calculatePayout()
{
double payout = 0;
for (int a = 0; a < employees.lengthIs(); a++)
{
payout += employees.getItem(a).calculateWeeklyPay();
System.out.printf(" %f ", payout);
}
return payout;
}
public int getIndex(int empNum)
{
for (int a = 0; a < employees.lengthIs(); a++)
if (empNum == employees.getItem(a).getEmployeeNumber())
return a;
return -1;
}
public void annualRaises()
{
for (int a = 0; a < employees.lengthIs(); a++)
{
employees.getItem(a).annualRaise();
}
}
public double holidayBonuses()
{
double payout = 0;
for (int a = 0; a < employees.lengthIs(); a++)
{
payout += employees.getItem(a).holidayBonus();
//System.out.print (employees.getItem(a).toString());
double b = employees.getItem(a).holidayBonus();
System.out.printf ("Bonus ammount: %.2f", b);
System.out.print (" ");
}
return payout;
}
public boolean increaseHours(int index, double amount)
{
if (employees.getItem(index) instanceof HourlyEmployee)
{
((HourlyEmployee)employees.getItem(index)).increaseHours(amount);
}
else
return false;
return true;
}
public boolean increaseSales(int index, double amount)
{
if (employees.getItem(index) instanceof CommissionEmployee)
{
((CommissionEmployee)employees.getItem(index)).increaseSales(amount);
}
else
{
return false;
}
return true;
}
public ArrayList<Employee> findAllBySubstring(String find)
{
ArrayList<Employee> ken = new ArrayList<Employee>();
for (int a = 0; a < employees.lengthIs(); a++)
{
if((RabinKarp((employees.getItem(a).getFirstName() + employees.getItem(a).getLastName()), find)) != -1)
{
try
{
ken.addItem(employees.getItem(a));
System.out.print(employees.getItem(a).toString() + " ");
}
catch (MaximumCapacityException e)
{
System.out.print("too large");
}
}
}
return ken;
}
private int RabinKarp(String name, String find)
{
if (find.length() > name.length())
return -1;
int findHash = stringHash(find);
int [] nameHash = new int [name.length() - find.length() + 1];
String sub = name.substring(0, find.length());
nameHash[0] = stringHash(sub);
RabinKarpHashes(name, nameHash, name.length() - find.length(), find.length());
int x = linearSearchRecursive(nameHash, findHash, name.length() - find.length());
return x;
}
private int stringHash (String s)
{
int hash = 0;
for(int i = 0; i < s.length(); i++)
{
hash += charNumericValue(s.charAt(i)) * Math.pow(26, s.length() - i - 1);
}
return hash;
}
private int charNumericValue(char c) throws InvalidCharacterException
{
switch (c)
{
case 'a': case 'A':
return 0;
case 'b': case 'B':
return 1;
case 'c': case 'C':
return 2;
case 'd': case 'D':
return 3;
case 'e': case 'E':
return 4;
case 'f': case 'F':
return 5;
case 'g': case 'G':
return 6;
case 'h': case 'H':
return 7;
case 'i': case 'I':
return 8;
case 'j': case 'J':
return 9;
case 'k': case 'K':
return 10;
case 'l': case 'L':
return 11;
case 'm': case 'M':
return 12;
case 'n': case 'N':
return 13;
case 'o': case 'O':
return 14;
case 'p': case 'P':
return 15;
case 'q': case 'Q':
return 16;
case 'r': case 'R':
return 17;
case 's': case 'S':
return 18;
case 't': case 'T':
return 19;
case 'u': case 'U':
return 20;
case 'v': case 'V':
return 21;
case 'w': case 'W':
return 22;
case 'x': case 'X':
return 23;
case 'y': case 'Y':
return 24;
case 'z': case 'Z':
return 25;
}
throw new InvalidCharacterException(c);
}
private int RabinKarpHashes (String s, int[] hashes, int pos, int length)
{
if (pos == 0)
{
hashes[pos] = stringHash(s.substring(pos, pos + length));
}
if (pos > 0)
{
hashes[pos] = 26 * (RabinKarpHashes(s,hashes,pos-1, length) - charNumericValue(s.charAt(pos-1)) * (int)Math.pow(26,length-1)) + charNumericValue(s.charAt(pos+length-1));
}
return hashes[pos];
}
private int linearSearchRecursive(int[] data, int key, int pos)
{
if (pos < 0)
{
return -1;
}
if (key == data[pos])
{
return pos;
}
return linearSearchRecursive(data, key, pos-1);
}
public void sort()
{
hourlyList.sort();
salaryList.sort();
commissionList.sort();
employees.sort();
}
public boolean addRequest(int empNum)
{
if (empNum > 10000 && empNum < 99999)
{
vacationRequests.enqueue(employees.getItem(getIndex(empNum)));
System.out.println(empNum);
return true;
}
else
return false;
}
public Employee viewNextRequest()
{
return vacationRequests.peek();
}
public Employee grantNextRequest()
{
if (vacationRequests != null)
{
return vacationRequests.dequeue();
}
else
return null;
}
public String outputRequests()
{
if (vacationRequests != null)
return vacationRequests.toString();
else
return "No vacation requests";
}
@SuppressWarnings("resource")
public boolean loadEmployees(String employeeFile, String requestFile)
{
FileInputStream file, file1;
ObjectInputStream inout = null ;
Scanner in;
Queue<Employee> q = null;
int emp = 0;
////clear
employees.clear();
hourlyList.clear();
salaryList.clear();
commissionList.clear();
vacationRequests.clear();
/////ending clear
////employee
try
{
inout = new ObjectInputStream(new FileInputStream(employeeFile));
while(true)
{
Employee ee = (Employee)(inout.readObject());
//System.out.println(ee);
employees.addItem(ee);
if (ee instanceof HourlyEmployee)
{
hourlyList.insertAtBack(ee);
}
if (ee instanceof SalaryEmployee)
{
salaryList.insertAtBack(ee);
}
if (ee instanceof CommissionEmployee)
{
commissionList.insertAtBack(ee);
}
}
}
catch (FileNotFoundException fnfe)
{
System.out.println("Error");
}
catch (EOFException eofee)
{
try
{
inout.close();
}
catch (IOException ioe)
{
System.err.println("etr12344");
System.exit(0);
}
}
catch (IOException ioe)
{
System.err.println("etr");
}
catch (MaximumCapacityException mce)
{
System.out.print("Maximum Capacity Reached");
return true;
}
catch (ClassNotFoundException cnfe)
{
System.out.print("err");
}
if (inout == null)
{
return false;
}
try
{
inout.close();
}
catch (IOException e)
{
System.exit(0);
e.printStackTrace();
}
//
try
{
file = new FileInputStream(employeeFile);
}
catch (IOException a)
{
System.err.println("err");
return false;
}
try
{
if (file != null)
{
file.close();
}
}
catch (IOException IOE)
{
System.err.println("error closing file");
System.exit(1);
}
try
{
in = new Scanner(new File(requestFile));
}
catch (FileNotFoundException fnfe)
{
System.err.println("err");
return false;
}
while (true)
{
try
{
emp = in.nextInt();
vacationRequests.enqueue(employees.getItem(getIndex(emp)));
}
catch (NoSuchElementException nsee)
{
break;
}
}
try
{
in.close();
}
catch (IllegalStateException iseee)
{
System.err.println("Scanner closed error.");
System.exit(1);
}
//
try
{
file1 = new FileInputStream(requestFile);
}
catch (IOException ioe)
{
System.out.print("err");
return false;
}
try
{
if (file1 != null)
{
file1.close();
}
}
catch (IOException ioe)
{
System.out.print("err");
System.exit(1);
}
return true;
}
@SuppressWarnings("resource")
public boolean saveEmployees(String employeeFile, String requestFile)
{
ObjectOutputStream output;
Formatter put = null;
////employeefile
try
{
output = new ObjectOutputStream(new FileOutputStream(employeeFile));
}
catch (IOException a)
{
System.out.println("error66");
System.exit(1);
return false;
}
for (int x = 0; x < employees.lengthIs(); x++)
{
try
{
output.writeObject(employees.getItem(x));
}
catch (IOException a)
{
System.out.print("Error77");
return false;
}
}
try
{
output.close();
}
catch (IOException e)
{
System.exit(0);
e.printStackTrace();
}
////requestfile put in while loop
try
{
put = new Formatter(requestFile);
while(vacationRequests.isEmpty() == false)
{
put.format("%d ", vacationRequests.dequeue().getEmployeeNumber());
}
}
catch (FormatterClosedException fce)
{
System.out.println("error99");
return false;
}
catch (FileNotFoundException fnfee)
{
System.out.print("err10");
}
if (put != null)
{
put.close();
}
return true;
}
public boolean processUpdates(String fileName)
{
int emp = 0;
double requesT = 0.0;
Scanner input;
try
{
input = new Scanner(new File(fileName));
}
catch (FileNotFoundException fnfe)
{
System.out.print("Error, file not found");
System.exit(1);
return false;
}
while (true)
{
try
{
emp = input.nextInt();
requesT = input.nextDouble();
}
catch(NoSuchElementException nsee)
{
break;
}
catch(IllegalStateException ISE)
{
System.err.println("Scanner closed error.");
System.exit(1);
}
int temp;
temp = getIndex(emp);
if (employees.getItem(temp) instanceof HourlyEmployee)
{
increaseHours(emp, requesT);
}
if (employees.getItem(temp) instanceof SalaryEmployee)
{
System.out.print("Cannot process update.");
}
if (employees.getItem(temp) instanceof CommissionEmployee)
{
increaseSales(emp,requesT);
}
if (temp == -1)
{
System.out.printf("Cannot process update %d is an invalid employee number.", emp);
}
}
try
{
if (input != null)
{
input.close();
}
}
catch(IllegalStateException ISE)
{
System.err.println("Scanner closed error.");
System.exit(1);
}
return true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.