Question

In: Computer Science

complete the public T removeRandom(),    public SetADT<T> union(SetADT<T> set), and the incomplete part in the arraysettester...

complete the public T removeRandom(),    public SetADT<T> union(SetADT<T> set), and the incomplete part in the arraysettester

arrayset.java

package arraysetpackage;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;

public class ArraySet<T> implements SetADT<T> {
   private static final int DEFAULT_SIZE = 20;
   private int count;
   private T[] setValues;
   private Random rand;
  
   public ArraySet (){
       this(DEFAULT_SIZE);
   } // end of constructor
  
   public ArraySet (int size){
       count = 0;
       setValues = (T[]) new Object[size];
       rand = new Random();
   } // end of constructor  

  
   public void add(T element) {
       if (contains(element))
           return;
       if (count == setValues.length) {
           T[] temp = (T[]) new Object[setValues.length*2];
           for (int i = 0; i < setValues.length; i++) {
               temp[i] = setValues[i];
           }
           setValues = temp;
       }
       setValues[count] = element;
       count++;
   }

   public void addAll(SetADT<T> set) {
       Iterator<T> iter = set.iterator();
       while (iter.hasNext()){
           System.out.println(iter.next());
       }
       // finish: this method adds all of the input sets elements to this array
      
   }

   public boolean contains(T target) {
       for (int i = 0; i < count; i++ )
           if (setValues[i].equals(target))
               return true;
       return false;
   }
  
   public String toString () {
       String toReturn = "[";
       for (int i = 0; i < count; i++) {
           toReturn += setValues[i] + " ";
       }
       toReturn +="]";
       return toReturn;
   }
  


   public boolean equals(SetADT<T> set) {
       // finish: tests to see if this set and the input set have exactly the same
       // elements
      
      
       return false; // this is just generic, you need to change the return
   }
  
   public boolean isEmpty() {
       return count==0;
   }

   public Iterator<T> iterator() {
       return new ArraySetIterator<T>(setValues,count);
   }

   public T remove(T element) {
       for (int i = 0; i < count; i++ ) {
           if (setValues[i].equals(element)) {
               T toReturn = setValues[i];
               setValues[i] = setValues[count-1];
               count--;
               return toReturn;
           }
       }
       throw new NoSuchElementException("not present");
   }


   public T removeRandom() {
       // finish: remove and return a random element. you will use the
       // local rand object
      
      
       return null; // this is just generic, you need to change the return
   }
     
   public int size() {
       return count;
   }
     
   public SetADT<T> union(SetADT<T> set) {
       // finish: a new set is created and returned. This new set will
       // contain all of elements from this set and the input parameter set
      
      
       return null; // this is just generic, you need to change the return
   }

}
arraysetester.java:

package arraysetpackage;

import java.util.Iterator;

public class ArraySetTester {


   public static void main(String[] args) {
       SetADT <String> mySet = new ArraySet<String>();

       for (int i = 0; i < 12; i++)
           mySet.add(new String("apple"+i));

       System.out.println(mySet);
      
       System.out.println("mysize = "+mySet.size()+ " [expect 12]");
       mySet.add(new String ("apple0"));
       System.out.println("mysize = "+mySet.size()+ " [expect 12]");
       System.out.println("contains 11? = "+mySet.contains(new String("11")));
       System.out.println("contains apple11? = "+mySet.contains(new String("apple11")));      
      
       try {
           String removedItem = mySet.remove("apple7");
           System.out.println(mySet);
           System.out.println(removedItem+ " was removed");
       } catch (Exception e) {
           System.out.println("item not found, can't remove");
       }
      
       try {
           String removedItem = mySet.remove("apple17");
           System.out.println(mySet);
           System.out.println(removedItem+ " was removed");
       } catch (Exception e) {
           System.out.println("item not found, can't remove");
       }
      
       Iterator<String> iter = mySet.iterator();
       while (iter.hasNext()){
           System.out.println(iter.next());
       }

       SetADT <String> mySet2 = new ArraySet<String>();

       for (int i = 0; i < 12; i++)
           mySet2.add(new String("orange"+i));  
       System.out.println(mySet2);
      
       // add code here to test methods you finish in ArraySet
      
      
      
       // after you complete the existing methods, do the Case Study
       // Approach 1 will be here in the main
      
       // Approach 2 will be here in ArraySetTester, but you will
       // create a local static method that you will call from the main
      
       // Approach 3 will start with uncommenting the prototype in SetADT
       // and then creating the method in ArraySet. Finally you will write
       // code here to test the new method
   }


}

