Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*****I posted the completed code for part 1**** Help me with part 2 at the bottom. Can you please write a code Java for the

*****I posted the completed code for part 1****

Help me with part 2 at the bottom. Can you please write a code Java for the LQHashed class SP2017LAB6_LQHashedDataStructure_yourLastName

OBJECTIVES

Doing this lab, students have the chance to:

-Review the concept of Hashtable data structures

-Know how to implement the Java Hashtable class: use functions, put, get, remove, etc.

-Know how to read the information of the object from an input file, place to the Hashtable object and write the information of all the nodes in the structure to the file before terminating the program -Know how to display all the nodes in the Hashtable on the screen

Part1: Java class Hashtable

REQUIREMENT

The company A stores the information of all employees in the text file named as employeeInfo.txt. The information of each employee is written on one line in the following format, for example:

For the employee:

Bill - White 0012345 Marketing department 45000 Or for the employee with the tittle:

Nancy Nguyen 1234567 Sale department 40000 - Manager

The application will use Hashtable object as data structure then open and read the file employeeInfo.txt The information of each line of the file employeeInfo.txt as a node to be inserted to the data structure. After reading the file, ensure all the information of employees or title employees are stored in the data structure by showing all nodes.

Provide the menu to allow users to select the following tasks. After finishing each task, re-display the menu to allow user to continue using the application:

1.Insert employee or employee with title

2.Fetch

3.Encapsulation

4.Update

5.Delete

6.Show all

7.Exit

//INSERT data to the Hashtable

-Allow users to enter the information of an employee or an employee with the title from the keyboard

-Create the object then insert to the data structure

-Users can continue inputing until users want to stop

//FETCH data

-Allow users to type an employee id then read the information from the data structure to print out the information on the screen

//ENCAPSULATION

-Allow users to type an employee id from the keyboard. Using the entered employee id to read the information of the node from data structure then assign the information to the node named temp.

Change the salary of the node temp

-Fetch the node with the entered employee id again then compare its salary with the salary of the node

temp. . If the salary are different then display the message box: The Java Hashtable structure has the encapsulation; otherwise display the message box: The Java Hashtable structure does not have the encapsulation

//UPDATE data

Display the message to ask users to enter the employee id. Using the employee id to read the node out the data structure. Asking for the new department from the keyboard. Change the department with the new department. Display the node from the data structure to check new updated information

//DELETE data

Display the message to ask users to enter the employee id. Remove the node with the entered employee id

//SHOW ALL

Display all the nodes are currently stored in the data structure

//Exit program

When users choose to exit the program, open the file employeeInfo.txt to write and write all the information of employee nodes stored in the data structure to the file employeeInfo.txt Close file

ANALYZING AND DESIGNING

-Draw the UML of class Employee_yourLastName and class TitleEmployee_yourLastName

-Write the pseudo-code of the main()

WHAT YOU NEED TO KNOW TO DO THE LAB

-Review how to use the do.. while loop to handle the menu to re-display

-switch statement to handle each task of menu

-How to open the file to read, how to open the file to write, how to read/write from/to the file; how to close the file

-How to declare a data structure of type Hashtable; put, get, remove, etc. the nodes

HOW TO START THE LAB

-You can create the project named FA2016LAB6_PART1 then add the data type class

FA2016LAB6_Employee_yourLastName, FA2016LAB6_TitleEmployee_yourLastName and a driver class FA2016LAB6_HashedDataStructure_yourLastName

public class employee { private String fisrtName,lastName; private int id; private String dept; private int salary; public String getFisrtName() { return fisrtName; } public void setFisrtName(String fisrtName) { this.fisrtName = fisrtName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; }

}

employeeWithTitle.java

public class employeeWithTitle { private String fisrtName,lastName; private int id; private String dept; private int salary; private String title; public String getFisrtName() { return fisrtName; } public void setFisrtName(String fisrtName) { this.fisrtName = fisrtName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }

}

employeeTest.java

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.util.Hashtable; import java.util.Map; import java.util.Scanner;

