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

Gain an understanding of using some of the common Java class library classes Inc

ID: 3725633 • Letter: G

Question

Gain an understanding of using some of the common Java class library classes

Increase your knowledge of generic classes

Understand performance implications of various data structures we have studied so far.

In order to this create a Java program that uses the generic Java classes ArrayList, LinkedList, HashSet and TreeSet to create a list of 100,000 random Strings. You should use the Random class to create the random strings. Measure the time to:

Populate the structure. That is, add the 100,000 Strings.

As you add the strings to the list, remember the first, middle and last String added.

Search for the first, middle and last String. Perform the search 10,000 times.

Because of the object hierarchy, you only need to write the method that implements steps 1 and 2 once. The main program should create (execute the new) the list to be tested and pass that reference to the method that executes steps 1 and 2.

Use System.currentTimeMillis() to determine the elapsed time for steps 1 and 2 above.

The results should be printed in a simple report as shown below. Submit this output along with an explanation of the results you experience. Your explanation of the results can be very brief. Only a couple of sentences is needed.

A submission that completes the above successfully will be graded as a B. In order to achieve an A, implement the same as above but rather than creating the generic classes using <String>, use <Item> instead. The only variable needed in Item is a random string, just like in the first part of the project. Item will need to implement Comparable and include the equals and toString methods. During your testing verify that the compareTo and equals methods are actually called.

Sample Output:

Array of String create time (ms) = 36

Array of String First search time (ms): 1

Array of String Middle search time (ms): 3239

Array of String Last search time (ms): 6839

Linked List of String create time (ms) = 16

Linked List of String First search time (ms): 1

Linked List of String Middle search time (ms): 5178

Linked List of String Last search time (ms): 10522

Hash Set of String create time (ms) = 84

Hash Set of String First search time (ms): 4

Hash Set of String Middle search time (ms): 1

Hash Set of String Last search time (ms): 1

Tree Set of String create time (ms) = 90

Tree Set of String First search time (ms): 3

Tree Set of String Middle search time (ms): 1

Tree Set of String Last search time (ms): 2

Optional Output:

Array of Item create time (ms) = 29

Array of Item First search time (ms): 3

Array of Item Middle search time (ms): 3747

Array of Item Last search time (ms): 7904

Linked List of Item create time (ms) = 561

Linked List of Item First search time (ms): 3

Linked List of Item Middle search time (ms): 7125

Linked List of Item Last search time (ms): 12549

Hash Set of Item create time (ms) = 47

Hash Set of Item First search time (ms): 1

Hash Set of Item Middle search time (ms): 1

Hash Set of Item Last search time (ms): 0

Tree Set of Item create time (ms) = 136

Tree Set of Item First search time (ms): 6

Tree Set of Item Middle search time (ms): 2

Tree Set of Item Last search time (ms): 2

Grading Criteria

Methods must be of appropriate length – usually less than 1 page in length

Appropriate private and public access modifiers must be used. Almost always data is private and methods are public although you may certainly have private or protected methods if appropriate.

Meets program specifications as described above

The program is robust with no runtime errors or problems

Program is readable

Comments

Use Javadocs conventions (Appendix H)

Include comments on the class as a whole including

Description of program

Your name (use @author)

Date (due or date you started or last modified)

Source of any “borrowed” code

For each method include

Concise description of what method does

Arguments for each method (use @param)

Returned value (use @returns)

Exceptions which are thrown (@throws)

Consistent and correct indenting

Meaningful identifiers

Normal capitalization conventions are used

The source of any "borrowed" code is clearly identified

Explanation / Answer

Following is Helper class to create List and search operations

HelperClass.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package genericclasstest;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
*
* @author kunal
*/
public class HelperClass {
  
private List<String> myList;
private Set<String> mySets;
public String first, middle, last;
private static final String chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final int sizeString = 10;
  
//get referece to ArrayList and LinkedList
public void createList(List l){
myList = l;
addStrings();

}
  
//get referece to HashSet and TreeSet
public void createSet(Set s){
mySets = s;
addStringsToSet();
}
  
private void addStringsToSet(){
for(int i=0;i<100000; i++){
String ranStr = generateRandomString();
mySets.add(ranStr);
switch(i){
case 0:
first = ranStr;
break;
case 49999:
middle = ranStr;
break;
case 99999:
last = ranStr;
break;   
}
}   
}
  

  
  
  
public void searchStringList(String toSearch){
for(int i=0;i<10000;i++){
myList.contains(toSearch);
}
}
  
public void searchStringSet(String toSearch){
for(int i=0;i<10000;i++){
mySets.contains(toSearch);
}
}
  
private void addStrings(){
  
for(int i=0;i<100000; i++){
String ranStr = generateRandomString();
myList.add(ranStr);
switch(i){
case 0:
first = ranStr;
break;
case 49999:
middle = ranStr;
break;
case 99999:
last = ranStr;
break;   
}
}
}
  

/**
* This method generates random string
* @return
*/
public String generateRandomString(){

StringBuffer randStr = new StringBuffer();
for(int i=0; i<sizeString; i++){
int number = getRandomNumber();
char ch = chars.charAt(number);
randStr.append(ch);
}
return randStr.toString();
}

/**
* This method generates random numbers
* @return int
*/
private int getRandomNumber() {
int randomInt = 0;
Random randomGenerator = new Random();
randomInt = randomGenerator.nextInt(chars.length());
if (randomInt - 1 == -1) {
return randomInt;
} else {
return randomInt - 1;
}
}
  
}

