Question
Please use Java only. Write the class OptionProcessor which will store a collection of Options and use it to check which options are found within
Please use Java only.
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).
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. 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); }
Please make sure that it passes the following: https://repl.it/@micky123/OutstandingKnottyBookmark
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