Consider the four core interfaces, Set, List, Queue, and Map. For each of the following four assignments, specify which of the four core interfaces is best-suited, and explain how to use it to implement the assignment.
1 :- Whimsical Toys Inc (WTI) needs to record the names of all its employees. Every month, an employee will be chosen at random from these records to receive a free toy.
2 :- WTI has decided that each new product will be named after an employee but only first names will be used, and each name will be used only once. Prepare a list of unique first names.
3 :- WTI decides that it only wants to use the most popular names for its toys. Count up the number of employees who have each first name.
4 :- WTI acquires season tickets for the local lacrosse team, to be shared by employees. Create a waiting list for this popular sport.
Above question has been taken from Oracle website :
Below is the implentation of the question given above :
For Que 1 :
Function findRandomEmployee() is the implementation
For Que 2 :
Function prepareUniqueListName() is the implementation
For Que 3 :
Function findpopularEmployeeName() is the implementation
For Que 4 :
It is straight forward so did not implemented it .
Implementation :
import java.awt.List;
import
java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import
java.util.Comparator;
import
java.util.HashMap;
import java.util.HashSet;
import
java.util.Map;
import
java.util.Random;
import java.util.Set;
import
java.util.SortedSet;
import
java.util.TreeSet;
class Employee
{
public String firstname ;
public String lastname ;
Employee(String firstname , String
lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
}
public class WhimsicalToysInc
{
public static void main(String[] args) {
ArrayList<Employee> list = new
ArrayList<Employee>();
list.add(new Employee("ishant","gaurav"));
list.add(new Employee("Ashish","Prasher"));
list.add(new Employee("Hemant","Bisht"));
list.add(new Employee("hemant","Bisht"));
list.add(new Employee("Rahul","Rana"));
list.add(new Employee("Deep","Money"));
//
Prining the random employee
Employee e = findRandomEmployee(list);
System.out.println("firstname "+e.firstname + " lastname "+e.lastname);
//
preparing the unique name list
prepareUniqueListName(list);
//
finding the popular list of employees
findpopularEmployeeName(list);
}
//
function to find the popular Employee name
private static void findpopularEmployeeName(ArrayList<Employee> list)
{
Map<String, Integer>
frequency = new HashMap<String, Integer>();
ArrayList<String> templist =
new
ArrayList<String>();
for(Employee e : list)
{
Employee temp = e;
templist.add(temp.firstname.toLowerCase());
}
for (String element : templist) {
if
(frequency.containsKey(element)) {
frequency.put(element,
frequency.get(element) + 1);
}
else
{
frequency.put(element, 1);
}
}
for (Map.Entry<String, Integer> entry :
frequency.entrySet()) {
System.out.print(entry.getKey() + "
= " + entry.getValue() + " ");
}
System.out.println();
}
//
Function to prepare the unique first name list of the employee
private static void prepareUniqueListName(ArrayList<Employee> list) {
//
Customized Comparator to implement the ignore case unique element
final Comparator<String> IGNORE_CASE = new Comparator<String>()
{
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
};
SortedSet<String> uniquefirstname = new TreeSet<String>(
IGNORE_CASE);
for(Employee e : list)
{
Employee temp = e;
uniquefirstname.add(temp.firstname);
}
System.out.println("Unique First Name : ");
for( String s : uniquefirstname )
{
System.out.println(s);
}
}
// Function to find the random employee from the
list
private static Employee findRandomEmployee(ArrayList<Employee>
list) {
int size = list.size();
//System.out.println("size
"+size);
int rand = new Random().nextInt(size);
//System.out.println(rand);
Employee e =list.get(rand);
return e;
}
}
Please comment if you have any question or suggestion for the above code.
No comments:
Post a Comment