arraysetiterator.java

package arraysetpackage;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArraySetIterator <T> implements Iterator <T> {
   private int position; //Always points to the next value
   private T [] values;
   private int count;
  
  
   public ArraySetIterator (T [] theValues, int aCount) {
       position = 0;
       values = theValues;
       count = aCount;
   }
  
   public boolean hasNext() {
       return position < count;
   }
  
   public T next() {
       if (position >= count)
           throw new NoSuchElementException("Past " + count + " elements");
       position++;
       return values[position - 1];
}
  
   public void remove() {
       throw new
       UnsupportedOperationException("No remove for ArraySet");
   }

}

setadt.java

package arraysetpackage;

import java.util.Iterator;

public interface SetADT<T> {
   public void add (T element); //Adds one element to this set, ignoring duplicates
   public void addAll (SetADT<T> set); //Adds all elements in the parameter to this set,
   // ignoring duplicates
   public T removeRandom (); //Removes and returns a random element from this set
   public T remove (T element); //Removes and returns the specified element from this set
   public SetADT<T> union (SetADT<T> set); //Returns the union of this set and the
   // parameter
   public boolean contains (T target); //Returns true if this set contains the parameter
   //public boolean contains(SetADT<T> Set); // Returns true if this set contains the parameter
   public boolean equals (SetADT<T> set); //Returns true if this set and the parameter
   //contain exactly same elements
   public boolean isEmpty(); //Returns true if this set contains no elements
   public int size(); //Returns the number of elements in this set   
   public Iterator<T> iterator(); //Returns an iterator for the elements in this set
   public String toString(); //Returns a string representation of this set
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// ArraySet.java

import java.util.Iterator;

import java.util.NoSuchElementException;

import java.util.Random;

public class ArraySet<T> implements SetADT<T> {

              private static final int DEFAULT_SIZE = 20;

              private int count;

              private T[] setValues;

              private Random rand;

              public ArraySet() {

                           this(DEFAULT_SIZE);

              } // end of constructor

              public ArraySet(int size) {

                           count = 0;

                           setValues = (T[]) new Object[size];

                           rand = new Random();

              } // end of constructor

              public void add(T element) {

                           if (contains(element))

                                         return;

                           if (count == setValues.length) {

                                         T[] temp = (T[]) new Object[setValues.length * 2];

                                         for (int i = 0; i < setValues.length; i++) {

                                                       temp[i] = setValues[i];

                                         }

                                         setValues = temp;

                           }

                           setValues[count] = element;

                           count++;

              }

              public void addAll(SetADT<T> set) {

                           Iterator<T> iter = set.iterator();

                           while (iter.hasNext()) {

                                         T item = iter.next();

                                         // adding item to set, add method will check for duplicates before

                                         // adding, so we dont need to worry about that

                                         add(item);

                           }

                           // finished

              }

              public boolean contains(T target) {

                           for (int i = 0; i < count; i++)

                                         if (setValues[i].equals(target))

                                                       return true;

                           return false;

              }

              public String toString() {

                           String toReturn = "[";

                           for (int i = 0; i < count; i++) {

                                         toReturn += setValues[i] + " ";

                           }

                           toReturn += "]";

                           return toReturn;

              }

              public boolean equals(SetADT<T> set) {// finished

                           if (this.size() == set.size()) {

                                         // same size

                                         Iterator<T> iter = set.iterator();

                                         // checking if all elements of set present on this set

                                         while (iter.hasNext()) {

                                                       T item = iter.next();

                                                       if (!contains(item)) {

                                                                    // item is not present on this set, so not equal

                                                                    return false;

                                                       }

                                         }

                                         // all elements are same (can be in different order)

                                         return true;

                           }

                           // not equal

                           return false;

              }

              public boolean isEmpty() {

                           return count == 0;

              }

              public Iterator<T> iterator() {

                           return new ArraySetIterator<T>(setValues, count);

              }

              public T remove(T element) {

                           for (int i = 0; i < count; i++) {

                                         if (setValues[i].equals(element)) {

                                                       T toReturn = setValues[i];

                                                       setValues[i] = setValues[count - 1];

                                                       count--;

                                                       return toReturn;

                                         }

                           }

                           throw new NoSuchElementException("not present");

              }