public class employeeTest {

public static void main(String a[])throws Exception { Hashtable employeeInfo=new Hashtable<>(); BufferedReader br=new BufferedReader(new FileReader("F:\\Corsera1\\ABC1\\src\\employeeInfo.txt")); //to read file String line=""; /* * read file line by line until last laine */ while((line=br.readLine())!=null) { //split firstname,lastname etc read in single line. String lineFields[]=line.split("-"); //if read line is record of employee if(lineFields.length==5) { employee e=new employee(); e.setFisrtName(lineFields[0]); e.setLastName(lineFields[1]); e.setId(Integer.parseInt(lineFields[2])); e.setDept(lineFields[3]); e.setSalary(Integer.parseInt(lineFields[4])); employeeInfo.put(e.getId(),e ); } //if read line is record of employee with title. else { employeeWithTitle e=new employeeWithTitle(); e.setFisrtName(lineFields[0]); e.setLastName(lineFields[1]); e.setId(Integer.parseInt(lineFields[2])); e.setDept(lineFields[3]); e.setSalary(Integer.parseInt(lineFields[4])); e.setTitle(lineFields[5]); employeeInfo.put(e.getId(),e ); } } Scanner s=new Scanner(System.in); int choice=0; do{ //show menu System.out.println("1.Insert Employee or employee with title " + "2.Fetch " + "3.Encapsulation " + "4.Update " + "5.Delete " + "6.Show All " + "7.Exit"); //read choice from user choice=s.nextInt(); s.nextLine(); switch(choice){ case 1: String fnm,lnm,dept,title=""; int id,salary; System.out.println("Enter First Name: "); fnm=s.nextLine(); System.out.println("Enter Last Name: "); lnm=s.nextLine(); System.out.println("Enter EmployeeId: "); id=s.nextInt(); s.nextLine(); System.out.println("Enter Department: "); dept=s.nextLine(); System.out.println("Enter Salary: "); salary=s.nextInt(); s.nextLine(); System.out.println("Enter title(leave blank if no title): "); title=s.nextLine(); //if no title then enter employee record if(title.equals("")) { employee e=new employee(); e.setId(id); e.setFisrtName(fnm); e.setLastName(lnm); e.setDept(dept); e.setSalary(salary); employeeInfo.put(id, e); } //if title is vailable then enter in employee with title record else { employeeWithTitle e=new employeeWithTitle(); e.setId(id); e.setFisrtName(fnm); e.setLastName(lnm); e.setDept(dept); e.setSalary(salary); e.setTitle(title); employeeInfo.put(id, e); } break; case 2: int eid; boolean flag=false; System.out.println("Enter Employee ID:"); eid=s.nextInt(); s.nextLine(); /* * iterate through the hashtable and retrieve objects one by one. * if the retrieved object would be of employee class then no exception * will occur * otherwise ClassCastException would be thrown and catch part * will be executed. * same applies in all cases. */ for(Map.Entry m:employeeInfo.entrySet()) { try{ employee e=(employee)m.getValue(); if(e.getId()==eid){ System.out.println(e.getFisrtName()+"-" + e.getLastName()+"-" + e.getId()+"-"+e.getDept()+"-" + e.getSalary()); flag=true; } }catch(Exception e){ employeeWithTitle e1=(employeeWithTitle)m.getValue(); if(e1.getId()==eid){ System.out.println(e1.getFisrtName()+"-" + e1.getLastName()+"-" + e1.getId()+"-"+e1.getDept()+"-" + e1.getSalary()+"-" + e1.getTitle()); flag=true; } } } if(flag==false) System.out.println("Employee Does not exist"); break; case 3: System.out.println("Enter Employee ID:"); int eid1=s.nextInt(); s.nextLine(); boolean flag1=false; for(Map.Entry m:employeeInfo.entrySet()) { try{ employee e=(employee)m.getValue(); if(e.getId()==eid1){ employee temp=e; temp.setSalary(100); if(temp.getSalary()==e.getSalary()) System.out.println("The Java HashTable Structure has the encapsulation"); else System.out.println("The Java HashTable Structure does not have the encapsulation"); flag1=true; } }catch(Exception e){ employeeWithTitle e1=(employeeWithTitle)m.getValue(); if(e1.getId()==eid1){ employeeWithTitle temp=e1; temp.setSalary(100); if(temp.getSalary()==e1.getSalary()) System.out.println("The Java HashTable Structure has the encapsulation"); else System.out.println("The Java HashTable Structure does not have the encapsulation"); flag1=true; } } } if(flag1==false) System.out.println("Employee does not exist"); break; case 4: boolean flag2=false; System.out.println("Enter Employee ID:"); int eid2=s.nextInt(); s.nextLine(); String dep1=""; for(Map.Entry m:employeeInfo.entrySet()) { try{ employee e=(employee)m.getValue(); if(e.getId()==eid2){ System.out.println("Enter new Department:"); dep1=s.nextLine(); e.setDept(dep1); System.out.println(e.getFisrtName()+"-" + e.getLastName()+"-" + e.getId()+"-"+e.getDept()+"-" + e.getSalary()); flag2=true; } }catch(Exception e){ employeeWithTitle e1=(employeeWithTitle)m.getValue(); if(e1.getId()==eid2){ System.out.println("Enter new Department:"); dep1=s.nextLine(); e1.setDept(dep1); System.out.println(e1.getFisrtName()+"-" + e1.getLastName()+"-" + e1.getId()+"-"+e1.getDept()+"-" + e1.getSalary()+"-" + e1.getTitle()); flag2=true; } } } if(flag2==false) System.out.println("Employee does not exist"); break; case 5: System.out.println("Enter Employee ID:"); int eid3=s.nextInt(); s.nextLine(); boolean flag3=false; for(Map.Entry m:employeeInfo.entrySet()) { try{ employee e=(employee)m.getValue(); if(e.getId()==eid3){ employeeInfo.remove(eid3); flag3=true; } }catch(Exception e){ employeeWithTitle e1=(employeeWithTitle)m.getValue(); if(e1.getId()==eid3){ employeeInfo.remove(eid3); flag3=true; } } } if(flag3==false) System.out.println("Employee does not exist"); break; case 6: for(Map.Entry m:employeeInfo.entrySet()) { try{ employee e=(employee)m.getValue(); System.out.println(e.getFisrtName()+"-" + e.getLastName()+"-" + e.getId()+"-"+e.getDept()+"-" + e.getSalary()); }catch(Exception e){ employeeWithTitle e1=(employeeWithTitle)m.getValue(); System.out.println(e1.getFisrtName()+"-" + e1.getLastName()+"-" + e1.getId()+"-"+e1.getDept()+"-" + e1.getSalary()+"-" + e1.getTitle()); } } break; case 7: BufferedWriter bw=new BufferedWriter(new FileWriter("F:\\Corsera1\\ABC1\\src\\employeeInfo.txt")); for(Map.Entry m:employeeInfo.entrySet()) { try{ employee e=(employee)m.getValue(); bw.write(e.getFisrtName()+"-" + e.getLastName()+"-" + e.getId()+"-"+e.getDept()+"-" + e.getSalary()+" "); }catch(Exception e){ employeeWithTitle e1=(employeeWithTitle)m.getValue(); bw.write(e1.getFisrtName()+"-" + e1.getLastName()+"-" + e1.getId()+"-"+e1.getDept()+"-" + e1.getSalary()+"-" + e1.getTitle()+" "); } bw.flush(); } bw.close(); break; } }while(choice!=7); br.close(); } }

PART2: Linear Quotient (LQHashed) Data Structure OBJECTIVES Doing this lab, students have the chance to:

-Implement the Linear Quotient (LQHashed) data structure: understand Insert, Fetch, Delete and update algorithm of the LQhashed structure -Know how to display all the nodes in the LQHashed data structure on the screen

REQUIREMENT Provide the application that creates a data structure of type LQHashed then provides the menu that includes the following tasks and allow users to work on each task until users want to exit

1. Insert

2. Fetch

3. Encapsulation

4. Update

5. Delete

6. Show all

INSERT data -read input information of employees or title employees from the keyboard then create the object and add to the data structure until users want to stop.

FETCH data -Display the message to ask for an employee id then Fetch the node from the LQHashed data structure to node named temp and display the information of node ENCAPSULATION -Use the above node fetched from the LQHahsed data structure, change the salary of temp -Fetch employee with the same employee id again to see if they have the same salary then display display the message box: The LQHashed structure has the encapsulation; otherwise display the message box: The LQHashed structure does not have the encapsulation

UPDATE data

-Display the message and read the employee id then fetch the node to node temp

-Read the new department from the keyboard, update the new department to temp

-Update the new node to the data structure

DELETE data -Display the message and read the employee id then using the employee id to delete the node

SHOWALL -Display all the nodes currently stored in the data structure

ANALYZING AND DESIGNING -Write the pseudo-code of the main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Project Management in Practice

Authors: Samuel J. Mantel Jr., Jack R. Meredith, Sco

4th edition

470533013, 978-0470533017

Students also viewed these Databases questions