Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topics String processing, ArrayList, text file handling, sequential search, binary search, and recursion Activities Write a program which will read a text file into an

image text in transcribed
image text in transcribed
image text in transcribed
Topics String processing, ArrayList, text file handling, sequential search, binary search, and recursion Activities Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., "sorted Strings.txt") contains the words already sorted for your convenience. Read a search key word (i.e., String) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. Refer to "Searchint.java" (given in "SearchString.zip") and the following UML diagram for the details of required program components (including the class name, data fields, and methods) for this assignment. Search String +TO_STOP: String +NOT FOUND: Int count: int count: it count3:int +sequentialSearch(array ArrayList value: Stringit +binary Search(array ArrayList, low:int, high-int, valor: String); int +maineveld HINTS & STEPS: (H1) Declare the two symbolic constants as public static, and final. To be more specific, declare them as follows: public static final String TO_STOP="-1"; and public static final int NOT_FOUND=-1; (H2) Declare countl, count2, and count3 as static, each of which keeps track of number of comparison operations in sequentialSearch(). iterative binarySearch(), and recursive binarySearch(), respectively. (H3) binarySearch(array: ArrayList, value: String) is the header for iterative binary search method and binary Search(array: ArrayList, low: int, high: int, value: String) is the header for recursive binary search method. (H4) Every search method should be written a separate method, which returns the index in the instance of ArrayList if the search key word exists, or returns - 1(i.e., NOT_FOUND) if the search key word doesn't exist. (HS) First, declare all the first four methods (i.c., sequentialSearch(), the two binary Search()s, and main() to be static (i.e., static(=class) methods) and complete the remaining program accordingly. After you confirm that your program works properly as shown in the running example, declare the first three methods (sequentialSearch() and the two binary Search(s) to be non-static (i.e., non-static(=instance) methods, commenting out the corresponding method headers. ArrayList, low:int, high: int value: String) is the header for recursive binary search method. (H4) Every search method should be written a separate method, which returns the index in the instance of ArrayList if the search key word exists, or returns -10... NOT_FOUND) if the search key word doesn't exist (H5) First, declare all the first four methods (ie., sequentialSearch(), the two binary Search0s, and main()) to be static (i.e. static class) methods) and complete the remaining program accordingly. After you confirm that your program works properly as shown in the running example, declare the first three methods (sequentialSearch and the two binary Search(s) to be non-static (i.e., non-static(-instance) methods, commenting out the corresponding method headers. What do you need to change in your static main if you want to use the non-static(=instance) methods. Briefly discuss (or explain this in your program file (H6) Declare the fifth method (i.c., main() as a non-static (i.e., instance) method in "SearchString java". Notice that this non-static main() does not have parameters and thus this main() is different from the static main(String[] args) in terms of using a different modifier as well as a different signature. By the way, is it fine to declare main() like this? If yes, what are you practicing now with this? Overloading or overriding? Otherwise, explain why it is not valid. (17) Implement a driver class (say "TestSearchString java") and show how to use call the methods in "SearchString.java" to get the same output shown as running example below. Briefly discuss (or explain) how to call static(class) methods and non-static-instance) methods in *SearchString java" from the driver ("TestSearchString.java"). TextSearch String mier: Stragili vald (18) What do you need to change if you change the access modifier from public to private after step (H7) for all the constants and variables in the data field? How about if you change all the occurrence of public (ie, both in data field and each method header) as private? Were you able to run your program either in ScarchString.java or in TestSearchString java? (H9) What if you don't have the two main()s in ScarchString class. What do you need to change in main() in TestScarchString class to get the exactly same result you get at step (H7) with either all public data fields or all private data fields? Running Example Typer the file traded Stres. 1.3040 words are populated in the instance of Amaya Type a ward to search (I to op computer requential and computer is found in 042425 terative in Search computer is found in 3404 cursive bin Scarchi computer is found 24 cm Type award to search to stop since mala) cicho is found in 10026 Kerative binary Search Site is forand in 10027cm) (6) Declare the fifth method (i.e., main() as a non-static (i.e., instance) method in "SearchString.java". Notice that this non-static main() does not have parameters and thus this main() is different from the static main(String[] args) in terms of using a different modifier as well as a different signature. By the way, is it fine to declare main() like this? If yes, what are you practicing now with this? Overloading or overriding? Otherwise, explain why it is not valid. (H7) Implement a driver class (say "Test SearchString.java") and show how to use call the methods in "SearchString.java" to get the same output shown as running example below. Briefly discuss (or explain) how to call static(=class) methods and non-static(=instance) methods in "SearchString.java" from the driver ("TestSearchString.java"). TestSearchString + main args: Stringb: vold (H8) What do you need to change if you change the access modifier from public to private after step (H7) for all the constants and variables in the data field? How about if you change all the occurrence of public (i.e, both in data field and each method header) as private? Were you able to run your program either in SearchString.java or in TestSearchString.java? (H9) What if you don't have the two main()s in SearchString class. What do you need to change in main() in TestSearchString class to get the exactly same result you get at step (17) with either all public data fields or all private data fields? Running Example Type the filename ot read sorted Strings.txt 13040 words are populated in the instance of ArrayList Type a word to search (-1 to stop): computer sequentialSearch) computer is found in [2424] (comparisce-2425) iterative binary Search: computer is found in 12424} (comparisce-11) recursive binary Search() computer is found in [2424] (compariscein) Type a word to search (-1 10 stop) science sequentialSearch) science is found in 10027] (comparison-100289 iterative binary Search: science is found in 10027] (comparison-13). recursive binaryScarch) science is found in 110027] (comparis-13). Type a word to search (-1 to stop) jasa sequentialSearch) java is not found (compariso-13040) iterative binary Search(): java is not found comparison-13). recursive binary Search) java is not found (comparison-13). Type a word to search (-1 to stop:-1 Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Know how to use reservations systems to inventory demand.

Answered: 1 week ago