Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview: Write an interface, Splittable, which describes some type of entity which can be split into two halves. Write the class SplittableString which is essentially

Overview:

  1. Write an interface, Splittable, which describes some type of entity which can be split into two halves.
  2. Write the class SplittableString which is essentially a string with the characteristic that it can be split into a first half and a second half.
  3. Write the class Interval which defines an integer interval on a number line.
  4. Write the class SplittableArray, which is an ArrayList of strings which can be split into two smaller subarrays.
  5. Download and use a tester module to ensure that your programs are correct.
  6. Prepare the assignment for submission and submit it.

Splittable Task:

(2pts) 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.

SplittableString Task:

(2pts) 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").

Interval Task:

(2pts) 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)).

SplittableArray Task:

(3pts) 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, so most of the array functionality will come pre-written for us, but we will also implement Splittable. Note that a class like this is one of the reasons we want to use interfaces: we would not have been able to derive from both ArrayList and Splittable if the latter had been a class. Also note that when we implement the Splittable interface, we will not need to implement a size() method because it already exists as part of the class!

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.

image text in transcribed

Jur splittabla intertoce shl include diree pabli: ethad: These SPLITTANESTKING TAK: :bl1a Spl1ttahat.ring Inilialia the s it the iwat string value. . publie char chacAt nt : Rec.cns the charactbit poenom-muse string. * &OvucradM publie ing tust ngI( : Returns the contents of the strap + pr blie Splittuble:: zq ir:lluit i Keams the fns, na t ha crn if te string is ooc length then the nrst half sting is te laper of te two (for example, te irst half at the String psuara 15 prcq . * li I ta l as r int mtanzes the low d and high + publie int Lew : Returns the lower bound oc th merval. +bl int qh() Keturns the upper houder the interval. bounds cfthe interval. e pn ate vanables uch old these va uts should be ces red- sul because we cont imen e conge them or ce ev r set e this s one o the Teasans we want to use mer aces: we wou c nct have been able derive trom bath herayLi t d been a s ass. he splitt bi a so imp en ent split: b e that 2 c ass becoase it alreedy exists zs part or the cles! nd Splitt ble the aner so no e a: en e m e en tr er ace e u noc need to Lm ement a i me The splittablakzzay clea sould impeme the tollawing publsc methods: *bli : Splittablalt"ny'r : Inticli?es the class. Na12 dat this construenar will iteed te da verv littie. Lid is enssiblF unnecessary (ul em ry det ault would he used i1steun

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

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

Explain the need for a new field of financial therapy.

Answered: 1 week ago