Question

In: Computer Science

Using Java Languse, Complete ArraySet.java down below by using (Array collection) : package Homework3; public class...

Using Java Languse, Complete ArraySet.java down below by using (Array collection) :

package Homework3;
public class ArraySet extends ArrayCollection {
public ArraySet() {
}
public ArraySet(int size) {
super(size);
}
public boolean add(T element) {
// Complete your code here
return true;
}
}

ArrayCollection.java:

package Homework3;
public class ArrayCollection {
protected static final int DEFAULT_CAPACITY = 100;
protected T[] elements;
protected int numberOfElements;
public ArrayCollection() {
this(DEFAULT_CAPACITY);
}
public ArrayCollection(int size) {
elements = (T[]) new Object[size];
numberOfElements = 0;
}
public boolean isEmpty() {
return numberOfElements == 0;
}
public boolean isFull() {
return numberOfElements == elements.length;
}
public int size() {
return numberOfElements;
}
public String toString() {
String collection = "";
for (int i = 0; i < numberOfElements; i++)
collection += elements[i] + "\n";
return collection;
}
public boolean add(T element) {
// Complete your code here
return true;
}
public boolean remove(T target) {
// Complete your code here
return true;
}
public boolean removeAll(T target) {
// Complete your code here
return true;
}
public void removeDuplicate() {
// Remove any duplicated elements
}
public boolean equals(ArrayCollection that) {
// Return true if ArrayCollection are identical.
boolean result = true;
// Complete your code here.
return result && this.size() == that.size();
}
public int count(T target) {
// Return count of target occurrences
int c = 0;
// Complete your code here
return c;
}
public void merge(ArrayCollection that) {
// Merge that ArrayCollection into this ArrayCollection
// Complete your code here
}
public void enlarge(int size) {
// Enlarge elements[] with additional size
// Complete your code here
}
public void clear() {
// Remove all elements in the collection
}
//Note: Different from textbook, this implementation has no 'found' and
'location' attributes.
// There is no find() method.
// There is a new methods findIndex().
public boolean contains(T target) {
// Return true if target is found
boolean found = false;
// Complete your code here
return found;
}
public int findIndex(T target) {
// Return index of target
int index = 0;
// Complete your code here
return index;
}
}

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")); // This should return true
System.out.println(ac2);
ac2.enlarge(10);
ac2.add("Watermelon");
System.out.println("Equals: " + ac1.equals(ac2));
as1.add("Avocado");
as1.add("Avocado"); // This will not be added, since the
collection is "set"
}
}

Solutions

Expert Solution

// ArrayCollection.java

package Homework3;

public class ArrayCollection<T> {
   protected static final int DEFAULT_CAPACITY = 100;
   protected T[] elements;
   protected int numberOfElements;
  
   public ArrayCollection() {
       this(DEFAULT_CAPACITY);
   }
   public ArrayCollection(int size) {
       elements = (T[]) new Object[size];
       numberOfElements = 0;
   }
   public boolean isEmpty() {
       return numberOfElements == 0;
   }
   public boolean isFull() {
       return numberOfElements == elements.length;
   }
   public int size() {
       return numberOfElements;
   }
   public String toString() {
       String collection = "";
       for (int i = 0; i < numberOfElements; i++)
           collection += elements[i] + "\n";
       return collection;
   }
   public boolean add(T element) {
       // if array is full , enlarge the array by 1 element
       if(numberOfElements == elements.length)
       {
           enlarge(1);
       }
       // add the element at the end
       elements[numberOfElements] = element;
       numberOfElements++;
       return true;
   }
  
   public boolean remove(T target) {
       // loop over the array
       for(int i=0;i<numberOfElements;i++)
       {
           // if element is found
           if(elements[i].equals(target))
           {
               // shift the elements left
               for(int j=i;j<numberOfElements-1;j++)
               {
                   elements[j] = elements[j+1];
               }
               numberOfElements--; // decrement the size
               return true;
           }
       }
       return false;
   }
   public boolean removeAll(T target) {
       boolean found = false;
       found = contains(target);
       // loop that continues till the collection contains the target
       while(contains(target))
       {
           remove(target);
       }
      
       return found;
   }
  
   public void removeDuplicate() {
       // loop over the elements
       for(int i=0;i<numberOfElements;i++)
       {
           // loop over the elements starting next to i
           for(int j=i+1;j<numberOfElements;)
           {
               // if jth element = ith element, remove jth element by shifting the elements left
               if(elements[i].equals(elements[j]))
               {
                   for(int k=j;k<numberOfElements-1;k++)
                   {
                       elements[k] = elements[k+1];
                   }
                   numberOfElements--; // decrement the size
               }else // if elements are not equal increment j to go to the next element
                   j++;
           }
       }
   }
  
