Question

In: Computer Science

Create MySortedArrayCollection.java This file is inherited from SortedArrayCollection.java This file implements MySortedArrayCollectionInterface.java ****** Make new and...

Create MySortedArrayCollection.java

  • This file is inherited from SortedArrayCollection.java
  • This file implements MySortedArrayCollectionInterface.java

****** Make new and just complete MySortedArrayCollection.java, dont modify others

////////////////////////////////////////////////////////

SortedArrayCollection.java

public class SortedArrayCollection<T> implements CollectionInterface<T> {
   protected final int DEFCAP = 100; // default capacity
   protected int origCap; // original capacity
   protected T[] elements; // array to hold collection elements
   protected int numElements = 0; // number of elements in this collection

   // set by find method
   protected boolean found; // true if target found, otherwise false
   protected int location; // indicates location of target if found,
                           // indicates add index if not found

   public SortedArrayCollection() {
       elements = (T[]) new Object[DEFCAP];
       origCap = DEFCAP;
   }

   public SortedArrayCollection(int capacity) {
       elements = (T[]) new Object[capacity];
       this.origCap = capacity;
   }

   protected void enlarge()
   // Increments the capacity of the collection by an amount
   // equal to the original capacity.
   {
       // Create the larger array.
       T[] larger = (T[]) new Object[elements.length + origCap];

       // Copy the contents from the smaller array into the larger array.
       for (int i = 0; i < numElements; i++) {
           larger[i] = elements[i];
       }

       // Reassign elements reference.
       elements = larger;
   }

   protected void find(T target)
   // Searches elements for an occurrence of an element e such that
   // e.equals(target). If successful, sets instance variables
   // found to true and location to the array index of e. If
   // not successful, sets found to false and location to the
   // array index where target should be inserted.
   {
       location = 0;
       found = false;
       if (!isEmpty())
           recFind(target, 0, numElements - 1);
   }

   protected void recFind(T target, int first, int last)
   // Used by find.
   {
       int result; // result of the comparison
       if (first > last) {
           found = false;
           result = ((Comparable) target).compareTo(elements[location]);
           if (result > 0)
               location++; // adjust location to indicate insert index
       } else {
           location = (first + last) / 2;
           result = ((Comparable) target).compareTo(elements[location]);
           if (result == 0) // found target
               found = true;
           else if (result > 0) // target too high
               recFind(target, location + 1, last);
           else // target too low
               recFind(target, first, location - 1);
       }
   }

   public boolean add(T element)
   // Precondition: element is Comparable to previously added objects
   //
   // Adds element to this collection.
   {
       if (numElements == elements.length)
           enlarge();

       find(element); // sets location to index where element belongs

       for (int index = numElements; index > location; index--)
           elements[index] = elements[index - 1];

       elements[location] = element;
       numElements++;
       return true;
   }

   public boolean remove(T target)
   // Removes an element e from this collection such that e.equals(target)
   // and returns true; if no such element exists, returns false.
   {
       find(target);
       if (found) {
           for (int i = location; i <= numElements - 2; i++)
               elements[i] = elements[i + 1];
           elements[numElements - 1] = null;
           numElements--;
       }
       return found;
   }

   public int size()
   // Returns the number of elements on this collection.
   {
       return numElements;
   }

   public boolean contains(T target)
   // Returns true if this collection contains an element e such that
   // e.equals(target); otherwise, returns false.
   {
       find(target);
       return found;
   }

   public T get(T target)
   // Returns an element e from this collection such that e.equals(target);
   // if no such element exists, returns null.
   {
       find(target);
       if (found)
           return elements[location];
       else
           return null;
   }

   public boolean isEmpty()
   // Returns true if this collection is empty; otherwise, returns false.
   {
       return (numElements == 0);
   }

   public boolean isFull()
   // This collection is unbounded so always returns false.
   {
       return false;
   }
}
///////////////////////////////////////////////////////

MySortedArrayCollectionInterface.java

public interface MySortedArrayCollectionInterface<T> extends CollectionInterface<T> {
   public String toString();
   // Creates and returns a string that correctly represents the current
   // collection.
   // Such a method could prove useful for testing and debugging the class and for
   // testing and debugging applications that use the class.
   // Assume each stored element already provides its own reasonable toString
   // method.

   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.
}

