Part 1:

Often when we are running a program, it will have a number ofconfiguration options which tweak its behavior while it’s running.Allow text completion? Collect anonymous usage data? These are alloptions our programs may use. We can store these options in anarray and pass it to the program. In its simplest form, we canimagine 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 keywhich identifies it within an options list (an array of strings).That is, if the key is in the options list, then the option isconsidered to be set. The class will implement the code whichdetects whether the option is found, along with some supportingfunctionality.

Any data elements should be declared private. If you think it isnecessary you may include helper methods of your own. The classshould 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. Forexample, we can have an option identified in a list of options (anarray of strings) by the key “s1” and have the name “Switch number1”.
  • public boolean isFound() Reports the value ofa flag indicating whether or not the option has been found yet. Theflag begins as false, but will change to true if the option isfound at some point in the string array (options list).
  • public String getKey() A getter which willreturn the value of the key string.
  • public String getName() A getter which willreturn the value of the name string.
  • public int numSlots() The number of slotstaken up by this option within a string array (options list). Thiswill return 1 (but we can imagine that if we were to includeattribute values, it could be more).
  • public boolean match(String[] s, int i) Checksthe option at position i of string array (options list) s to see ifit’s the same option (meaning that the option is found), and if so,set the internal flag to true. Return true or false depending onwhether there was a match. For example:

    > Option o = new Option(“mode_priv”, “Privatebrowsing”);
    > String[] s = {“no_cookies”, “mode_priv”,”java_enabled”};
    > System.out.println(o.isFound());
    > System.out.println(o.match(s, 0));
    > System.out.println(o.match(s, 1));
    > System.out.println(o.isFound());

  • @Override public String toString() Returns thename of the option, followed by a colon, space, and either “yes” or”no” depending on whether the option was found. For example:

    > Option o = new Option(“d”, “Debug mode”);
    > System.out.println(o);
    Debug mode: no

Part 2:

Now, we will write an OptionProcessor class which collectsseveral Options, and uses it to detect which ones of those optionsare found within a given string array (options list). So imaginethat we have three Option objects, “Option 1” (with key “o1″),”Option 2” (with key “o2”), and “Option 3” (with key “o3”), and weare handed an options list {“o2”, “o1”}. We would look at the listand see that Option 1 and Option 2 are set, while Option 3 is notset (it’s not part of the list). This class will have aprocess()method which goes through the provided list one by one inorder to determine which of our stored Options are part of thelist. Thus, by the end of the array, we would know which optionsare set and which are not.

The class should implement the following public methods:

  • public OptionProcessor() Initialize an optionprocessor with no available built-in options.
  • public void add(Option o) Add a type of Optionto be checked for in a string array (options list).
  • public void process(String[] input) Processthe options list contained in input, using the selection ofpossible Options which have been added previously, to determinewhich options are set. Example:

    > OptionProcessor op = new OptionProcessor();
    > op.add(new Option(“4wd”, “Four wheel drive”));
    > op.add(new Option(“bswarn”, “Blind spot warning”));
    > op.add(new Option(“ebr”, “Emergency braking”));
    > op.add(new Option(“xwarn”, “Rear cross-trafficwarning”));
    > op.add(new Option(“nav”, “Navigation system”));
    > String[] oplist = {“bswarn”, “xwarn”, “ebr”, “nav”};
    > op.process(oplist);
    > System.out.println(op.toString());
    Four wheel drive: no
    Blind spot warning: yes
    Emergency braking: yes
    Rear cross-traffic warning: yes
    Navigation system: yes

    Here is a suggested approach to do this: we would want to firststart at the begining of input the list, and continue until we’vechecked everything in the list. At every position, check each ofthe stored Option objects to see if there is a match. If so, movethe position forward by numSlots() (which is just 1 positionforward for ordinary Options, but we’ll see later why we may wantto skip more places for other types of Option). If no Optionmatches at the current position, then skip it and move on to thenext position.

  • @Override public String toString() Returns theconcatenation of the toString() output of each of the stored Optionobjects, separated by newlines. See above.

Part 3:

Now, we will write the StringOption class which extends Option.Did you wonder why we needed the numSlots() method in Option, orwhy we incremented by numSlots() in OptionProcessor.process()? It’sbecause sometimes we have some kind of attributes associated withan option. For example, we may have a name option which has anadditional field which stores the actual name. We will do this byusing two spots in the string array (options list), where the firstone identifies the option as before, while the second one is thetext associated with the option. So where our “Option 1” may haveshown up as {“op1″} in a string array (options list), we may have a”Name” option which shows up as {“n”, “George Mason”}. OurStringOption class uses an extra member to store the text attributeassociated with the option. Since we have already written Optionand we have the power of inheritance at our disposal, we do notneed to do too much to make this work.

The class should implement the following public methods:

  • public StringOption(String key, String name, StringdefValue) Initialize the key and name as before, but now,since we have an attribute value as well, use defValue to indicatethe default value of the attribute.
  • @Override public int numSlots() This shouldreturn 2 (instead of 1 like before – now we use two slots in thestring array (options list), because the second slot is theattribute value).
  • public String getValue() A getter whichreturns the attribute value
  • @Override public boolean match(String[] s, inti) Now, in addition to what the method did previously, ifthe option matches it should save the attribute value from indexi+1 of the array.
  • @Override public String toString() This shouldbehave like the parent’s toString(), except that instead ofprinting yes or no, it should print the text of the attribute.
  • https://mason.gmu.edu/~iavramo2/classes/cs211/s19/E6tester.java