=============================================================================

Following is the main class

GenericClassTest.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package genericclasstest;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeSet;

/**
*
* @author kunal
*/
public class GenericClassTest {
  
//display Execution time for List
private void displayExecutionTimesList(HelperClass helperClass, String typeClass){
long start, stop;
start = System.currentTimeMillis();
helperClass.searchStringList(helperClass.first);
stop = System.currentTimeMillis();
System.out.println(typeClass+" of String First search time (ms):"+(stop-start));
start = System.currentTimeMillis();
helperClass.searchStringList(helperClass.middle);
stop = System.currentTimeMillis();
System.out.println(typeClass+" of String Middle search time (ms):"+(stop-start));
start = System.currentTimeMillis();
helperClass.searchStringList(helperClass.last);
stop = System.currentTimeMillis();
System.out.println(typeClass+" of String Last search time (ms):"+(stop-start)+" ");
}
  
//display Execution time for Set
private void displayExecutionTimesSet(HelperClass helperClass, String typeClass){
long start, stop;
start = System.currentTimeMillis();
helperClass.searchStringSet(helperClass.first);
stop = System.currentTimeMillis();
System.out.println(typeClass+" of String First search time (ms):"+(stop-start));
start = System.currentTimeMillis();
helperClass.searchStringSet(helperClass.middle);
stop = System.currentTimeMillis();
System.out.println(typeClass+" of String Middle search time (ms):"+(stop-start));
start = System.currentTimeMillis();
helperClass.searchStringSet(helperClass.last);
stop = System.currentTimeMillis();
System.out.println(typeClass+" of String Last search time (ms):"+(stop-start)+" ");
}
  
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
GenericClassTest obj = new GenericClassTest();
HelperClass helperClass = new HelperClass();
long start=0 , stop = 0;
helperClass = new HelperClass();
start = System.currentTimeMillis();
helperClass.createList(new ArrayList<String>());
stop = System.currentTimeMillis();
System.out.println("Array of String create time (ms) ="+(stop-start));
obj.displayExecutionTimesList(helperClass,"Array");
  
start = System.currentTimeMillis();
helperClass.createList(new LinkedList<String>());
stop = System.currentTimeMillis();
System.out.println("LinkedList of String create time (ms) ="+(stop-start));
obj.displayExecutionTimesList(helperClass,"LinkedList");
  
start = System.currentTimeMillis();
helperClass.createSet(new HashSet<String>());
stop = System.currentTimeMillis();
System.out.println("Hash Set of String create time (ms) ="+(stop-start));
obj.displayExecutionTimesSet(helperClass, "Hash Set");
  
start = System.currentTimeMillis();
helperClass.createSet(new TreeSet<String>());
stop = System.currentTimeMillis();
System.out.println("Tree Set of String create time (ms) ="+(stop-start));
obj.displayExecutionTimesSet(helperClass,"Tree Set");
  
  
}
  
  
  

  
}

==============================================================================

following is the required output

Output :

Array of String create time (ms) =137

Array of String First search time (ms):2
Array of String Middle search time (ms):4027
Array of String Last search time (ms):17288

LinkedList of String create time (ms) =106
LinkedList of String First search time (ms):1
LinkedList of String Middle search time (ms):16536
LinkedList of String Last search time (ms):37768

Hash Set of String create time (ms) =175
Hash Set of String First search time (ms):2
Hash Set of String Middle search time (ms):2
Hash Set of String Last search time (ms):1

Tree Set of String create time (ms) =230
Tree Set of String First search time (ms):3
Tree Set of String Middle search time (ms):9
Tree Set of String Last search time (ms):3

========================================================

It is observe that ArrayList and Linkelist take less time for creation but more time for search than HashSet and TreeSet. In ArrayList and LinkedList first string search is best case, middle string search is average case and last string is worst case. Search time of ArrayList is better than LinkedList. Search time of HashSet and TreeSet is very minimal as compared to ArrayList/LinkedList.