Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Required Skills Inventory Declare and instantiate an ArrayList Write a method according to given specifications Write a method that returns an ArrayList Write a method

Required Skills Inventory
Declare and instantiate an ArrayList
Write a method according to given specifications
Write a method that returns an ArrayList
Write a method that takes an ArrayList as an argument
Open and read input from a text file
Use a Scanner to read input from a text file
Use String methods to identify and compare specific character in a String
Implement a simple linear search over a collection
Compose a program out of simple methods
Problem Description
For this project you will construct a program that will search a list for words that match a specified pattern. For example, given the following list of words:
amble
apple
box
echoes
fox
school
tumble
and the pattern:
_cho__
the program will find and print out:
[echoes, school]
Note that the only words in the list of seven words above that match the pattern _cho__ are echoes and school. These two words match the pattern because the pattern specifies a six letter word that has the letters c, h, and o as the second, third, and fourth letters. In the pattern, an underscore (_) specifies any single letter.
_ c h o __
e c h o e s
s c h o o l
The project can be broken down into the following methods:
getWordList(String) : ArrayList The purpose of this public static method is to open a text file containing a list of words and return an ArrayList that contains all the words in the file. This method should be defined to throw a FileNotFoundException using the throws keyword. Later, this ArrayList will be searched to find words that match a specified pattern.
When called and passed a file name (like "words.txt"), this method will declare and instantiate a new ArrayList, then open the specified text file. The method will then read in each word in the file and add each word to the ArrayList. After all words have been read and added this method will return the ArrayList.
Note: The file will always contain a list of words with one word on each line, and each line ending with a newline (
) character.
Hint: The Scanner constructor method throws a FileNotFoundException when attempting to access a file.
isMatchAtIndex(String, String, int) : boolean A public static method that when called and passed a word (String), and a pattern (String), and an index (int), the method will determine if the character at the index in the pattern is a match for the character at the same index in the word. The method will return true if the characters are a match, otherwise it will return false.
Example 1
isMatchAtIndex("school","_cho__",2) would return true because the character at index 2 in the pattern "_cho__"('h') is an exact match for the character at index 2 in the word "school".
Example 2
isMatchAtIndex("school","_chi__",3) would return false because the character at index 3 in the pattern "_chi__"('i') is not a match for the character at index 3 in the word "school" ('o').
Example 3
isMatchAtIndex("school","_cho__",0) would return true because the character at index 0 in the pattern "_cho__"('_') is an underscore and an underscore is a match for any character.
Example 4
isMatchAtIndex("school","_cho_",5) would return false because there is no character at index 5 in the pattern "_cho_".
isMatch(String, String) : boolean A public static method that when called and passed a word (String), and a pattern (String), will determine if the specified word matches the specified pattern. The method will return true if the word and pattern match, otherwise it will return false.
Note that if a word does match a pattern, it is because isMatchAtIndex(word, pattern, index) returns true for each index.
Example 1
isMatch("school","_cho__") would return true because all character in the pattern match the character at the same index in the word. Remember that an underscore character ('_') in the pattern matches any letter at that same index in the word.
Example 2
isMatch("school","_chi__") would return false because the character at index 3 in the pattern "_chi__"('i') is not a match for the character at index 3 in the word "school" ('o'), and therefore the word "school" does not match the pattern "_chi__".
Example 3
isMatch("apple","a_p_") would return false because the word and the patter are not the same length, so they cannot be a match.
getMatches(ArrayList, String) : ArrayList Write a public static method named getMatches. This method will take an ArrayList as its first argument and a String as its second argument. The method will return an Arraylist. When called and passed an word list (ArrayList), and a pattern (String), the method will find all of the words in the word list that match the specified pattern, add these to a new ArrayList, and then return this new ArrayList.
Given an ArrayList named wordList that contains the following words:
amble
apple
box
echoes
fox
school
tumble
Example 1
getMatches(wordList,"_cho__") woul

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_2

Step: 3

blur-text-image_3

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

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions