task 1. Create an employee Class that encapsulates the concept of an employee. T
ID: 3855932 • Letter: T
Question
task 1.
Create an employee Class that encapsulates the concept of an employee. The attributes of an employee are: - id o a random integer in the range 0 to 999,999,999 inclusive o modify your code that generates the id value to insure that duplicate id values do not occur. o we will be using the id attribute as the key value and we want this key to be unique - name o a String of a random length between 5 and 10 characters (inclusive) made up of a random set of lower case characters - dept o a random integer in the range 1 to 5 (inclusive) - hired o a random integer in the range 1995 to 2005 (inclusive)
Task 2:
Create the following classes: - TreeMap - AVLTreeMap - SplayTreeMap - RBTreeMap
Task 3: -
Create a client class that o Generates an array of 100,000 employees in random order o Create a second array of 10,000 unique integers in the range 0 to 99,999. These integer values should be in a random order. o For each of the tree map classes created in Task2 do the following: Start a timer Insert each employee object from the first array into the tree class Stop the timer and record the insertion/build time Start a timer Walk across the second array using its stored integer values as the key values (id value) to look up each entry in the tree. Stop the timer and record the search/lookup time. o Create and display a nicely formatted ASCII table that shows the insertion/build time and search/lookup time for each of the tree classes. Be sure to include the value of N as part of the table. o Consider upping N to 1,000,000/100,000 and possibly 10,000,000/1,000,000 if it appears that your program will run in a “reasonable” amount of time. You only need to include the result table for the highest value of N used.
I have code for task 1.
package company;
import java.util.Random;
//class employee
public class Employee{
public static int id;
public static String name;
public static int dept;
public static int hired;
public int getid();
public String getname;
public int getdept();
public int gethired();
} //end employee class
public abstract class AbstractFactory {
abstract public int getid();
abstract public String getname;
abstract public int getdept();
abstract public int gethired();
}
public class EmployeeFactory extends AbstractFactory {
public int getid() {
Random rand = new Random();
public static int emp_id=rand.nextInt(1000000000);
return emp_id }
//string name
public String getname() {
public static String name;
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
StringBuilder sb = new StringBuilder();
Random rand2 = new Random();
for (int i = 0; i < 20; i++) {
char c = chars[random2.nextInt(chars.length)];
sb.append(c); }
name = sb.toString();
return name;
}
public int getdept(){
Random rand1 = new Random();
rand1 = 1 + (int)(Math.random() * 5);
public static dept=rand1;
return dept;
}
// hired variable
public int gethired(){
Random rand3 = new Random();
rand3 = 1995 + (int)(Math.random() * 2005);
public static hired = rand3;
return hired;
}
}
public class client() {
public static void main(String args[]){
// array of 1000 employess
Employee[] myList = new Employee[1000];
for( int i=0; i<1000; i++ ) Employee [i] = new Employee();
}
Explanation / Answer
First create an employee struct containing the properties of the conceptual employee.
Then create a database with matching table structure, say for example mssql
Then create a employee repository For that database EmployeeRepoMsSql with the various CRUD operations you require.
Then create an IEmployeeRepo interface exposing the CRUD operations
Then expand your Employee struct to a class with a construction parameter of IEmployeeRepo. Add the various Save/Delete etc methods you require and use the injected EmployeeRepo to implement them.
When it cones to Id I suggest you use a GUID which can be generated via code in the constructor.
To work with existing objects your code can retrieve them from the database via the repository before calling their Update Method.
Alternatively you can opt for the frowned upon (but in my view superior) Anemic Domain Object model where you don't add the CRUD methods to your object, and simply pass the object to the repo to be updated/saved/deleted
Immutability is a design choice which will depend on your patterns and coding style. If you are going all functional then try to be immutable as well. But if you are unsure a mutable object is probably easier to implement.
Instead of Create() I would go with Save(). Create works with the immutability concept, but I always find it useful to be able to construct an object which is not yet 'Saved' eg you have some UI which allows you to populate an employee object or objects and then verify them again some rules before saving to the database.
***** example code
2down voteFirst create an employee struct containing the properties of the conceptual employee.
Then create a database with matching table structure, say for example mssql
Then create a employee repository For that database EmployeeRepoMsSql with the various CRUD operations you require.
Then create an IEmployeeRepo interface exposing the CRUD operations
Then expand your Employee struct to a class with a construction parameter of IEmployeeRepo. Add the various Save/Delete etc methods you require and use the injected EmployeeRepo to implement them.
When it cones to Id I suggest you use a GUID which can be generated via code in the constructor.
To work with existing objects your code can retrieve them from the database via the repository before calling their Update Method.
Alternatively you can opt for the frowned upon (but in my view superior) Anemic Domain Object model where you don't add the CRUD methods to your object, and simply pass the object to the repo to be updated/saved/deleted
Immutability is a design choice which will depend on your patterns and coding style. If you are going all functional then try to be immutable as well. But if you are unsure a mutable object is probably easier to implement.
Instead of Create() I would go with Save(). Create works with the immutability concept, but I always find it useful to be able to construct an object which is not yet 'Saved' eg you have some UI which allows you to populate an employee object or objects and then verify them again some rules before saving to the database.
***** example code
public class Employee { public string Id { get; set; } public string Name { get; set; } private IEmployeeRepo repo; //with the OOP approach you want the save method to be on the Employee Object //so you inject the IEmployeeRepo in the Employee constructor public Employee(IEmployeeRepo repo) { this.repo = repo; this.Id = Guid.NewGuid().ToString(); } public bool Save() { return repo.Save(this); } } public interface IEmployeeRepo { bool Save(Employee employee); Employee Get(string employeeId); } public class EmployeeRepoSql : IEmployeeRepo { public Employee Get(string employeeId) { var sql = "Select * from Employee where Id=@Id"; //more db code goes here Employee employee = new Employee(this); //populate object from datareader employee.Id = datareader["Id"].ToString(); } public bool Save(Employee employee) { var sql = "Insert into Employee (...."; //db logic } } public class MyADMProgram { public void Main(string id) { //with ADM don't inject the repo into employee, just use it in your program IEmployeeRepo repo = new EmployeeRepoSql(); var emp = repo.Get(id); //do business logic emp.Name = TextBoxNewName.Text; //save to DB repo.Save(emp); } } Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.