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

Can you please Modify the “DomainBlocker” program using (1) HashSet and (2) Link

ID: 3846176 • Letter: C

Question

Can you please Modify the “DomainBlocker” program using (1) HashSet and (2) LinkedHashSet. Measure the efficiency of three different programs using TreeSet, HashSet, and LinedHashSet. Which implementation is most efficient?

//DomainBlocker.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.TreeSet;

/**
* A URL domain blocker.
*
* @author Java Foundations
* @version 4.0
*/
public class DomainBlocker
{
private TreeSet<String> blockedSet;

/**
* Sets up the domain blocker by reading in the blocked domain names from
* a file and storing them in a TreeSet.
* @throws FileNotFoundException
*/
public DomainBlocker() throws FileNotFoundException
{
blockedSet = new TreeSet<String>();

File inputFile = new File("blockedDomains.txt");
Scanner scan = new Scanner(inputFile);

while (scan.hasNextLine())
{
blockedSet.add(scan.nextLine());
}
}

/**
* Checks to see if the specified domain has been blocked.
*
* @param domain the domain to be checked
* @return true if the domain is blocked and false otherwise
*/
public boolean domainIsBlocked(String domain)
{
return blockedSet.contains(domain);
}
}

//DomainChecker.java

import java.io.FileNotFoundException;
import java.util.Scanner;

/**
* Domain checking driver.
*
* @author Java Foundations
* @version 4.0
*/
public class DomainChecker
{
/**
* Repeatedly reads a domain interactively from the user and checks to
* see if that domain has been blocked.
*/
public static void main(String[] args) throws FileNotFoundException
{
DomainBlocker blocker = new DomainBlocker();
Scanner scan = new Scanner(System.in);

String domain;

do
{
System.out.print("Enter a domain (DONE to quit): ");
domain = scan.nextLine();

if (!domain.equalsIgnoreCase("DONE"))
{
if (blocker.domainIsBlocked(domain))
System.out.println("That domain is blocked.");
else
System.out.println("That domain is fine.");
}
} while (!domain.equalsIgnoreCase("DONE"));
}
}

Explanation / Answer

CODE using HashSet:

//DomainBlocker.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.*;
/**
* A URL domain blocker.
*
* @author Java Foundations
* @version 4.0
*/
public class DomainBlocker
{
private HashSet<String> blockedSet;
/**
* Sets up the domain blocker by reading in the blocked domain names from
* a file and storing them in a HashSet.
* @throws FileNotFoundException
*/
public DomainBlocker() throws FileNotFoundException
{
blockedSet = new HashSet<String>();
File inputFile = new File("blockedDomains.txt");
Scanner scan = new Scanner(inputFile);
while (scan.hasNextLine())
{
blockedSet.add(scan.nextLine());
}
}
/**
* Checks to see if the specified domain has been blocked.
*
* @param domain the domain to be checked
* @return true if the domain is blocked and false otherwise
*/
public boolean domainIsBlocked(String domain)
{
return blockedSet.contains(domain);
}
}

//DomainChecker.java
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Domain checking driver.
*
* @author Java Foundations
* @version 4.0
*/
public class DomainChecker
{
/**
* Repeatedly reads a domain interactively from the user and checks to
* see if that domain has been blocked.
*/
public static void main(String[] args) throws FileNotFoundException
{
DomainBlocker blocker = new DomainBlocker();
Scanner scan = new Scanner(System.in);
String domain;
do
{
System.out.print("Enter a domain (DONE to quit): ");
domain = scan.nextLine();
if (!domain.equalsIgnoreCase("DONE"))
{
if (blocker.domainIsBlocked(domain))
System.out.println("That domain is blocked.");
else
System.out.println("That domain is fine.");
}
} while (!domain.equalsIgnoreCase("DONE"));
}
}

-----------------------------------------------------------------------------------
CODE using LinkedHashSet:

//DomainBlocker.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.*;
/**
* A URL domain blocker.
*
* @author Java Foundations
* @version 4.0
*/
public class DomainBlocker
{
private LinkedHashSet<String> blockedSet;
/**
* Sets up the domain blocker by reading in the blocked domain names from
* a file and storing them in a LinkedHashSet.
* @throws FileNotFoundException
*/
public DomainBlocker() throws FileNotFoundException
{
blockedSet = new LinkedHashSet<String>();
File inputFile = new File("blockedDomains.txt");
Scanner scan = new Scanner(inputFile);
while (scan.hasNextLine())
{
blockedSet.add(scan.nextLine());
}
}
/**
* Checks to see if the specified domain has been blocked.
*
* @param domain the domain to be checked
* @return true if the domain is blocked and false otherwise
*/
public boolean domainIsBlocked(String domain)
{
return blockedSet.contains(domain);
}
}

//DomainChecker.java
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Domain checking driver.
*
* @author Java Foundations
* @version 4.0
*/
public class DomainChecker
{
/**
* Repeatedly reads a domain interactively from the user and checks to
* see if that domain has been blocked.
*/
public static void main(String[] args) throws FileNotFoundException
{
DomainBlocker blocker = new DomainBlocker();
Scanner scan = new Scanner(System.in);
String domain;
do
{
System.out.print("Enter a domain (DONE to quit): ");
domain = scan.nextLine();
if (!domain.equalsIgnoreCase("DONE"))
{
if (blocker.domainIsBlocked(domain))
System.out.println("That domain is blocked.");
else
System.out.println("That domain is fine.");
}
} while (!domain.equalsIgnoreCase("DONE"));
}
}
--------------------------------------------------------------------------------------
Efficiency of Methods:

TreeSet: It is implemented using a tree structure (red-black tree). The elements in a set are sorted.
Complexity of methods like add, remove, and contains methods has time complexity of O(log (n)).
         
HashSet: It is implemented using a Hash table. The elements are not ordered.
Complexity of methods like add, remove, and contains methods have constant time complexity O(1).

LinkedHashSet: This is between HashSet and TreeSet. It is implemented as a Hash table same as HashSet.
The time complexity of basic methods is O(1).
             
As explained above, either HashSet OR LinkedHashSet implementation is efficient.