Question
**** 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***
We understand the notion of splitting something into two parts. If we have a string, we can split it into a first half and a second half, so "programmer" can be split into "progr" and "ammer"; an interval on a number line can be split into two smaller intervals; and an array can be split into two smaller subarrays, each containing half of the elements in the list. So we will define an interface called Splittable which we use to describe a type of class which can be split into two halves. Any Splittable class is guaranteed to do three things: it has a size, it has a method to retrieve its first half, and it has a method to retrieve its second half.
Why are we making Splittable an interface instead of a class which we can derive from? Because the types of objects which are splittable may be conceptually very different. We can make a splittable array or a splittable string or a splittable interval, but these are relatively different types of classes: they may already derive from different existing classes, and even the notion of how splitting is supposed to work is different for each of the three. Instead of committing ourselves to a common base class, we only promise that each one has a notion of splitting.
Our Splittable interface should include three public methods:
- public int size() The size of the Splittable.
- public Splittable firstHalf() Returns the first half of the Splittable. If the halves are uneven in size (for example, an odd-length string), then we will adopt the convention that the first half is the larger of the two halves.
- public Splittable secondHalf() Returns the second half of the Splittable.
All method signatures should match the ones shown exactly.
Now, we will write a String wrapper which is splittable. We will call the class SplittableString and it will implement Splittable. If our splittable string contains a string value, then the first and second half will be determined by substrings containing the first and second half of the string, respectively. Thus, if our string is "noon", then our first half is "no" and our second half is "on".
Note that since the String class itself is final, we are not allowed to create subclasses of String. However, we can create a wrapper instead - make a a class which contains a String member and which has a few methods which look and behave like String's methods.
The class should implement the following public methods:
- public SplittableString(String s) Initialize the splittable string with the given string value.
- public int size() Returns the length of the string (this is one of the interface's methods).
- public int length() Returns the length of the string (in order to behave the same as String).
- public char charAt(int i) Returns the character at position i in the string.
- @Override public String toString() Returns the contents of the string.
- public SplittableString firstHalf() Returns the first half substring. If the string is odd-length, then the first half string is the larger of the two (for example, the first half of the string "program" is "prog").
- public SplittableString secondHalf() Returns the second half substring. If the string is even-length, then the second half string is the smaller of the two (for example, the second half of the string "program" is "ram").
An interval defines a portion of a number line. For example, the interval [1, 3) is the set of numbers from 1 to 3, including 1 but not including 3. The size of the interval is 2 (there is a distance of 2 between 1 and 3), and it can be split into two half-intervals: [1, 2) and [2, 3). We will write an Interval class which implements Splittable and can return one of two half-intervals. The class should implement the following public methods:
- public Interval(int a, int b) Initializes the low (a) and high (b) bounds of the interval. The private variables which hold these values should be declared final, because we don't intend to change them once they're set.
- public int low() Returns the lower bound of the interval.
- public int high() Returns the upper bound of the interval.
- public int size() Returns the total size of the interval.
- @Override public String toString() Returns the bounds of the interval in the following format: "[0, 10)".
- public Interval firstHalf() Returns the first half interval. Since the intervals are integer intervals, it's possible that one half-interval is larger and one is smaller, in which case the first half is the larger of the two (for example, if the interval is [1, 10)then the first half interval is [1, 6)).
- public Interval secondHalf() Returns the second half interval. Since the intervals are integer intervals, it's possible that one half-interval is larger and one is smaller, in which case the second half is the smaller of the two (for example, if the interval is [1, 10) then the second half interval is [6, 10)).
Similarly to how we created a splittable string which let us split into two half-strings, we will now create a SplittableArray class which lets us split an array into two half-sized subarrays. We will extend this class from ArrayList
The SplittableArray class should implement the following public methods:
- public SplittableArray() Initializes the class. Note that this constructor will need to do very little, and is possibly unnecessary (an empty default would be used instead).
- public SplittableArray firstHalf() Returns an array containing a subarray of the elements from the first half of the array.
- public SplittableArray secondHalf() Returns an array containing a subarray of the elements from the second half of the array.
Tester File:
- https://mason.gmu.edu/~iavramo2/classes/cs211/s19/E7tester.java
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