CISC181 S2017 Lab5
Preliminaries
- Make a new project with n = 5 (following these instructions)
- Name your main class "Lab5" (when creating a new module in the instructions above, in the Java class name field)
- Modify Lab5.java by adding your name and section number in a comment before the Lab5 class body.
Instructions
In this lab you will analyze text files and get experience working with strings and characters.
WordStats
In this exercise you will repeatedly prompt the user in the console to enter the name of a text file (relative to a base directory of your choosing) or 'q' to quit. If they do not want to quit, open the file with FileInputStream and read it with a Scanner, using this regular expression as your delimiter/word separator:
"[\\s.!?,;:\\-()_\"]+"
Be careful about cutting and pasting this into Android Studio. I have seen extra backslashes inserted automatically for several students, so make sure your delimiter string matches what you see here
After reading every word in the file, print the following information:
- Number of words
- Longest word. Note that if there are multiple words which "tie", the expected behavior is to output the first one found
- Word with most vowels. Treat 'y' as a consonant
- Alphabetically first word with 4 or more letters (treating upper-case and lower-case the same). Do not count words that start with a non-alphabetic character
- Alphabetically last word with 4 or more letters (treating upper-case and lower-case the same)
After printing this information, make sure to close the file, then prompt the user again until they want to quit.
All of this should be be in a public class WordStats. WordStats should have a constructor which takes the base directory string as its sole parameter, and handles the console input and looping itself. Note: FileInputStream's constructor wants the full-path name of the file. The user should only have to enter the simple file name. So the WordStats constructor parameter is the full path to the directory that the files live in, then concatenate that with the filename typed by the user
Please instantiate your WordStats class by creating an object in main(). You should test it with the following files:
Here is the expected output for the above files (you don't have to print it in this format--these are just the right values):
- getty.txt: 268 words, longest word = "proposition", word with most vowels = "proposition", first word = "above", last word = "years"
- doi.txt: 1325 words, longest word = "undistinguished", word with most vowels = "naturalization", first word = "abdicated", last word = "would"
- bts.txt: 14574 words, longest word = "obstreperousness", word with most vowels = "qualifications", first word = "abate", last word = "youth"
- greatexp.txt: 186685 words, longest word = "architectooralooral", word with most vowels = "architectooralooral", first word = "a'most", last word = "zest"
InterpretTurtle
First, download this reference implementation of the Turtle class from Lab 5 and add it to your Lab 6 project, as well as TurtleTester.java from Lab 5. Make sure to change the package name to "cisc181.mylab_5" in TurtleTester.java.
For this exercise you will write a simple file parser that will turn text files into sequences of Turtle instructions and execute them. A Turtle program is a variable-length text file, with a name ending in ".turtle", that has a Turtle instruction on each line. Each instruction consists of a name followed by a parenthesis-enclosed, comma-separated list of arguments. There will be no space between the instruction name and the left parenthesis, but there may or may not be whitespace in the argument list. Case should not matter.
There are two categories of instructions. First is a category which handles turtle initialization. The start instruction has either 3 arguments or 5 arguments, corresponding to the different Turtle constructor options. There may be at most one start() instruction per turtle program, and it must be on the first line.
- start(x, y, direction). direction should be the whole word "EAST" or "East" or "east" (remember, case does not matter)
- start(x, y, direction, forward, lateral)
When you read a start() instruction, call the Turtle constructor with the appropriate arguments that you have just parsed. If the first instruction is not start(), call the zero-argument constructor.
The second category are the standard move instructions from Lab 5:
- move(n)
- turn(n)
- flip(doForward, doLateral). doForward and doLateral should be the full words "TRUE", "True", or "true" or "FALSE", "False", or "false".
Each time you read one of these you should call the corresponding Turtle method with the appropriate arguments.
Call TurtleTester's print() method after and only after start() (or the zero-argument constructor) and move() instructions.
All of this should be in a public class InterpretTurtle. Like WordStats, the InterpretTurtle constructor should take a string representing the base directory where turtle programs live, then ask the user repeatedly which program to run (or 'q' to quit). InterpretTurtle should make a TurtleTester object, handle opening and closing the turtle program files, and calling TurtleTester's startTest() and endTest() methods before and after parsing each program. If you encounter any unexpected or erroneous input, print an error message and exit the program.
Try the following sample programs, which are translations of the first three tests from Lab 5:
Output should be exactly the same as if you had written a test function for each of these in Lab 5.
Submission
Submit the following 3 files:
- Lab5.java
- WordStats.java
- InterpretTurtle.java
Make sure to add your name and section number to all 3!