I need help with this two methods in java. Here are the guidelines. The methods
ID: 3924390 • Letter: I
Question
I need help with this two methods in java. Here are the guidelines. The methods private int stringHash(String s) and private int RabinKarpHashes(String s, int[] hashes, int pos, int length).
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
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.
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
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
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;
@SuppressWarnings("unchecked")
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();
salaryList = new LinkedList();
commissionList = new LinkedList();
vacationRequests = new Queue();
}
// Method: addEmployee
// Parameters: type,fn, ln, m, g, en, ft, amount
// Returns: none
// Partners: none
// Method uses variables to decide type of employee
public void addEmployee(int type, String fn, String ln, char m, char g, int en, boolean ft, double amount) throws InvalidEmployeeNumberException
{
if (type == 1)
{
HourlyEmployee E = new HourlyEmployee (fn,ln,m,g,en,ft,amount);
try
{
employees.addItem(E);
hourlyList.insertAtBack(E);
}
catch (MaximumCapacityException e)
{
System.out.print("too large");
}
}
if (type == 2)
{
SalaryEmployee E = new SalaryEmployee (fn,ln,m,g,en,ft,amount);
try
{
employees.addItem(E);
salaryList.insertAtBack(E);
}
catch (MaximumCapacityException e)
{
System.out.print("too large");
}
}
if (type == 3)
{
CommissionEmployee E = new CommissionEmployee (fn,ln,m,g,en,ft,amount);
try
{
employees.addItem(E);
commissionList.insertAtBack(E);
}
catch (MaximumCapacityException e)
{
System.out.print("too large");
}
}
}
// Method: removeEmployee
// Parameters: index
// Returns: none
// Partners: CLC
// Method removes a certain employee
public void removeEmployee(int index)
{
if(index != -1)
employees.removeItem(index);
}
// Method: listAll
// Parameters: none
// Returns: none
// Partners: none
// Method lists all employees
public String listAll()
{
String x = "";
if(employees.isEmpty() == true)
return "No Employees";
else
return x + employees.toString();
}
// Method: listHourly
// Parameters: none
// Returns: none
// Partners: none
// Method lists all hourly employees
public String listHourly()
{
String a = "";
for (int x = 0; x < hourlyList.lengthIs(); x++)
{
if (hourlyList.isEmpty() == true)
return "No Hourly Employees";
else if (hourlyList.getItem(x) instanceof HourlyEmployee)
{
return a + hourlyList.toString() + " ";
}
}
return null;
}
// Method: listSalary
// Parameters: none
// Returns: none
// Partners: none
// Method lists all salary employees
public String listSalary()
{
String b = "";
for (int x = 0; x < salaryList.lengthIs(); x++)
{
if(salaryList.isEmpty() == true)
{
return "No Salary Employees";
}
else if (salaryList.getItem(x) instanceof SalaryEmployee)
{
return b + salaryList.toString() + " ";
}
}
return null;
}
// Method: listCommission
// Parameters: none
// Returns: none
// Partners: none
// Method lists all commission employees
public String listCommission()
{
String c = "";
for (int x = 0; x < commissionList.lengthIs(); x++)
{
if(commissionList.isEmpty() == true)
{
return "No Commission Employees";
}
else if (commissionList.getItem(x) instanceof CommissionEmployee)
{
return c + commissionList.toString() + " ";
}
}
return null;
}
// Method: resetWeek
// Parameters: none
// Returns: none
// Partners: none
// Method reset
public void resetWeek()
{
for (int a = 0; a < employees.lengthIs(); a++)
{
employees.getItem(a).resetWeek();
}
}
// Method: calculatePayout
// Parameters: none
// Returns: payout
// Partners: none
// Method calculates and returns weekly payout
public double calculatePayout()
{
double payout = 0;
for (int a= 0; a < employees.lengthIs(); a++)
{
payout += employees.getItem(a).calculateWeeklyPay();
}
return payout;
}
// Method: getIndex
// Parameters: empNum
// Returns: empNum
// Method retrieves location of employee number
public int getIndex(int empNum)
{
for (int a = 0; a < employees.lengthIs(); a++)
if (empNum == employees.getItem(a).getEmployeeNumber())
return a;
return -1;
}
// Method: annualRaises
// Parameters: none
// Returns: none
// Partners: none
// Method calculates annual raise
public void annualRaises()
{
for (int a = 0; a < employees.lengthIs(); a++)
{
employees.getItem(a).annualRaise();
}
}
// Method: holidayBonuses
// Parameters: none
// Returns: payout
// Partners: none
// Method used to output
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;
}
// Method: increaseHours
// Parameters: index, amount
// Returns: none
// Partners: none
// Method increases a certain employee’s hours worked.
public boolean increaseHours(int index, double amount)
{
for (int x = 0; x < employees.lengthIs(); x++)
{
if (employees.getItem(x) instanceof HourlyEmployee)
{
if (index == ((HourlyEmployee)employees.getItem(x)).getEmployeeNumber())
{
((HourlyEmployee)employees.getItem(x)).increaseHours(amount);
}
else
return false;
}
}
return true;
}
// Method: increaseSales
// Parameters: index, amount
// Returns: none
// Partners: none
// Method increases a certain employee’s sales
public boolean increaseSales(int index, double amount)
{
for (int x = 0; x < employees.lengthIs(); x++ )
{
if (employees.getItem(x) instanceof CommissionEmployee)
{
if (index == ( ((CommissionEmployee)employees.getItem(x)).getEmployeeNumber()))
{
((CommissionEmployee)employees.getItem(x)).increaseSales(amount);
}
else
return false;
}
}
return true;
}
// Method: findAllBySubString
// Parameters: findt
// Returns: Employee[]
// Method finds substring of string
public ArrayList<Employee> findAllBySubstring(String find)
{
ArrayList<Employee> ken = new ArrayList();
int count = 0;
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));
}
catch (MaximumCapacityException e)
{
System.out.print("too large");
}
}
}
return ken;
}
// Method: RabinKarp
// Parameters: name, find
// Returns: hash
// Method calculates hashes
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;
}
// Method: stringHash
// Parameters: s
// Returns: hash
// Method finds hash of entire string
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;
}
// Method: charNumericValue
// Parameters: findt
// Returns: c
// Method finds substring of string
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);
}
// Method: RabinKarpHashes
// Parameters: s, hashes, pos, length
// Returns: pos
// Method finds hashaes
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];
}
// Method: linearSearchRecursive
// Parameters: find
// Returns: pos
// Method does recursivelinear search
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);
}
// Method: sort
// Parameters: none
// Returns: pos
// Method sorts lists
public void sort()
{
hourlyList.sort();
salaryList.sort();
commissionList.sort();
}
// Method: addRequest
// Parameters: empNum
// Returns: none
// Method does adds request for vacation
public boolean addRequest(int empNum)
{
if (empNum >= 10000 && empNum <= 99999)
{
vacationRequests.enqueue(employees.getItem(getIndex(empNum)));
return true;
}
else
return false;
}
// Method: viewextRequest
// Parameters: none
// Returns: Employee
// Method views next request
public Employee viewNextRequest()
{
Employee emu = null;
if (vacationRequests == null)
return null;
else
return emu;
}
// Method: grantNextRequest
// Parameters: none
// Returns: Employee
// Method grants request
public Employee grantNextRequest()
{
if (vacationRequests != null)
{
return vacationRequests.dequeue();
}
else
return null;
}
// Method: outputRequests
// Parameters: none
// Returns: Employee
// Method prints requests
public String outputRequests()
{
String o = "";
if (vacationRequests != null)
return o + vacationRequests.toString();
else
return "No vacation requests";
}
// Method: loadEmployees
// Parameters: employeeFIle, requestFile
// Returns: boolean
// Method reads in and loads employees
public boolean loadEmployees(String employeeFile, String requestFile)
{
FileInputStream file, file1;
ObjectInputStream inout = null ;
Scanner in;
Employee ee = null;
Queue<Employee> q = null;
int emp = 0;
////clear
System.out.print(hourlyList.toString());
employees.clear();
hourlyList.clear();
salaryList.clear();
commissionList.clear();
vacationRequests.clear();
/////ending clear
////employee
try
{
inout = new ObjectInputStream(new FileInputStream(employeeFile));
}
catch (FileNotFoundException fnfe)
{
System.out.println("Error");
}
catch (IOException ioe)
{
System.err.println("etr");
}
if (inout == null)
{
return false;
}
try
{
while(true)
{
try
{
ee = (Employee)inout.readObject();
employees.addItem(ee);
}
catch (NoSuchElementException nsee)
{
break;
}
catch (IOException a)
{
System.err.println("");
return true;
}
}
}
catch (MaximumCapacityException mce)
{
System.out.print("Maximum Capacity Reached");
return true;
}
catch (ClassNotFoundException cnfe)
{
System.out.print("err");
}
//
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);
}
////queue
try
{
in = new Scanner(new File(requestFile));
}
catch (FileNotFoundException fnfe)
{
System.err.println("err");
return false;
}
while (true)
{
try
{
emp = in.nextInt();
}
catch (NoSuchElementException nsee)
{
break;
}
}
//
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;
}
// Method: saveEmployees
// Parameters: employeeFIle, requestFile
// Returns: boolean
// Method saves employees
public boolean saveEmployees(String employeeFile, String requestFile)
{
ObjectOutputStream output;
Formatter put = null;
Queue<Employee> vacayCopy = new Queue();
vacayCopy = vacationRequests;
////employeefile
try
{
output = new ObjectOutputStream(new FileOutputStream(employeeFile));
}
catch (IOException a)
{
System.out.println("error");
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("Error");
return false;
}
}
try
{
if(output != null)
{
output.close();
}
}
catch (IOException a)
{
System.out.println("error");
System.exit(1);
return false;
}
////requestfile
try
{
put = new Formatter(requestFile);
for (int x = 0; x < vacationRequests.lengthIs(); x++)
{
put.format("%d", vacationRequests.dequeue().getEmployeeNumber());
}
}
catch (FormatterClosedException fce)
{
System.out.println("error");
return false;
}
catch (FileNotFoundException fnfee)
{
System.out.print("err");
}
if (put != null)
{
put.close();
}
return true;
}
// Method: processUpdates
// Parameters: fileName
// Returns: boolean
// Method updates files
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;
}
int temp;
temp = getIndex(emp);
if (employees.getItem(temp) instanceof HourlyEmployee)
{
increaseHours(emp, requesT);
}
if (employees.getItem(emp) instanceof SalaryEmployee)
{
System.out.print("Cannot process update.");
}
if (employees.getItem(emp) instanceof CommissionEmployee)
{
increaseSales(emp,requesT);
}
if (temp == -1)
{
System.out.printf("Cannot process update %d is an invalid employee number.", emp);
}
}
if (input != null)
{
input.close();
}
return true;
}
}