Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I jusy need the last four methods, to understand how the iterator works with Has

ID: 3832893 • Letter: I

Question

I jusy need the last four methods, to understand how the iterator works with HashSet, HashMap and with loops. You don't have to draw the diagram its for refrence only. Please follow the steps, Thank You!

Format

Exercise

Notes

Project

DRAW

Review lecture slides to complete these (2) diagrams

6.X1

Suppose a company has a name and group (collection) of employees where each employee name (Key) is mapped to an employee id (Value):

The employee name is always stored in uppercase with ALL extra spaces (leading, trailing and multiples in the middle) removed

The employee id is the first letter of each word in the name along with a random 3-digit number between 100 – 999 (inclusive of both ends)

            i.e. à <”CHIA CHIA CHIEN”, “CCC123”>

DRAW (by hand) the class diagram for this project with:

a class named Company with

class constants MIN = 100 and MAX = 999 (declared and initialized)

a String field named name

a HashMap field named employees

On a blank piece of paper

6.X2

DRAW (by hand) an object diagram with:

1 object instance of the Company class named myCompany whose company name is “ACME CO”

2 Map instances in the employees HashMap field:

employees.put(“JOHN PAUL JONES”, “JPJ246”)

employees.put(“MARY JANE SMITH”, “MJS579”)

Continued on BACK …

do

New Project

§ In BlueJ, create “New Project” named LASTNAME-company with a README

Use good programming style and Javadoc documentation standards

LASTNAME-company

CODE

6.X3

Create a “New Class…” named Company to store the employee mappings of employee names (Key) to employee ids (Value)

Declare and initialize class constants MIN and MAX to appropriate values

Declare and instantiate a Random randomGenerator field object

Declare a String field named name for the company’s name

Declare a HashMap field named employees

Add a constructor with only 1 parameter named name and:

Assign name field to name parameter after trim, uppercase & replaceAll   (1 or more ANY white space regular expression with just 1 single space)

Initialize employees field by creating an instance of that object

Add accessors getName( ) and getEmployees( )

Add accessor getTotalNumberEmployees( ) to return the total number of mappings in the collection of the employees field

Add formatString(String origString) to return a formatted origString after trim, uppercase, and replaceAll in between white spaces (with single space)

MUST first check if origString is null (which just returns an empty String)

Add method String generateId(String name) to return the employee id generated by taking the first letter of each word in name parameter AND adding a random 3-digit integer between 100-999 (inclusive of both ends)

MUST .split the parameter name into a String[ ] nameArray with regex taking into account multiple white spaces in between words in name

MUST use a for loop to get the first letter in each word in nameArray

MUST use class constants MIN & MAX (NO hard-code) to generate (using .nextInt) the random number used for the id in the range [MIN – MAX]

Add addEmployee(String inputName) & removeEmployee(String inputName) both with void returns and:

Check if (trimmed) inputName is empty or null, print “Name is INVALID”

MUST use formatString for inputName (remember Strings are immutable)

Print “Existing: ” (if name already exists for addEmployee)

OR …“Non-existing: ” (if non-existing for removeEmployee)

MUST use generateId (for addEmployee) to generate employee id to .put

MUST print heading with name & id, if employee add/delete is successful

Add void removeIds(String id) to remove ALL mappings in employees where the value matches the id search parameter and:

MUST use a local variable to keep track if a match was found

MUST also have check if id parameter is null or empty

MUST use formatString to format the id input parameter

MUST use a for loop, .keySet and .iterator to iterate thru all employees

Check if each employee id value is equal (ignoring case) to the search id

MUST use the Iterator.remove to remove id matching mappings

MUST print heading with name & id for EACH removed employee

Only after the entire search is completed (THUS … outside of loop), check if no matches found and print “NO employees with id: ”

Add void listEmployees() to print ALL employee names (Key) & ids (Value):

MUST always print the heading “Employees for :”

MUST use getTotalNumberEmployees or .isEmpty to check if NO entries in employees exist and prints“NO employees”

MUST use a for-each loop and .keySet to iterate thru all employees

MUST print employee in format “     : ” (w/ leading spaces)

Format

Exercise

Notes

Project

DRAW

Review lecture slides to complete these (2) diagrams

6.X1

Suppose a company has a name and group (collection) of employees where each employee name (Key) is mapped to an employee id (Value):

The employee name is always stored in uppercase with ALL extra spaces (leading, trailing and multiples in the middle) removed

The employee id is the first letter of each word in the name along with a random 3-digit number between 100 – 999 (inclusive of both ends)

            i.e. à <”CHIA CHIA CHIEN”, “CCC123”>

DRAW (by hand) the class diagram for this project with:

a class named Company with

class constants MIN = 100 and MAX = 999 (declared and initialized)

a String field named name

a HashMap field named employees

On a blank piece of paper

6.X2

DRAW (by hand) an object diagram with:

1 object instance of the Company class named myCompany whose company name is “ACME CO”

2 Map instances in the employees HashMap field:

employees.put(“JOHN PAUL JONES”, “JPJ246”)

employees.put(“MARY JANE SMITH”, “MJS579”)

Explanation / Answer

class Company {

private const int MIN = 100;
private const int MAX = 100;    
private HashMap<Employee,String> employees = new HashMap <Employee,String>();
private Random random = new Random();
private String name;

Company(String name){
   this.name = name.trim().toUpperCase();

}

public setEmployees(Map<Employee,String> emp){
   this.employees = emp;
}

public void getEmployees(){
   return this.employees;
}

public void getTotalNumberEmployees(){
   return (this.employees.size());
}

public String generateId(String name){

   String firstLetters = "";
   String[ ] nameArray = name.split("regex")';
   for(String s : nameArray.split(" "))
   {
           firstLetters += s.charAt(0);
   }
  
    int randomNum = random.nextInt((this.MAX - this.MIN) + 1) + MIN;  

   StringBuilder sb = new StringBuilder();
   sb.append(firstLetters).append(Integer.toString(randomNum)).toString();
   return sb;
}