add the following methods listed below to the SortedArrayCollection class shown in screen shot public String toString() // Creates and returns a string that correctly represents the current collection. public T smallest() // Returns null if the collection is empty, otherwise returns the smallest element of the collection. public int greater(T element)  // Returns a count of the number of elements e in the collection that are greater then element, that is such that e.compareTo(element) is > 0 public MySortedArrayCollection combine (MySortedArrayCollectionother)  // Creates and returns a new SortedArrayCollection object that is a combination of this object and the argument object. public T []toArray() // Returns an array containing all of the elements of the collection. public void clear() // Removes all elements. public boolean equals(Object o) // Takes an Object argument, returning true if it is equal to the current collection and false otherwise public boolean addAll(MySortedArrayCollectionc) // Takes a MySortedArrayCollection argument and adds its contents to the current collection; returns a boolean indicating success or failure. public boolean retainAll(MySortedArrayCollectionc) // Takes a MySortedArrayCollection argument and removes any elements from the current collection that are not in the argument collection; returns a boolean indicating success or failure. public void removeAll(MySortedArrayCollectionc){ // Takes a MySortedArrayCollection argument and removes any elements from the current collection that are also in the argument collection. then write a driver program to test that the methods listed above were implemented corre

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

add the following methods listed below to the SortedArrayCollection class shown in screen shot

public String toString()

// Creates and returns a string that correctly represents the current collection.

public T smallest()

// Returns null if the collection is empty, otherwise returns the smallest element of the collection.

public int greater(T element) 

// Returns a count of the number of elements e in the collection that are greater then element, that is such that e.compareTo(element) is > 0

public MySortedArrayCollection <T> combine (MySortedArrayCollection<T>other) 

// Creates and returns a new SortedArrayCollection object that is a combination of this object and the argument object.

public T []toArray()

// Returns an array containing all of the elements of the collection.

public void clear()

// Removes all elements.

public boolean equals(Object o)

// Takes an Object argument, returning true if it is equal to the current collection and false otherwise

public boolean addAll(MySortedArrayCollection<T>c)

// Takes a MySortedArrayCollection argument and adds its contents to the current collection; returns a boolean indicating success or failure.

public boolean retainAll(MySortedArrayCollection<T>c)

// Takes a MySortedArrayCollection argument and removes any elements from the current collection that are not in the argument collection; returns a boolean indicating success or failure.

