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

1)Write a method public static void downsize(LinkedList<String> employeeNames, i

ID: 3705258 • Letter: 1

Question

1)Write a method

public static void downsize(LinkedList<String> employeeNames, int n)


that removes every nth employee from a linked list.


2)Implement the sieve of Eratosthenes: a method for computing prime numbers, known to the ancient Greeks. This method will compute all prime numbers up to n.

Choose an n.
First insert all numbers from 2 to n into a set.
Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . . Erase all multiples of 3; that is, 6, 9, 12, 15, . . . . Go up to ?n.
Then print the set.

3)Write a program that keeps a map in which both keys and values are strings—the names of students and their course grades. Prompt the user of the program to add or remove students, to modify grades, or to print all grades. The printout should be sorted by name and formatted like this:

Carl: B+
Joe: C
Sarah: A

4)Insert all words from a large file (such as the novel War and Peace) into a hash set and a tree set. Time the results. Which data structure is more efficient?

Explanation / Answer

1) Method to delete n'th employee from a linked list

(The below method is writeen in java)

import java.util.LinkedList;

public static void downsize(LinkedList<String> employeeNames, int n)

{

Node head = employeeNames.head;

if(head == null) //if list is empty

return;

Node temp = head; //store the head

if(n == 0)

{

head = temp.next; //head shifted

return;

}

for(int i = 0; temp!=null; && i < n-1; i++)

{

temp = temp.next;

}

if(temp == null || temp.next == null)

return;

Node next = temp.next.next;

temp.next = next;

}

2).

class Prime

{

void getPrimeNumbers(int n)

{

// Create a boolean array "prime[0..n]" and initialize

// all entries it as true. A value in prime[i] will

// finally be false if i is Not a prime, else true.

boolean prime[] = new boolean[n+1];

for(int i=0;i<n;i++)

prime[i] = true;

for(int p = 2; p*p <=n; p++)

{

// If prime[p] is not changed, then it is a prime

if(prime[p] == true)

{

// Update all multiples of p

for(int i = p*2; i <= n; i += p)

prime[i] = false;

}

}

// Print all prime numbers

for(int i = 2; i <= n; i++)

{

if(prime[i] == true)

System.out.print(i + " ");

}

}

public static void main(String args[])

{

int n = 100;

System.out.print("Following are the prime numbers ");

System.out.println("smaller than or equal to " + n);

Prime g = new Prime();

g.getPrimeNumbers(n);

}

}