Question

In: Computer Science

Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size);...

Complete ArrayCollection.java with following methods (Please complete code in java language):

public ArrayCollection();

public ArrayCollection(int size);

public boolean isEmpty();

public boolean isFull();

public int size(); // Return number of elements in the Collection.

public String toString();

public boolean add(T element); // Add an element into the Collection, return true if successful

public boolean remove(T target); // Remove the target from Collection, return true if successful

public boolean removeAll(T target); // Remove all occurrences of Target, return if successful

public void removeDuplicate(); // Remove duplicated element(s)

public boolean equals(ArrayCollection that); // Return true if this Collection is same as that Collection

public int count(T target); // Return the number of a given target in the Collection

public void merge(ArrayCollection that); // Merge this Collection with that Collection

public void enlarge(int size); //Increase this Collection with additional size elements

public void clear(); // Clear entire Collection

public boolean contains(T target); // Return true if the target is in the Collection

public int findIndex(T target); // Return index of the target

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) {

// 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;

}

}

Solutions

Expert Solution

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(isFull())
           enlarge(size());
       elements[numberOfElements] = element;
       numberOfElements++;
       return true;

   }

   public boolean remove(T target) {
       int index = findIndex(target);
       if(index == -1)
           return false; //not found

       //shift other elements
       for(int i = index + 1; i < numberOfElements; i++)
           elements[i-1] = elements[i];
       return true;

   }

   public boolean removeAll(T target) {
       if(contains(target)) {
           while(contains(target))
               remove(target);

           return true;
       }
       else
           return false;

   }

   public void removeDuplicate() {
       T[] temp = (T[]) (new Object[numberOfElements]);
       int n = 0;
       int i = 0, j = 0;

       while(i < numberOfElements) {
           boolean dup = false;
           for(int k = 0; k < i; k++) {
               if(elements[k].equals(elements[i])) {
                   dup = true;
                   break;
               }
           }
           if(!dup) {
               temp[j++] = elements[i];
               n++;
           }
           i++;
       }
       elements = temp;
       numberOfElements = n;
   }


   public boolean equals(ArrayCollection that) {

       // Return true if ArrayCollection are identical.

       boolean result = true;

       if(size() != that.size())
           result = false;
       else {
           for(int i = 0; i < size(); i++) {
               if(!elements[i].equals(that.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 < size(); 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.size(); i++) {
           add(that.elements[i]);
       }
   }

   public void enlarge(int size) {

       // Enlarge elements[] with additional size
       T[] temp = (T[]) new Object[elements.length + size];
       for(int i = 0; i < numberOfElements; i++)
           temp[i] = elements[i];
       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 < size(); i++) {
           if(elements[i].equals(target)) {
               found = true;
               break;
           }
       }
       return found;

   }

   public int findIndex(T target) {

       // Return index of target

       int index = -1;

       for(int i = 0; i < size(); i++) {
           if(elements[i].equals(target)) {
               index = i;
               break;
           }
       }
       return index;

   }

}


Related Solutions

[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
consider the code; typedef struct { int size; int *nums; }Data; Complete the function below that...
consider the code; typedef struct { int size; int *nums; }Data; Complete the function below that creates and returns a new data type that contains a nums array with size randomly chosen integers. Ensure proper error checking. Data *getRandomData(int size) { //PUT CODE HERE //Create random ints for(int i=0; i<size; i++) d->nums[1] = (int)(rand()/(float)RAND_MAX*100); return d;
Please don't just copy another solution. Write the following Java program: public static String getFlag(int size,...
Please don't just copy another solution. Write the following Java program: public static String getFlag(int size, char color1, char color2, char color3) - This method returns a string where a triangle appears on the left size of the diagram, followed by horizontal lines. For example, calling DrawingApp.getFlag(9, 'R', '.', 'Y'); will generate the string: R............................................ RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRRR.................................... RRRRRRRRR.................................... RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY R............................................ The diagram has a number of rows that...
Please follow the instructions carefully and complete the code given below. Language: Java Instructions from your...
Please follow the instructions carefully and complete the code given below. Language: Java Instructions from your teacher: (This is a version of an interview question I once saw.) In this problem, we will write a program that, given an integer k, an integer n, and a list of integers, outputs the number of pairs in in the list that add to k. To receive full credit for design, your algorithm must have a runtime in O(n) , where n is...
Language: Java To be able to code a class structure with appropriate attributes and methods. To...
Language: Java To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure to...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
please recode this in c++ language public abstract class BankAccount { double balance; int numOfDeposits; int...
please recode this in c++ language public abstract class BankAccount { double balance; int numOfDeposits; int numOfWithdraws; double interestRate; double annualInterest; double monSCharges; double amount; double monInterest; //constructor accepts arguments for balance and annual interest rate public BankAccount(double bal, double intrRate) { balance = bal; annualInterest = intrRate; } //sets amount public void setAmount(double myAmount) { amount = myAmount; } //method to add to balance and increment number of deposits public void deposit(double amountIn) { balance = balance + amountIn;...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five". Test Cases Test case #1 Expected result: When t is 2, your code sets inwords to "two" Test case #2 Expected result: When t is 50, your code sets inwords to "fifty" Test case #3 Expected result: When t...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT