I need help with two things First Id like someone to check o

I need help with two things. First I\'d like someone to check over my code for the 3 //TODO statements below to verify that they\'re correct

public static HashSet<Integer> doHashSetInsertMedian(int numItems) {
       System.out.print(\"doHashSetInsertMedian: \");
       HashSet<Integer> set = new HashSet<>();

       // TODO Write code that adds integers 0 through (numItems - 1)
       // to set, inside a loop.

       for(int i=0; i<numItems-1; i++){
       set.add(i);
       }
      
       // TODO Write code that removes integer (numItems / 2)
       // from set.

   int median = numItems / 2;
   set.remove(median);
      
       long startTime = getTimestamp();

       // TODO Write code that adds integer (numItems / 2)
       // to set.

       set.add(median);
      
       long endTime = getTimestamp();
       long totalTime = endTime - startTime;
       System.out.println(totalTime);
      
       return set;
   }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Second I just need someone to write an example code for the 2nd //TODO comment

public static LinkedHashSet<Integer> doLinkedHashSetRemoveMedian(int numItems) {
       System.out.print(\"doLinkedHashSetRemoveMedian: \");
       LinkedHashSet<Integer> set = new LinkedHashSet<>();

       // TODO Write code that adds integers 0 through (numitems - 1)
       // to set.

       long startTime = getTimestamp();

       // TODO Write code that removes the median element
       // from set, repeatedly until the set is empty.
  
       long endTime = getTimestamp();
       long totalTime = endTime - startTime;
      
       System.out.println(totalTime);
      
       return set;
   }

Solution

PART 1

Hi friend, first part you have done correctly except a small mistake. In first TODO you missed \'=\' sign:

public static HashSet<Integer> doHashSetInsertMedian(int numItems) {
System.out.print(\"doHashSetInsertMedian: \");
HashSet<Integer> set = new HashSet<>();
// TODO Write code that adds integers 0 through (numItems - 1)
// to set, inside a loop.
for(int i=0; i<=numItems-1; i++){
set.add(i);
}
  
// TODO Write code that removes integer (numItems / 2)
// from set.
int median = numItems / 2;
set.remove(median);
  
long startTime = getTimestamp();
// TODO Write code that adds integer (numItems / 2)
// to set.
set.add(median);
  
long endTime = getTimestamp();
long totalTime = endTime - startTime;
System.out.println(totalTime);
  
return set;
}

PART 2:

public static LinkedHashSet<Integer> doLinkedHashSetRemoveMedian(int numItems) {
System.out.print(\"doLinkedHashSetRemoveMedian: \");
LinkedHashSet<Integer> set = new LinkedHashSet<>();
// TODO Write code that adds integers 0 through (numitems - 1)
// to set.
for(int i=0; i<=numitems-1; i++)
set.add(i);

long startTime = getTimestamp();
// TODO Write code that removes the median element
// from set, repeatedly until the set is empty.
int median;
int current_size = set.size();
while(set.size() > 0){
current_size = set.size(); //getting current size of set
median = current_size/2; // finding current median
set.remove(median); // removing median
}
  
long endTime = getTimestamp();
long totalTime = endTime - startTime;
  
System.out.println(totalTime);
  
return set;
}

I need help with two things. First I\'d like someone to check over my code for the 3 //TODO statements below to verify that they\'re correct public static HashS
I need help with two things. First I\'d like someone to check over my code for the 3 //TODO statements below to verify that they\'re correct public static HashS

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site