   public boolean equals(ArrayCollection<T> that) {
       // Return true if ArrayCollection are identical.
       boolean result = true;
       // Complete your code here.
       // loop to check if count of all elements in both collection is same
       for(int i=0;i<numberOfElements;i++)
       {
           // if not same they are not equal
           if(count(elements[i]) != that.count(elements[i]))
           {
               result = false;
               break;
           }
       }
      
       return result && this.size() == that.size();
   }
  
   public int count(T target) {
       // Return count of target occurrences
       int c = 0;
       for(int i=0;i<numberOfElements;i++)
           if(elements[i].equals(target))
               c++;
       return c;
   }
  
   public void merge(ArrayCollection<T> that) {
       // Merge that ArrayCollection into this ArrayCollection
       for(int i=0;i<that.numberOfElements;i++)
       {
           this.add(that.elements[i]);
       }
   }
  
   public void enlarge(int size) {
       // Enlarge elements[] with additional size
       T temp[] = (T[])new Object[numberOfElements + size];
      
       for(int i=0;i<numberOfElements;i++)
           temp[i] = elements[i];
       elements = (T[])new Object[temp.length];
       elements = temp;
   }
  
   public void clear() {
       // Remove all elements in the collection
       numberOfElements = 0;
   }
   //Note: Different from textbook, this implementation has no 'found' and 'location' attributes.
   // There is no find() method.
   // There is a new methods findIndex().
   public boolean contains(T target) {
       // Return true if target is found
       boolean found = false;
       for(int i=0;i<numberOfElements;i++)
       {  
           if(elements[i].equals(target))
           {
               found = true;
               break;
           }
       }
       return found;
   }
  
   public int findIndex(T target) {
       // Return index of target
       int index = 0;
       // Complete your code here
       for(index=0;index < numberOfElements;index++)
       {
           if(elements[index].equals(target))
               return index;
       }
       return -1; // element not found
   }

}
//end of ArrayCollection.java

//ArraySet.java

package Homework3;

public class ArraySet<T> extends ArrayCollection<T> {
  
   public ArraySet() {
   }
   public ArraySet(int size) {
   super(size);
   }
   public boolean add(T element) {
       // if element is not present in the set, add to the set
if(!contains(element))
       {
           super.add(element);
           return true;
       }
      
       return false;
   }

}
//end of ArraySet.java

//Homework3 .java

public class Homework3 {
  
  
   public static void main(String[] args) {
       ArrayCollection<String> ac1 = new ArrayCollection<String>(); // Calling Default Constructor
       ArrayCollection<String> ac2 = new ArrayCollection<String>(2); // Calling overloaded   constructor
       ArraySet<String> as1 = new ArraySet<String>();
       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")); // This should return true
       System.out.println(ac2);
       ac2.enlarge(10);
       ac2.add("Watermelon");
       System.out.println("Equals: " + ac1.equals(ac2));
       as1.add("Avocado");
       as1.add("Avocado"); // This will not be added, since the collection is "set"
       System.out.println(as1);
   }

}
//end of Homework3 .java

Output:


Related Solutions

Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:) package Homework3; public class LinkedListBag...
Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:) package Homework3; public class LinkedListBag extends LinkedListCollection { LinkedListBag() { } public T grab() { T result; Node cursor = head; int rand = (int) (Math.random() * size()); // Some code in here.. result = cursor.getInfo(); remove(result); return result; } } LinkedListCollection.java: package Homework3; public class LinkedListCollection <T> { protected Node<T> head = null; public LinkedListCollection() { } public boolean isEmpty() { return head == null; } public int...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that contains an array as a private instance variable. Construct methods with arrays as parameters and return values. Use partially filled arrays to implement a class where objects can be dynamically added. Implement searching and sorting algorithms. Instructions For this assignment you will be implementing an application that manages a music collection. The application will allow the user to add albums to the collection and...
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")); //...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
JAVA package stringPractice1068; public class SomePracticeStringMethods { /* returns true if c is a punctuation mark...
JAVA package stringPractice1068; public class SomePracticeStringMethods { /* returns true if c is a punctuation mark or false otherwise * * Punctuation mark is defined as: * apostrophe ' * comma , * period . * semicolon ; * colon : * exclamation point ! * question mark ? * * (You don't have to worry about any others) */ public static boolean isPunct(char c) {    /* placeholder just so that the function    * compiles. fill in your...
Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList...
Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList { public static void main(String[] args) { ArrayList list = new ArrayList(); } private String[]a = new String[1000]; private int end = -1; // Throw an IndexOutOfBoundsException if the index is invalid public String get(int index); // Return the first index in the list with the given value, or -1 if it's not found public int find(String val); // Return true if the element...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT