Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. **** *** ALSO MAKE SURE IT PASS THE TESTER FILE

**** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. ****

*** ALSO MAKE SURE IT PASS THE TESTER FILE PLEASE***

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. For example:

    > Option o = new Option("mode_priv", "Private browsing"); > String[] s = {"no_cookies", "mode_priv", "java_enabled"}; > System.out.println(o.isFound()); false > System.out.println(o.match(s, 0)); false > System.out.println(o.match(s, 1)); true > System.out.println(o.isFound()); true

  • @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. For example:

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

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. 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-traffic warning")); > 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 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. See above.

In past assignments, we have used standard Java arrays, while noting that those arrays cannot be resized without being recreated. It is far more convenient to use a dynamically-resizing array, and Java does provide classes which do this. In this lab, we will use a class called an ArrayList, which implements a dynamically-resizing array. We would use the datatype ArrayList

to identify it (it's an "ArrayList of Options" - the notation is something which we'll discuss later when we talk about generics). Unfortunately, we can't use brackets with an ArrayList, but it has get, set, and add methods, plus it can be iterated over using a foreach-style loop. To use an ArrayList, java.util.ArrayList must be imported. Example:

ArrayList

opArr = new ArrayList(); opArr.add(new Option("o1", "Option 1")); opArr.add(new Option("o2", "Option 2")); Option o = opArr.get(0); // o is now option 1 opArr.set(0, new Option("o3", "Option 3")); for (Option op : opArr) { System.out.println(op); }

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.

Example:

> OptionProcessor op = new OptionProcessor(); > op.add(new StringOption("n", "Name", "")); > String[] s = { "n", "George Mason" }; > op.process(s); > System.out.println(op); Name: George Mason

Tester file:

  • https://mason.gmu.edu/~iavramo2/classes/cs211/s19/E6tester.java

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

Step: 3

blur-text-image

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

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

What is treasury stock and why is it acquired?

Answered: 1 week ago