//////////////////////////////////////////////

CollectionInterface.java

public interface CollectionInterface<T> {
   boolean add(T element);
   // Attempts to add element to this collection.
   // Returns true if successful, false otherwise.

   T get(T target);
   // Returns an element e from this collection such that e.equals(target).
   // If no such e exists, returns null.

   boolean contains(T target);
   // Returns true if this collection contains an element e such that
   // e.equals(target); otherwise returns false.

   boolean remove(T target);
   // Removes an element e from this collection such that e.equals(target)
   // and returns true. If no such e exists, returns false.

   boolean isFull();
   // Returns true if this collection is full; otherwise, returns false.

   boolean isEmpty();
   // Returns true if this collection is empty; otherwise, returns false.

   int size();
   // Returns the number of elements in this collection.
}

Solutions

Expert Solution

MySortedArrayCollection.java


public class MySortedArrayCollection<T extends Comparable<T>> extends SortedArrayCollection<T> implements MySortedArrayCollectionInterface<T> {

        // Creates and returns a string that correctly represents the current
        // collection.
        public String toString() {
                T elements[] = this.toArray();
                String k = "[ ";
                k = k + elements[0];
                for (int i=1; i<elements.length; i++) {
                        k = k + ", " + elements[i];
                }
                k=k+"]";
                return k;
        }
        
        // Returns null if the collection is empty, otherwise returns the smallest
        // element of the collection.
        public T smallest() {
                if(this.isEmpty())
                        return null;
                T elements[] = this.toArray();
                T small=elements[0];
                for(int i=1;i<this.size();i++)
                {
                        if(elements[i].compareTo(small)<0)
                        {
                                small=elements[i];
                        }
                }
                return small;
        }

        // 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 int greater(T element) {
                int count=0;
                if(this.isEmpty())
                        return 0;
                T elements[] = this.toArray();
                for (int i=0; i<this.size(); i++) {
                        if (elements[i].compareTo(element)>0) {
                                count++;
                        }
                }
                return count;
                
        }

        // Creates and returns a new SortedArrayCollection object that is a combination
        // of this object and the argument object.
        public MySortedArrayCollection<T> combine(MySortedArrayCollection<T> other) {
                T[] a=this.toArray();
                T[] b=other.toArray();
                MySortedArrayCollection<T> k = new MySortedArrayCollection<T>();
                for (int i=0; i<a.length; i++) {
                        k.add(a[i]);
                }
                for (int i=0; i<b.length; i++) {
                        k.add(b[i]);
                }
                return k;
        }

        // Returns an array containing all of the elements of the collection.
        public T[] toArray() {
                return elements;
        }

        // Removes all elements.
        public void clear()
        {
                T elements[] = this.toArray();
                for(int i=this.size()-1; i>=0;i--)
                {
                        this.remove(elements[i]);
                }
        }

        // Takes an Object argument, returning true if it is equal to the current
        // collection and false otherwise
        public boolean equals(Object o) {
                return (this.equals(o));
        }

        // Takes a MySortedArrayCollection argument and adds its contents to the current
        // collection;
        // returns a boolean indicating success or failure.
        public boolean addAll(MySortedArrayCollection<T> c) {
                T arr[] = c.toArray();
                for (int i=0; i<arr.length; i++) {
                        if(!this.add(arr[i]))
                                return false;
                }
                return true;
        }

        // 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 boolean retainAll(MySortedArrayCollection<T> c) {
                T arr[] = c.toArray();
                for (int i=0; i<arr.length; i++) {
                        if (!this.contains(arr[i])) {
                                if (!this.remove(arr[i])) {
                                        return false;
                                }
                        }
                }
                return true;
        }

        // Takes a MySortedArrayCollection argument and removes any elements from the
        // current collection that are also in the argument collection.
        public void removeAll(MySortedArrayCollection<T> c) {
                for(int i=0;i<c.size();i++)
                {
                        this.remove(elements[i]);
                }
        }
}

MySortedArrayCollectionInterface.java

public interface MySortedArrayCollectionInterface<T extends Comparable<T>> extends CollectionInterface<T> {
   public String toString();
   // Creates and returns a string that correctly represents the current
   // collection.
   // Such a method could prove useful for testing and debugging the class and for
   // testing and debugging applications that use the class.
   // Assume each stored element already provides its own reasonable toString
   // method.

   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.
}

