Question
Option Task: ( 3pts ) Often when we are running a program, it will have a number of configuration options which tweak its behavior while
Option Task:
(3pts) Often when we are running a program, it will have a number of configuration options which tweak its behavior while it's running. Allow text completion? Collect anonymous usage data? These are all options our programs may use. We can store these options in an array and pass it to the program. In its simplest form, we can imagine that if an option shows up in the array, then it is set, and if it does not show up in the array, then it is not set.
Our first task is to encode an Option class, which has a key which identifies it within an options list (an array of strings). That is, if the key is in the options list, then the option is considered to be set. The class will implement the code which detects whether the option is found, along with some supporting functionality.
Any data elements should be declared private. If you think it is necessary you may include helper methods of your own. The class should implement the following public methods:
- public Option(String key, String name) Initialize an option which will be recognized using the given key, and which has a full English description given in name. For example, we can have an option identified in a list of options (an array of strings) by the key "s1" and have the name "Switch number 1".
- public boolean isFound() Reports the value of a flag indicating whether or not the option has been found yet. The flag begins as false, but will change to true if the option is found at some point in the string array (options list).
- public String getKey() A getter which will return the value of the key string.
- public String getName() A getter which will return the value of the name string.
- public int numSlots() The number of slots taken up by this option within a string array (options list). This will return 1 (but we can imagine that if we were to include attribute values, it could be more).
- public boolean match(String[] s, int i) Checks the option at position i of string array (options list) s to see if it's the same option (meaning that the option is found), and if so, set the internal flag to true. Return true or false depending on whether there was a match.
OptionProcessor Task:
(3pts) Now, we will write an OptionProcessor class which collects several Options, and uses it to detect which ones of those options are found within a given string array (options list). So imagine that we have three Option objects, "Option 1" (with key "o1"), "Option 2" (with key "o2"), and "Option 3" (with key "o3"), and we are handed an options list {"o2", "o1"}. We would look at the list and see that Option 1 and Option 2 are set, while Option 3 is not set (it's not part of the list). This class will have a process()method which goes through the provided list one by one in order to determine which of our stored Options are part of the list. Thus, by the end of the array, we would know which options are set and which are not.
The class should implement the following public methods:
- public OptionProcessor() Initialize an option processor with no available built-in options.
- public void add(Option o) Add a type of Option to be checked for in a string array (options list).
- public void process(String[] input) Process the options list contained in input, using the selection of possible Options which have been added previously, to determine which options are set
StringOption Task:
(3pts) Now, we will write the StringOption class which extends Option. Did you wonder why we needed the numSlots() method in Option, or why we incremented by numSlots() in OptionProcessor.process()? It's because sometimes we have some kind of attributes associated with an option. For example, we may have a name option which has an additional field which stores the actual name. We will do this by using two spots in the string array (options list), where the first one identifies the option as before, while the second one is the text associated with the option. So where our "Option 1" may have shown up as {"op1"} in a string array (options list), we may have a "Name" option which shows up as {"n", "George Mason"}. Our StringOption class uses an extra member to store the text attribute associated with the option. Since we have already written Option and we have the power of inheritance at our disposal, we do not need to do too much to make this work.
The class should implement the following public methods:
- public StringOption(String key, String name, String defValue) Initialize the key and name as before, but now, since we have an attribute value as well, use defValue to indicate the default value of the attribute.
- @Override public int numSlots() This should return 2 (instead of 1 like before - now we use two slots in the string array (options list), because the second slot is the attribute value).
- public String getValue() A getter which returns the attribute value
- @Override public boolean match(String[] s, int i) Now, in addition to what the method did previously, if the option matches it should save the attribute value from index i+1 of the array.
- @Override public String toString() This should behave like the parent's toString(), except that instead of printing yes or no, it should print the text of the attribute.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started