public void removeAll(MySortedArrayCollection<T>c){

// Takes a MySortedArrayCollection argument and removes any elements from the current collection that are also in the argument collection.

then write a driver program to test that the methods listed above were implemented correctly

### SortedArrayCollection Class Implementation

The `SortedArrayCollection<T>` class implements the `CollectionInterface<T>`. It is a generic class designed to store elements in a sorted array. Here is a breakdown of its components:

#### Fields

- `protected final int DEFCAP = 100;`
  - Default capacity for the array.

- `protected int origCap;`
  - Stores the original capacity of the array.

- `protected T[] elements;`
  - Array to hold the elements of the collection.

- `protected int numElements = 0;`
  - Tracks the number of elements currently in the collection.

- `protected boolean found;`
  - A flag to indicate if the target element is found in the array.

- `protected int location;`
  - Represents the location of the target if found, or the insertion point if not found.

#### Constructors

- **Default Constructor**
  - Initializes the collection with the default capacity `DEFCAP` and sets `origCap`.

- **Parameterized Constructor**
  - Accepts an integer `capacity` to define the size of the elements array and sets `origCap`.

#### Methods

- `protected void enlarge()`
  - Increases the capacity of the collection:
    - **Creates a larger array:** Allocates a new array with additional space equal to `origCap`.
    - **Copies contents:** Transfers existing elements to the new larger array.
    - **Reassigns the reference:** Updates the `elements` reference to the new larger array.

- `protected void find(T target)`
  - Searches for a target element in the array:
    - **Initializes `location` and `found`:** Sets `location` to 0 and `found` to false.
    - **Checks if empty:** Calls `recFind` if the array is not empty to find or decide the position.

- `protected void recFind(T target, int first, int last)`
  - A recursive helper method used by `find`:
    - **Determines the `location`:** Calculates the middle of the current range.
    - **Performs comparison:**
      - Uses `compareTo` for comparison of `target` with current element.
      - Updates `found` and `location` based on the comparison result.
      - Calls itself recursively to adjust the range when the target is not found.

This class is useful for managing collections that need to maintain a sorted order and
Transcribed Image Text:### SortedArrayCollection Class Implementation The `SortedArrayCollection<T>` class implements the `CollectionInterface<T>`. It is a generic class designed to store elements in a sorted array. Here is a breakdown of its components: #### Fields - `protected final int DEFCAP = 100;` - Default capacity for the array. - `protected int origCap;` - Stores the original capacity of the array. - `protected T[] elements;` - Array to hold the elements of the collection. - `protected int numElements = 0;` - Tracks the number of elements currently in the collection. - `protected boolean found;` - A flag to indicate if the target element is found in the array. - `protected int location;` - Represents the location of the target if found, or the insertion point if not found. #### Constructors - **Default Constructor** - Initializes the collection with the default capacity `DEFCAP` and sets `origCap`. - **Parameterized Constructor** - Accepts an integer `capacity` to define the size of the elements array and sets `origCap`. #### Methods - `protected void enlarge()` - Increases the capacity of the collection: - **Creates a larger array:** Allocates a new array with additional space equal to `origCap`. - **Copies contents:** Transfers existing elements to the new larger array. - **Reassigns the reference:** Updates the `elements` reference to the new larger array. - `protected void find(T target)` - Searches for a target element in the array: - **Initializes `location` and `found`:** Sets `location` to 0 and `found` to false. - **Checks if empty:** Calls `recFind` if the array is not empty to find or decide the position. - `protected void recFind(T target, int first, int last)` - A recursive helper method used by `find`: - **Determines the `location`:** Calculates the middle of the current range. - **Performs comparison:** - Uses `compareTo` for comparison of `target` with current element. - Updates `found` and `location` based on the comparison result. - Calls itself recursively to adjust the range when the target is not found. This class is useful for managing collections that need to maintain a sorted order and
### Educational Explanation of Collection Methods in Java

This document provides an explanation of methods used to manage a collection in Java. This example appears to be part of a class that handles elements generically (`T` represents a generic type).

#### Method Descriptions

1. **`public boolean add(T element)`**
   - **Precondition:** The element is comparable to previously added objects.
   - **Purpose:** Adds an element to the collection.
   - **Process:**
     - Checks if the collection is full, calling `enlarge()` if necessary.
     - Finds the correct location for the element.
     - Shifts elements to make room.
     - Inserts the element and increments `numElements`.
     - Returns `true` upon successful insertion.

2. **`public boolean remove(T target)`**
   - **Purpose:** Removes a specified element from the collection.
   - **Process:**
     - Searches for the target element with `find(target)`.
     - If found:
       - Shifts subsequent elements to fill the gap.
       - Decrements `numElements`.
       - Returns `true`.
     - Otherwise, returns `false`.

3. **`public int size()`**
   - **Purpose:** Returns the current number of elements in the collection.
   - **Returns:** `numElements`.

4. **`public boolean contains(T target)`**
   - **Purpose:** Checks if the collection contains a specific element.
   - **Returns:** `true` if the element is found, otherwise `false`.

5. **`public T get(T target)`**
   - **Purpose:** Retrieves a specific element from the collection.
   - **Returns:** The element if found, otherwise `null`.

6. **`public boolean isEmpty()`**
   - **Purpose:** Checks if the collection is empty.
   - **Returns:** `true` if `numElements` is zero, otherwise `false`.

7. **`public boolean isFull()`**
   - **Purpose:** Checks if the collection is full.
   - **Note:** This collection is unbounded, so it always returns `false`.

#### Explanation of Key Concepts

- **Generic Type (`T`)**: Allows the class to handle any object type, making it versatile and reusable.
- **Dynamic Sizing**: The collection can grow in size using an `enlarge()` method, which is called when the current capacity is reached.
- **Element Shifting**: Elements are moved
Transcribed Image Text:### Educational Explanation of Collection Methods in Java This document provides an explanation of methods used to manage a collection in Java. This example appears to be part of a class that handles elements generically (`T` represents a generic type). #### Method Descriptions 1. **`public boolean add(T element)`** - **Precondition:** The element is comparable to previously added objects. - **Purpose:** Adds an element to the collection. - **Process:** - Checks if the collection is full, calling `enlarge()` if necessary. - Finds the correct location for the element. - Shifts elements to make room. - Inserts the element and increments `numElements`. - Returns `true` upon successful insertion. 2. **`public boolean remove(T target)`** - **Purpose:** Removes a specified element from the collection. - **Process:** - Searches for the target element with `find(target)`. - If found: - Shifts subsequent elements to fill the gap. - Decrements `numElements`. - Returns `true`. - Otherwise, returns `false`. 3. **`public int size()`** - **Purpose:** Returns the current number of elements in the collection. - **Returns:** `numElements`. 4. **`public boolean contains(T target)`** - **Purpose:** Checks if the collection contains a specific element. - **Returns:** `true` if the element is found, otherwise `false`. 5. **`public T get(T target)`** - **Purpose:** Retrieves a specific element from the collection. - **Returns:** The element if found, otherwise `null`. 6. **`public boolean isEmpty()`** - **Purpose:** Checks if the collection is empty. - **Returns:** `true` if `numElements` is zero, otherwise `false`. 7. **`public boolean isFull()`** - **Purpose:** Checks if the collection is full. - **Note:** This collection is unbounded, so it always returns `false`. #### Explanation of Key Concepts - **Generic Type (`T`)**: Allows the class to handle any object type, making it versatile and reusable. - **Dynamic Sizing**: The collection can grow in size using an `enlarge()` method, which is called when the current capacity is reached. - **Element Shifting**: Elements are moved
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education