Related Solutions

Create a file and change the file permissions to (a) make it executable, (b) enable the...
Create a file and change the file permissions to (a) make it executable, (b) enable the SUID, and (c) use the ACL to add a permission for the root user. Do a long listing of this file. LINUX command line / virtual box.
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch multiple methods testing In this document program logic & program structure (class, methods, variables) input / output assumptions and limitations documentation & style suggested schedule cover letter discussion questions grading, submission, and due date additional notes challenges sample output test plan Program Logic The program prompts the user to select a type of math problem (addition,...
it should be done in libre office Create a new presentation – Click File -> New-...
it should be done in libre office Create a new presentation – Click File -> New- > Presentation then select ForestBird template. Activate slide pane by clicking on View tab then Slide Pane. Right click on the first slide on the slide pane and click on Layout then select Title Only layout. In the title box, type – Getting started with Impress Right click on the title box and change the alignment to Centre Delete other slides from Slide Pane...
Once the form in this file is submitted, create a new paragraph that will stack on...
Once the form in this file is submitted, create a new paragraph that will stack on top of the existing paragraphs and will contain the data entered into the textarea. Each new paragraph should have the class 'tweet' applied to it. *Hint: Remember to use return false; to stop the form submission from reloading the page. <!DOCTYPE html> <html> <head>    <title>Simple Twitter</title>    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">    <style>    .tweet{border-top:1px solid #dedede; padding:20px;}    </style>    <script...
Once the form in this file is submitted, create a new paragraph that will stack on...
Once the form in this file is submitted, create a new paragraph that will stack on top of the existing paragraphs and will contain the data entered into the textarea. Each new paragraph should have the class 'tweet' applied to it. *Hint: Remember to use return false; to stop the form submission from reloading the page. <!DOCTYPE html> <html> <head> <title>Simple Twitter</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <style> .tweet{border-top:1px solid #dedede; padding:20px;} </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body class="container" style="width:50%"> <h1>Twitter</h1>...
Create a geoprocessing tool from the .py file included (script from this file included below). Use...
Create a geoprocessing tool from the .py file included (script from this file included below). Use any csv file you would like. I will work around that. This is for a programming for GIS class. We work with jupyter, idle, arcgis pro import os import arcpy from arcpy import env input_table = r"C:\Answers\January2018.CSV" output_fc = r"C:\Answers\crime.gdb\January2018_Crime" spRef = arcpy.SpatialReference("NAD 1983 StatePlane Missouri East FIPS 2401 (US Feet)") gdb_name = os.path.dirname(output_fc) fc_name = os.path.basename(output_fc) env.overwriteOutput = True print("Creating File GDB") arcpy.CreateFileGDB_management(os.path.dirname(gdb_name),...
You inherited the following SAS program. Add all the necessary statements to: a) Create two new...
You inherited the following SAS program. Add all the necessary statements to: a) Create two new variables. Call one OVERALL, computed as the average of the IQ score, the MATH score, and the SCIENCE score divided by 500. Call the other GROUP, defined as 1 for IQ scores between 0 and 100 (inclusive), 2 for IQ cores between 101 and 140 (inclusive), and 3 for IQ greater than 140. b) Provide a listing of this data set in IQ order....
A) Modify the ArrayOfCircles.java file such that it implements the missing methods in the given SimpleListInterface....
A) Modify the ArrayOfCircles.java file such that it implements the missing methods in the given SimpleListInterface. circArray and size are member fields which can be accessed by all method. Make sure both circArray and size are updated as needed in the method you write. 1. The methods which are already implemented are:  constructor (not specified in interface)  size, isFull, and the two get methods 2. The methods you need to implement (given and explained in the SimpleListInterface) are:...
In MySQL, create a new schema titled <yourlastname>module3. Using the below file, create the tables in...
In MySQL, create a new schema titled <yourlastname>module3. Using the below file, create the tables in your new schema and populate with the supplied data. Tables do not have keys. Do not define them at this time. There are errors in the data population (INSERT) statements. It is your job to find the errors and correct them. This is important. You will need correct data for future exercises in this module. In the submission area, choose Write Submission and identify...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT