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.
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
/** * 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
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")); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
