Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Week 4 review This exercise is a simplified demonstration of some of the basics of word searching found in search engines like Google, Bing, and
Week review
This exercise is a simplified demonstration of some of the basics of word searching found in search engines like Google, Bing, and Yahoo. The application uses files and folders on your local hard drive and has limited search rules.
Since this review exercise focuses on File IO and Exceptions, you're responsible for writing the parts of the application that handle files and exceptions. The fully commented search engine code is provided.
Essentially, you'll write the code to gather the files and folders to be searched. Once you have the list of files, you'll open each file, read its contents, and feed it to the search engine.
You'll also create a logging class, which you'll use to log the progress of the application to a file. Finally, you'll use various custom exception classes to report errors, which are handled by the application.
Step One: Getting started
First, open the week four review project in IntelliJ, and review the initial code:
Application.java is the main class of the application.
SearchDomain.java represents the search domain used by the search engine.
SearchDomainException.java extends Exception and reports SearchDomainspecific exceptions.
SearchEngine.java contains the indexing and search code that makes up the search engine.
SearchEngineException.java extends Exception and reports SearchEnginespecific exceptions.
TELogException.java extends RuntimeException and reports TELogspecific exceptions. You'll implement TELog in this exercise.
You'll find test data files for you to use in the data folder. There are six text files, each with a single line.
Step Two: Create the TELog class and log application activity
Searching for text is limited to file reading. There's no writing to files. However, since this exercise focuses on reading and writing to files, you'll record all application activity by writing to a log file.
Begin by creating the TELog class in the com.techelevator.util package, and adding a single public static method named logString message which takes a String parameter and returns void.
You'll handle any exceptions internally within log by catching them, capturing their message into a new TELogException, and throwing the new exception.
Note: you don't need to say the log method throws TELogException since the exception is an "unchecked" exception.
The log method must open logssearchlog for writing, and append the message to the end of the log file. Then, it must close the file.
Note: the logs folder already exists in the root of the project.
Once you've written the TELog class, log the message "Search application started" after the Step Two comment in Application.java.
Step Three: Complete the SearchDomain class
The SearchDomain gathers the list of files to search based on the folder name passed into its constructor. For instance, given the following folder and files:
datafiletxt
datafiledoc
datafiledat
datafiletxt
datafiledoc
datafiledat
A call to the constructor SearchDomaindata builds the internal list of files, as the example shows.
Your job is to complete the private List buildDomain method by looping through the folder and gathering filenames.
Locating the folder and looping through the files may cause exceptions. You'll handle them internally within buildDomain by catching them, capturing their message into a new SearchDomainException, and throwing the new exception. Use the SearchDomainException, which extends Exception, and add throws SearchDomainException to buildDomain
Step Four: Instantiate a SearchDomain object
Under the Step Four comment in Application.java, instantiate a new SearchDomain, passing "data" as the parameter.
Log the new SearchDomain after instantiating it There's a toString method defined in the class that prints out the names of the files indexed.
Step Five: Index files
Now that the SearchDomain has been established, you'll work on the SearchEngine class. The SearchEngine has two important methods: indexFiles and search Before performing any searches, the SearchEngine must prepare its index.
The SearchEngine class has only one constructor, which takes an instance of SearchDomain. Complete the indexFiles method where you see the Step Five comment.
In the indexFiles method, do the following:
Call SearchDomain's getFiles to retrieve the list of filenames in the domain.
Loop through the files in the list using a for loop.
Open each file and read the contents of the file one line at a time.
Pass each line to the private indexWords method, which is described below.
The indexWords method has two parameters: the line as a String and an int named fileID. The fileID is the index of the current file in the list of files.
Searching for multiple words is similar to searching for a single word: a list of files where the words were found is returned, but the order of the list is more refined.
Using the same instance, call searchtelephoneiles method loops through
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started