              public T removeRandom() {// finished

                           // making sure that the set is not empty

                           if (count > 0) {

                                         // generating a random index

                                         int randIndex = rand.nextInt(count);

                                         // getting element at selected index

                                         T itemToRemove = setValues[randIndex];

                                         // removing the random item using remove() method

                                         remove(itemToRemove);

                                         // returning removed item

                                         return itemToRemove;

                           }

                           return null; //empty set

              }

              public int size() {

                           return count;

              }

              public SetADT<T> union(SetADT<T> set) {// finished

                           // creating a new SetADT object

                           SetADT<T> newSet = new ArraySet<T>(count + set.size());

                           // adding all elements of this set

                           newSet.addAll(this);

                           // adding all elements of other set (not allowing duplicates)

                           newSet.addAll(set);

                           return newSet;

              }

}

// ArraySetTester.java

import java.util.Iterator;

public class ArraySetTester {

              public static void main(String[] args) {

                           SetADT<String> mySet = new ArraySet<String>();

                           for (int i = 0; i < 12; i++)

                                         mySet.add(new String("apple" + i));

                           System.out.println(mySet);

                           System.out.println("mysize = " + mySet.size() + " [expect 12]");

                           mySet.add(new String("apple0"));

                           System.out.println("mysize = " + mySet.size() + " [expect 12]");

                           System.out

                                                       .println("contains 11? = " + mySet.contains(new String("11")));

                           System.out.println("contains apple11? = "

                                                       + mySet.contains(new String("apple11")));

                           try {

                                         String removedItem = mySet.remove("apple7");

                                         System.out.println(mySet);

                                         System.out.println(removedItem + " was removed");

                           } catch (Exception e) {

                                         System.out.println("item not found, can't remove");

                           }

                           try {

                                         String removedItem = mySet.remove("apple17");

                                         System.out.println(mySet);

                                         System.out.println(removedItem + " was removed");

                           } catch (Exception e) {

                                         System.out.println("item not found, can't remove");

                           }

                           Iterator<String> iter = mySet.iterator();

                           while (iter.hasNext()) {

                                         System.out.println(iter.next());

                           }

                           SetADT<String> mySet2 = new ArraySet<String>();

                           for (int i = 0; i < 12; i++)

                                         mySet2.add(new String("orange" + i));

                           System.out.println(mySet2);

                           // code to test new methods finished in ArraySet

                           //finding and displaying union

                           SetADT<String> union = mySet.union(mySet2);

                           System.out.println("Union of mySet and mySet2:");

                           iter = union.iterator();

                           while (iter.hasNext()) {

                                         System.out.println(iter.next());

                           }

                           //storing size

                           int size = mySet2.size();

                           //removing a random element, displaying removed value

                           String str = mySet2.removeRandom();

                           System.out.println("Random element " + str + " is removed from mySet2");

                           //displaying the size and expected size of set (should be size-1)

                           System.out.println("Current size of mySet2: " + mySet2.size()

                                                       + ", [expect: " + (size - 1)+"]");

                           //ensuring that deleted element is no longer present

                           System.out.println("MySet2 contains " + str + "? "

                                                       + mySet2.contains(str));

                           //testing equals method

                           System.out.println("mySet equals mySet2: " + mySet.equals(mySet2));

                           System.out.println("mySet equals mySet: " + mySet.equals(mySet));

              }

}

/*OUTPUT*/

[apple0 apple1 apple2 apple3 apple4 apple5 apple6 apple7 apple8 apple9 apple10 apple11 ]

mysize = 12 [expect 12]

mysize = 12 [expect 12]

contains 11? = false

contains apple11? = true

[apple0 apple1 apple2 apple3 apple4 apple5 apple6 apple11 apple8 apple9 apple10 ]

apple7 was removed

item not found, can't remove

apple0

apple1

apple2

apple3

apple4

apple5

apple6

apple11

apple8

apple9

apple10

[orange0 orange1 orange2 orange3 orange4 orange5 orange6 orange7 orange8 orange9 orange10 orange11 ]

Union of mySet and mySet2:

apple0

apple1

apple2

apple3

apple4

apple5

apple6

apple11

apple8

apple9

apple10

orange0

orange1

orange2

orange3

orange4

orange5

orange6

orange7

orange8

orange9

orange10

orange11

Random element orange6 is removed from mySet2

Current size of mySet2: 11, [expect: 11]

MySet2 contains orange6? false

mySet equals mySet2: false

mySet equals mySet: true


Related Solutions

public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
Set out two circumstances in which it may be necessary to complete financial statements from incomplete...
Set out two circumstances in which it may be necessary to complete financial statements from incomplete records and discuss the risks involved in preparing these financial statements.
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() {...
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() { } public boolean add(T element) { // Code here return true; } } Homework3 class: public class Homework3 { public static void main(String[] args) { ArrayCollection ac1 = new ArrayCollection(); // Calling Default Constructor ArrayCollection ac2 = new ArrayCollection(2); // Calling overloaded constructor ArraySet as1 = new ArraySet(); ac2.add("Apple"); ac2.add("Orange"); ac2.add("Lemon"); // This can't be added into ac2 as collection is full System.out.println(ac2.remove("Apple")); //...
class LLNode<T> { public T info; public LLNode<T> link; public LLNode(T i, LLNode<T> l) { //...
class LLNode<T> { public T info; public LLNode<T> link; public LLNode(T i, LLNode<T> l) { // constructor info=i; link=l; } } class CircularLinkedQueue<T> { private LLNode<T> rear = null; // rear pointer public boolean isEmpty() { /*checks if the queue is empty */ } public int size() { /* returns the number of elements in the queue */ } public void enQueue(T element) { /* enqueue a new element */ } public T deQueue() { /* dequeue the front element...
Below is the complete set of Financial Statements of Take-Home University (THU), a Public University of...
Below is the complete set of Financial Statements of Take-Home University (THU), a Public University of Ghana, issued on 1st May 2020. Statement of Financial Performance for the Year Ended 31 December, 2018 Revenue GOG grant Internally Generated Funds Donations and other income Expenditure Compensation for employees Goods and services Social benefit Interest Capital expenditure (CAPEX) Other expenses Net Operating Result – Deficit Notes 2 3 4 5 6 1,540,000,000 14,427,492,000 9,278,258,000 25,245,750,000 8,385,270,000 2,238,083,000 1,720,000 1,720,000 25,542,515,000 79,100,000 36,248,408,000...
Snapdragon: Is it complete or incomplete? Perfect or imperfect? How are the floral parts of the...
Snapdragon: Is it complete or incomplete? Perfect or imperfect? How are the floral parts of the snapdragon flower arranged? What type of symmetry does it exhibit? Remove the corolla. What happens to the stamens when the corolla is removed? When you have finished examining the flower, list any other types of union of floral parts that you may have found? Is the ovary superior or inferior? Is the flower hypogynous, perigynous, or epigynous? How many locules (cavities) are in the...
explain the difference between zero, incomplete, and complete crowding out. if crowding out is complete, does...
explain the difference between zero, incomplete, and complete crowding out. if crowding out is complete, does it call into question the effectiveness of a rise in government purchases in order to remove an economy from a recessionary gap? explain and diagrammatically represent your answer.
QUESTION TWO Below is the complete set of Financial Statements of Take-Home University (THU), a Public...
QUESTION TWO Below is the complete set of Financial Statements of Take-Home University (THU), a Public University of Ghana, issued on 1st May 2020. Statement of Financial Performance for the Year Ended 31 December, 2018 Revenue Notes GOG grant 1,540,000,000 Internally Generated Funds 2 14,427,492,000 Donations and other income 9,278,258,000 25,245,750,000 Expenditure Compensation for employees 3 8,385,270,000 Goods and services 4 2,238,083,000 Social benefit 1,720,000 Interest 1,720,000 Capital expenditure (CAPEX) 5 25,542,515,000 Other expenses 6 79,100,000 36,248,408,000 Net Operating Result...
- hierarchical or partitional - overlapping or non-overlapping - fuzzy or crisp - complete or incomplete...
- hierarchical or partitional - overlapping or non-overlapping - fuzzy or crisp - complete or incomplete Note: Each part should be labeled with four characteristics, e.g., partitional, overlapping, crisp, and incomplete. Also, if you feel there may be some ambiguity about what characteristics a grouping has, provide a short justification of your answer. Case 1: The objects are the students in a class. There are groups for each official grade students received for the class. Case 2: The objects are...
An incomplete subsidiary ledger of materials inventory for May is as follows: a. Complete the materials...
An incomplete subsidiary ledger of materials inventory for May is as follows: a. Complete the materials issuances and balances for the materials subsidiary ledger under FIFO. Received Issued Balance Receiving Report Number Quantity Unit Price Materials Requisition Number Quantity Amount Date Quantity Unit price Amount May 1 370 $6 $2,220 27 260 $8 May 4 100 420 $ May 10 33 170 10 May 21 113 250 May 27 b. Determine the materials inventory balance at the end of May....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT