Class name: Dir
Method name: dir
Parameters: String, String[]
Returns: String[]
Almost all (if not all) operating systems provide users with the possibility to
arrange their files into folders (aka 'catalogs' and 'directories'). Because
people have many files on their machines, some means of filtering is required.
Descendants of DOS, for example, provide a 'dir' command that can be used to
view all files in a specific folder. There is an option to specify a filtering
pattern that restricts the files that we want to see, filtering them by name.
Patterns are like normal file names, with the exception that they can contain
two other specially treated characters: '*' and '?'. Asterisks (*) are used to
represent zero or more chracters. Question marks represent exactly one
character.
So for example a pattern like win*.??? will match:
win.com
win32.sys
winipcfg.exe
win.jav
But will not match:
wincom
win.co
win.java
win.com.temp
Notice that, in contrast to DOS:
1. There can be more than one * in the pattern
2. Characters after * are meaningful (e.g. "w*nt.exe" does not match
"win32.exe" as in DOS)
3. The . is not a special character in any way (e.g. "w*e" matches "win32.exe")
4. *.* matches only files that contain the character '.'
Implement a class Dir that contains a method dir. dir takes a pattern
(represented as a String) and a list of file names (represented as a String[]).
The result is another list of Strings (again String[]) that represents only
those names that match the given pattern. Names should appear in the order
they appeared in the original list. Names are case sensitive.
The method signature is (Be sure your method is public):
String[] dir (String pattern, String[] contents);
INPUT:
*pattern is 1 to 50, inclusive, characters long and contains only 'a'..'z',
'A'..'Z', '0'..'9', '.', '?' and '*'
*contents contains up to 50 String objects, each of which can contain from 1 to
50, inclusive, characters that contain only 'a'..'z', 'A'..'Z', and '0'..'9'
Examples:
pattern: *.exe
contents: [prog.java, cmd.exe, test.bat.exe, list.com, picture.png, main.tmp.ex]
result: [cmd.exe, test.bat.exe]
pattern: a*b*c
contents: [abc, aaabbbccc, ababac, aaaccc, babc, cabc, abbbbbc, ac]
result: [abc, aaabbbccc, ababac, abbbbbc]
pattern: ??*?
contents: [a, aa, aaa, bbbb, bbbbb, cccccc]
result: [aaa, bbbb, bbbbb, cccccc]