Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA write the following classes and pass the test cases: Write a class, Option, which encodes whether or not a named option has been

IN JAVA write the following classes and pass the test cases:

  1. Write a class, Option, which encodes whether or not a named option has been seen in an option list (an array of Strings.
  2. Write the class OptionProcessor which will store a collection of Options and use it to check which options are found within a list of strings (options).
  3. Write the class StringOption, a subclass of Option which allows a string attribute to be included with the option.

Option Task: 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.
  • @Override public String toString() Returns the name of the option, followed by a colon, space, and either "yes" or "no" depending on whether the option was found.

OptionProcessor Task 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. Here is a suggested approach to do this: we would want to first start at the begining of input the list, and continue until we've checked everything in the list. At every position, check each of the stored Option objects to see if there is a match. If so, move the position forward by numSlots() (which is just 1 position forward for ordinary Options, but we'll see later why we may want to skip more places for other types of Option). If no Option matches at the current position, then skip it and move on to the next position.
  • @Override public String toString() Returns the concatenation of the toString() output of each of the stored Option objects, separated by newlines.

StringOption Task:

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.

Ensure the program passes the following test cases :

import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class E6tester { public static void main(String args[]){ org.junit.runner.JUnitCore.main("E6tester"); } @Test public void option_construct() { Option o = new Option("op1", "Option 1"); assertEquals("incorrect key value", "op1", o.getKey()); assertEquals("incorrect option name", "Option 1", o.getName()); assertEquals("number of slots should be 1", 1, o.numSlots()); assertFalse("isFound() should initially be false", o.isFound()); } @Test public void option_match() { for (int i = 0; i < 5; i++) { String[] s = {"y", "n", "y", "y", "n"}; boolean expected = s[i].equals("y"); Option o = new Option("y", "test option"); String errMsg = String.format("improper match detection at position %d of %s", i, Arrays.toString(s)); assertEquals(errMsg, expected, o.match(s, i)); errMsg = String.format("incorrect isFound() output after position %d of %s", i, Arrays.toString(s)); assertEquals(errMsg, expected, o.isFound()); String expectStr = String.format("test option: %s", (expected ? "yes" : "no")); errMsg = String.format("incorrect toString() output after position %d of %s", i, Arrays.toString(s)); assertEquals(errMsg, expectStr, o.toString()); } } @Test public void optionprocessor_construct() { OptionProcessor op = new OptionProcessor(); assertEquals("OptionProcessor should initially have an empty toString()", "", op.toString()); } @Test public void optionprocessor_add() { OptionProcessor op = new OptionProcessor(); String expected = ""; for (int i = 0; i < 5; i++) { op.add(new Option("op" + i, "Option " + i)); expected += String.format("Option %d: no ", i); } assertEquals("incorrect output from adding Options", expected, op.toString()); } @Test(timeout=1000) public void optionprocessor_process() { String[] opList = {"op1", "op0", "op5", "op4"}; boolean[] expected = {true, true, false, false, true, true, false, false}; String expString = ""; Option[] optObjs = new Option[8]; OptionProcessor op = new OptionProcessor(); for (int i = 0; i < optObjs.length; i++) { optObjs[i] = new Option("op" + i, "Option " + i); op.add(optObjs[i]); expString += String.format("Option %d: %s ", i, (expected[i] ? "yes" : "no")); } op.process(opList); String errMsg = String.format("incorrect result of processing options list %s", Arrays.toString(opList)); assertEquals(errMsg, expString, op.toString()); } @Test(timeout=1000) public void optionprocessor_skipopt() { String[] opList = {"unknown", "op0"}; boolean[] expected = {true, false}; String expString = ""; Option[] optObjs = new Option[expected.length]; OptionProcessor op = new OptionProcessor(); for (int i = 0; i < optObjs.length; i++) { optObjs[i] = new Option("op" + i, "Option " + i); op.add(optObjs[i]); expString += String.format("Option %d: %s ", i, (expected[i] ? "yes" : "no")); } op.process(opList); String errMsg = String.format("incorrect result of processing options list %s", Arrays.toString(opList)); assertEquals(errMsg, expString, op.toString()); } @Test public void stringoption_construct() { StringOption o = new StringOption("op1", "Option 1", "default"); assertTrue("StringOption should inherit from Option", o instanceof Option); assertEquals("incorrect key value", "op1", o.getKey()); assertEquals("incorrect option name", "Option 1", o.getName()); assertEquals("incorrect option attribute value", "default", o.getValue()); assertEquals("number of slots should be 2", 2, o.numSlots()); assertFalse("isFound() should initially be false", o.isFound()); } @Test public void stringoption_match() { for (int i = 0; i < 5; i++) { int j = 2*i; String[] s = {"y", "s0", "n", "s1", "y", "s2", "y", "s3", "n", "s4"}; boolean expected = s[j].equals("y"); StringOption o = new StringOption("y", "test option", "def value"); String errMsg = String.format("improper match detection at position %d of %s", j, Arrays.toString(s)); assertEquals(errMsg, expected, o.match(s, j)); errMsg = String.format("incorrect isFound() output after position %d of %s", j, Arrays.toString(s)); assertEquals(errMsg, expected, o.isFound()); String expectStr = String.format("test option: %s", (expected ? "s"+i : "def value")); errMsg = String.format("incorrect toString() output after position %d of %s", j, Arrays.toString(s)); assertEquals(errMsg, expectStr, o.toString()); } } @Test(timeout=1000) public void stringoption_process() { String[] opList = {"sop1", "s1", "op0", "sop5", "s5", "op4"}; boolean[] expFound = {true, true, false, false, true, true, false, false}; String[] expValue = {"yes", "s1", "no", "def3", "yes", "s5", "no", "def7"}; String expString = ""; Option[] optObjs = new Option[8]; OptionProcessor op = new OptionProcessor(); for (int i = 0; i < optObjs.length; i++) { if (i%2 == 0) { optObjs[i] = new Option("op" + i, "Option " + i); expString += String.format("Option %d: %s ", i, expValue[i]); } else { optObjs[i] = new StringOption("sop" + i, "String Option " + i, "def" + i); expString += String.format("String Option %d: %s ", i, expValue[i]); } op.add(optObjs[i]); } op.process(opList); String errMsg = String.format("incorrect result of processing options list %s", Arrays.toString(opList)); assertEquals(errMsg, expString, op.toString()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions

Question

5. How would you describe your typical day at work?

Answered: 1 week ago

Question

7. What qualities do you see as necessary for your line of work?

Answered: 1 week ago