Programming Assignment 5 (100 Points) Due: 11:59PM Thursday, November 2 START EARLY! Mining is arduous and dangerous work. Miners need to be taught how to mine minerals in the most efficient manner possible. For this PA, we will be creating a MiningTrainer where various miners will be evaluated on their mining speed and ability to extract the highest value from mineral veins. This program focuses on using interfaces, handling simple exceptions, handling command-line arguments, and reading from files. You will be creating a command-line java application that reads in mineral patterns from a file (or System.in) and processes them through various miners. There are 5 miner classes that we will be writing:,,, OddFirstEvenReverseMiner and. Miner is an interface and must be implemented by all miner classes. MiningTrainer is the command line program that will evaluate the various miners. README ( 10 Points ) You are required to provide a text file named README, NOT Readme.txt, README.pdf, or README.doc, with your assignment in your pa5 directory. There should be no file extension after the file name "README". Your README should include the following sections: Program Description ( 2 points ) : Provide a high level description of what your program does and how you can interact with it. Make this explanation such that your grandmother or uncle or someone you know who has no programming experience can understand what this program does and how to use it. Write your READMEs as if it was intended for a 5 year old. Do not assume your reader is a computer science major. Short Response ( 8 points ) : Answer the following questions in a couple of sentences: 1. (AI) How do you maintain your integrity even when you're stressed, pressured, or tired? 2. Why is so slow? 3. (Unix) What is the difference between Ctrl-C and Ctrl-D? 4. (Unix) How can you determine the number of lines in a file from the command line? (not opening vim) 5. (Unix) How can you determine the last time each file in a directory was edited? 6. (Vim) How do you search for all occurrences of the word taco in vim? (give the 1 line command) 7. (Vim) You have almost finished with PA5 when you realize that you misspelled PA5Strings as PA5strings (incorrect capitalization) in one of your files. In that file, you ve typed PA5strings a lot and so you don t want to have to change every misspelling by hand. What vim command can you use to change all instances of PA5strings to PA5Strings? 8. (Java) What is an interface?
STYLE ( 20 points ) Please refer to the style guidelines in the PA2 write-up. Note: Some of the guidelines have changed since PA1. In terms of grading, we will be using the style guidelines in PA2 for the rest of the PAs. CORRECTNESS ( 70 points ) Setting up your PA5 directory: We have provided two starter files for your use. You must copy them to your pa5 directory. mkdir ~/pa5 cp PUBLIC/pa5StarterFiles/PA5Strings.java ~/pa5/ cp PUBLIC/pa5StarterFiles/Miner.java ~/pa5/ Program Overview - About Mining Mineral patterns are modeled as strings. Here are the possible characters in a mineral pattern and their significance. '*' - Gem. The most valuable mineral in the mine. 'O' or 'o' - Ore. The least valuable mineral in the mine. 'X' or 'x' - Empty space. Miners don t want to mine this because they will get a penalty for wasting time (PENALTY). Sample mineral pattern: xoxoxoxox*** Note: All other characters are worth 0, and don t incur a penalty. For instance, if there is a mineral pattern oxmxo*, then the miners will mine the sequence normally, but would get 0 money for mining the m. Each character of the string represents a mining location. To get a character value from a string at a specified index, we can use charat()from the String class. MiningTrainer.java This class is the main driver of the program, where you parse the command line arguments, set up an input stream, create an instance of each miner type, make each miner mine according to their unique algorithm, and finally collect information about their mining time and earnings. Note: To avoid losing points because of mismatch of error messages and any other format strings with the reference output, make sure to use strings provided in PA5Strings.java. Refer to the SAMPLE OUTPUT section (see below) for more details of how the program should behave in different scenarios. Stage 1 : Parse the command line arguments Usage: java MiningTrainer SEED [-i INFILE] All command line options are case-sensitive (-i not -I). SEED is an integer that is to be used as the seed for random number generation -i INFILE : infile is the name of the input file.
If the -i flag was provided then it should have only one argument. If infile is put as -, then System.in should be used as the input stream. If the -i flag is not given, System.in is used. Stage 2 : Construct miners, perform mining tasks and print info Create an instance of Scanner class using the input stream. (Hint: you will find Scanner s hasnext() and next() methods helpful for implementing your program.) Create an array of miners. Iterate until the scanner has no more tokens to read and have each miner mine the read in sequence. If the input stream is System.in, print the instructions and prompt before accepting user input (string provided PA5Strings.java). Iterate over the miner array and perform mining tasks for each miner type (,,, and ). Print info about the time taken mining and total earnings for each miner when they finish mining. To print the miner s name, you can use minerobj.getclass().getname() Miner.java This is the interface that defines behaviors of a Miner. We make all Miner types (,,, and ) implement this interface so that they will have common defined behavior (i.e. method names). This makes it really convenient when you, say, want all Miner types to perform the mine() task. Now that they all have the same method named mine(), we can just call mine() for each of them in a simple loop in MinerTrainer. This interface defines the following public methods - 1. int mine(string minerals, int numgems) This method will process the string based on a mining algorithm until the entire string has been explored or all the gems have been found. It will return the time taken to mine all the gems. The miner will stop mining once it has found all the gems. 2. int gettotalearnings() This method returns the total money earned from all the mining operations. 3. default int appraisemineral(char mineral) This method will take in a character and determine its worth. It returns the monetary value of the mineral. Since this is a default method, it will be implemented in the interface itself. (You will have to implement this method yourself)..java This miner takes in a seed that is used to generate random numbers. You need to create a Random object in the constructor based on that seed (see how this was done in PA2Simulator). To begin, implement the mine(string minerals, int numgems) method. works by the following algorithm:
1. Pick a random location to mine at by calling nextint()to generate a random integer. 2. If we have already mined that location, apply a penalty (REPEAT_PENALTY = 2) to our total earnings for having wasted time and try a new random location (return to step 1) 3. If we have not mined that location, mark the location as visited, and get the value of the mineral at that location using appraisemineral and add it to the total earnings 4. If there are still gems left to mine, pick a new random location (return to step 1) 5. Return the time taken to mine the mineral sequence. Here are a few things that you need to keep track of: Locations you have already mined (an array might be helpful here). Number of gems found and how many yet to find. Total earnings made by This value does NOT reset each time mine() is called, it should be a running total. The time a takes to mine This value DOES reset each time mine() is called. Each time you pick a new location, the time should increase by MINE_RATE At the end of this method, you should return the time taken to mine all of the gems The last thing you need to do for is to implement the gettotalearnings() method..java This miner will sequentially mine all the EVEN indices in the string (beginning at 0), and then mine all ODD indices (starting at 1). First implement the mine(string minerals, int numgems) method. This method follows the following algorithm: 1. Go through all of the even indices, taking note of the value of each mineral and updating the total earnings accordingly. For each index you visit, the amount of time taken to mine should increase by MINE_RATE. 2. After you have either found all the gems, or visited all of the even indices, start visiting the odd indices. Complete the same process as you did with the even indices. 3. Once you have found all of the gems, or visited every index, the time spent mining should be returned. Lastly, implement the gettotalearnings() method..java The next miner you will be implementing is similar to the previous, except you need to visit the of the ODD indices first, then visit the EVEN indices. As with before, implement the mine() method first, then complete the gettotalearnings() method..java The next miner wants to try something different -- they want to visit all of the odd indices then visit all of the even indices IN REVERSE order.
To implement the mine(string minerals, int numgems) method, first visit all of the odd indices as you have done before, then, starting from the last even index, visit every even location in reverse until index 0. Don t forget to complete the gettotalearnings() method..java The last miner is very traditional, they don't want to do any fancy optimizations. This miner will simply visit each mining location in linear order until all of the gems are found, or every location has been mined. As with the other miners, first implement mine(), then complete the gettotalearnings() method. General notes: 1. After you have found all of the gems in the sequence, you should stop looking for more minerals (don't visit another index). 2. The total earnings for each Miner accumulate, this means that you should not reset the total earnings after or before each call to mine(), but earnings should start at 0 before the very first call to mine() 3. Every time you visit an index, you need to increase the total time spent mining by MINE_RATE. Note that this value should start at 0 at the beginning of each call to mine(). Sample Output Note: bolded text is what you type in the terminal. represents command prompts. 1. Command line parsing errors 1.1 Wrong number of arguments java MiningTrainer Usage: java MiningTrainer SEED [-i INFILE] 1.2 Invalid seed java MiningTrainer NotInteger Error: Invalid Integer Seed NotInteger 1.3 Invalid Flag java MiningTrainer 1234 -o Error: Unrecognized flag -o Usage: java MiningTrainer SEED [-i INFILE]
1.4 Missing argument to -i flag java MiningTrainer 1234 -i Error: Expected argument for -i flag Usage: java MiningTrainer SEED [-i INFILE] 2. User input via System.in 2.1 Program run with a seed value 17 and invalid sequence of minerals entered by the user via stdin: java MiningTrainer 17 Please enter a sequence of minerals. Minerals can be represented by the following characters: x,x: No minerals o,o: Low value ore. *: High value gems. End sequences by pressing the <ENTER> key. Finish by typing ctrl-d > mmmmmm Mining Minerals... mmmmmm Time taken for this sequence: 0 Money earned so far: 0 Time taken for this sequence: 0 Money earned so far: 0 Time taken for this sequence: 0 Money earned so far: 0 Time taken for this sequence: 0 Money earned so far: 0 Time taken for this sequence: 0 Money earned so far: 0 > ^d 2.2 Program run with a seed value 17 and the sequence "xo*" entered by the user via stdin: java MiningTrainer 17 Please enter a sequence of minerals. Minerals can be represented by the following characters: x,x: No minerals o,o: Low value ore. *: High value gems. End sequences by pressing the <ENTER> key. Finish by typing ctrl-d > xo* Mining Minerals...
xo* Time taken for this sequence: 8 Money earned so far: 7 Time taken for this sequence: 4 Money earned so far: 5 Time taken for this sequence: 6 Money earned so far: 9 Time taken for this sequence: 4 Money earned so far: 11 Time taken for this sequence: 6 Money earned so far: 9 > ^d 2.3 No input provided ( java MiningTrainer 1234) java MiningTrainer 1234 Please enter a sequence of minerals. Minerals can be represented by the following characters: x,x: No minerals o,o: Low value ore. *: High value gems. End sequences by pressing the <ENTER> key. Finish by typing ctrl-d > ^d 2.4 Input redirected from a file cat infile xooxxo***xooo*x**** java MiningTrainer 1234 < infile Please enter a sequence of minerals. Minerals can be represented by the following characters: x,x: No minerals o,o: Low value ore. *: High value gems. End sequences by pressing the <ENTER> key. Finish by typing ctrl-d > Mining Minerals... xooxxo***xooo*x**** Time taken for this sequence: 62 Money earned so far: 40
Time taken for this sequence: 32 > 2.5 Input piped into a different command echo '***oxoxxxxxoo*' java MiningTrainer 1234 Please enter a sequence of minerals. Minerals can be represented by the following characters: x,x: No minerals o,o: Low value ore. *: High value gems. End sequences by pressing the <ENTER> key. Finish by typing ctrl-d > Mining Minerals... ***oxoxxxxxoo* Time taken for this sequence: 126 Money earned so far: -72 Time taken for this sequence: 28 Money earned so far: 32 Time taken for this sequence: 18 Money earned so far: 36 Time taken for this sequence: 28 Money earned so far: 32 Time taken for this sequence: 28 Money earned so far: 32 > 3. User input via file 3.1 Program run with a seed 17 and an input file named infile cat infile xooxxo***xooo*x**** java MiningTrainer 17 -i infile Mining Minerals... xooxxo***xooo*x****
Time taken for this sequence: 150 Money earned so far: -42 Time taken for this sequence: 32 3.2 File with multiple lines cat infile_multiline oooxoxoxoxox***xoo *xoxoxo***xoxoxxxx java MiningTrainer 1234 -i infile_multiline Mining Minerals... oooxoxoxoxox***xoo Time taken for this sequence: 36 Money earned so far: 27 Time taken for this sequence: 32 Money earned so far: 43 Time taken for this sequence: 34 Money earned so far: 41 Time taken for this sequence: 24 Money earned so far: 21 Time taken for this sequence: 30 Money earned so far: 39 Mining Minerals... *xoxoxo***xoxoxxxx Time taken for this sequence: 70 Money earned so far: 17 Time taken for this sequence: 28 Money earned so far: 69 Time taken for this sequence: 28 Money earned so far: 79
Time taken for this sequence: 36 Money earned so far: 51 Time taken for this sequence: 20 Money earned so far: 73 3.3 Empty File touch empty_file java MiningTrainer 1234 -i empty_file 3.4 File doesn t exist java MiningTrainer 1234 -i nosuchfile Error: Could not open stream for reading EXTRA CREDIT ( 5 Points ) We have some new miners in town for the training, and they just so happen to be fans of the TV show Silicon Valley. Implement the following 2 Miners: EC_PiedPiperMiner.java (2.5 Points) Implements the famous middle-out algorithm. Starts from the middle and works its way outwards consuming one position per loop. Eg: Given a sequence of length 5, we would visit indices in the following order: 2 -> 1 -> 3 -> 0 -> 4 EC_HooliMiner.java (2.5 Points) Mines from the edges to the middle or until there are no gems left. This miner will mine the edges and then move one index inward on each side. Eg: Given a sequence of length 5, we would visit indices in the following order: 0 -> 4 -> 1 -> 3 -> 2 Create an EC version of MiningTrainer called EC_MiningTrainer.java that appends these miners to the array of regular miners. (Copy MiningTrainer.java to EC_MiningTrainer.java) Remember to switch the usage to EC_USAGE! SAMPLE OUTPUT: java EC_MiningTrainer 1234 -i infile
Mining Minerals... xooxxo***xooo*x**** Time taken for this sequence: 62 Money earned so far: 40 Time taken for this sequence: 32 EC_PiedPiperMiner EC_HooliMiner Time taken for this sequence: 34 Money earned so far: 68 TURNIN Turnin To turnin your code, navigate to your home directory and run the following command: cse11turnin pa5 You may turn in your programming assignment as many times as you like. The last submission you turn in before the deadline is the one that we will collect. Verify To verify a previously turned in assignment, cse11verify pa5 If you are unsure your program has been turned in, use the verify command. We will not take any late files you forgot to turn in. Verify will help you check which files you have successfully submitted. It is your responsibility to make sure you properly turned in your assignment. Files to be collected:.java
.java Miner.java MiningTrainer.java.java.java PA5Strings.java.java README Additional files to be collected for Extra Credit: EC_HooliMiner.java EC_PiedPiperMiner.java EC_MiningTrainer.java The files that you turn in (in your pa5 dir) must be EXACTLY the same name as those above, and no other.java source files. Any.java source file in your pa5 directory must compile without error for turnin. NO LATE ASSIGNMENTS ACCEPTED! START EARLY! And above all HAVE FUN!