Description
Or A project for all of us who find ourselves frequently out of the loop… Recursion!
The purpose of this programming project is to demonstrate the functionality of Recursion while reviewing such things as the use of Sets (here only a single Set will be implemented), String manipulation, file I/O using the Scanner class for the input file.
REQUIREMENTS
Though there will be several instances where iteration would solve the problem, Absolutely NO loops should be found in any of your code! It is a requirement of this assignment that all repeated processes will be performed through recursion.
You will submit a single file “Assignment8.java” (NO Zipped File) through the Programming Assignment 8 Submission link on Canvas. This file will be your solution to this assignment. Too, you can use the file I posted on Canvas or create your own test input file “In8.txt” BUT, you will NOT include this in8.txt in the upload to Canvas. I will use my own for testing.
Not only will you be graded on program correctness (Program executes correctly, proper use of methods, classes, inheritance, etc.) but also, good programming documentation techniques as we have learned during this course. 1. Assignment8.java All methods used to solve this problem will be contained in a single file that includes main. Of course, these methods will be declared as static. At very least, you are to write 3 separate recursive methods to solve this problem. Their intent is as follows: getWordsString: Recursively read all the words contained in the input file and return a String containing only the words found in the file that contain a given char (The words within this String will be separated by a space). Multiple return statements will be allowed here. hasCharacter: Recursively scan the characters within a received word to see if it contains a given char and return true if it does, false otherwise. getWordSet: Recursively scan the words in a received String where each word is separated by a space and placing each word into a Set (HashSet) which is returned to the calling program once all the words have been scanned. Multiple return statements will be allowed here. Other details of each of these methods follows the discussion on main below. main: This program is to read all the words contained in the input file, one at a time recursively until no words remain. To do this main will call a recursive method named getWordsString (discussed shortly) which is passed a Scanner to a File object and a single character. getWordsString will read all the words in the file and return a String containing all words from the input file that contain a single given character. Keep in mind, this string must have a single space inserted between each word.
In order to remove redundant words, the String returned from getWordsString will be sent to another recursive method “getWordSet” which will ‘pick’ out each word in the String and add them to a HashSet (this will remove all duplicates). The HashSet will be returned to main for final output. Details on getWordSet are listed below. main will output this Set to out8.txt (No special formatting here. Just print the Set to the output file “out8.txt” with a single println statement). This program MUST read a file named in8.txt and generate an output file named out8.txt. The in8.txt file must be created by you based on formatting described shortly. In8.txt and out8.txt are NOT to be included during the submission process. public static String getWordsString(Scanner theFile, char theC) The method getWordsString is passed the open input file through a Scanner and a single char (your choice) and returns a String of words (each separated by a space) found in the file that contain the char argument. This method will recursively read each word contained in the input file. As each word is read, you will check to determine if it contains the given character through a call to the method hasCharacter discussed below This means you can only use length, indexOf, substring, and charAt. You are not allowed to use any other existing String methods or other pre-written methods to determine if the char is in the String. You have to write this process. If the current word contains the given char, then this word should be concatenated onto a String along with a space to separate each word. This will be tied in with the return statement that includes the recursive call. When all words in the file have been processed, getWordsString will return this String of words that contain the given character. Multiple return statements will be allowed here. public static Boolean hasCharacter(String theS, char theC) The hasCharacter method will recursively check the first character of the continuously smaller substring for the given character until the length of the substring reduces to 0, at which point no match was found and the method should return false. However, if the length is 0 and a match is found, hasCharacter should return true. This means there are 2 base cases here. For all other cases, the length is 0 AND the character is not found, you should return the result of the recursive call.
You can use the charAt method here. Also, though the character being used for testing is lower case, don’t forget to check for the uppercase equivalent. toUpperCase or any other pre-written methods are NOT allowed. You have to make this conversion yourself (ASCII ‘a’ is 32 greater than ‘A’, ‘b’ is 32 greater than ‘B;, and so on).
public static Set