Question

In: Computer Science

Show the values contained in the instance variables elements and numElements of the sample collection after...

Show the values contained in the instance variables elements and numElements of the sample collection after the following sequence of operations:

ArrayCollection<String> sample = new ArrayCollection<String>; sample.add("A"); sample.add("B"); sample.add("C"); sample.add("D"); sample.remove("B");

//---------------------------------------------------------------------------
// ArrayCollection.java by Dale/Joyce/Weems Chapter 5
//
// Implements the CollectionInterface using an array.
//
// Null elements are not allowed. Duplicate elements are allowed.
//
// Two constructors are provided: one that creates a collection of a default
// capacity, and one that allows the calling program to specify the capacity.
//---------------------------------------------------------------------------
package ch05.collections;

public class ArrayCollection<T> implements CollectionInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected T[] elements; // array to hold collection’s 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

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

public ArrayCollection(int capacity)
{
elements = (T[]) new Object[capacity];
}

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.
{
location = 0;
found = false;

while (location < numElements)
{
if (elements[location].equals(target))
{
found = true;
return;
}
else
location++;
}
}

public boolean add(T element)
// Attempts to add element to this collection.
// Returns true if successful, false otherwise.
{
if (isFull())
return false;
else
{
elements[numElements] = 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)
{
elements[location] = elements[numElements - 1];
elements[numElements - 1] = null;
numElements--;
}
return found;
}
  
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 isFull()
// Returns true if this collection is full; otherwise, returns false.
{
return (numElements == elements.length);
}

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

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

Solutions

Expert Solution

ArrayCollection.java

public class ArrayCollection<T> implements CollectionInterface<T> {
   protected final int DEFCAP = 100; // default capacity
   protected T[] elements; // array to hold collection’s 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

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

   public ArrayCollection(int capacity) {
      
       elements = (T[]) new Object[capacity];
   }

   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.
   {
       location = 0;
       found = false;

       while (location < numElements) {
           if (elements[location].equals(target)) {
               found = true;
               return;
           } else
               location++;
       }
   }

   public boolean add(T element)
// Attempts to add element to this collection.
// Returns true if successful, false otherwise.
  
   {
       if(element==null) {
           return false;
       }
       if (isFull())
           return false;
       else {
           elements[numElements] = 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) {
           elements[location] = elements[numElements - 1];
           elements[numElements - 1] = null;
           numElements--;
       }
       return found;
   }

   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 isFull()
// Returns true if this collection is full; otherwise, returns false.
   {
       return (numElements == elements.length);
   }

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

   public int size()
// Returns the number of elements in this collection.
   {
       return numElements;
   }
  
   public void display() {
       System.out.print("[");
       for(int i=0;i<numElements;i++) {
           System.out.print(elements[i]+" ");
       }
      
       System.out.print("]");
       System.out.println();
   }
}

Main.java

public class Main {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       System.out.println("Constructing Array with Default Constructer");
       ArrayCollection<String> sample = new ArrayCollection<String>();
       System.out.println("Displaying Collection");
       sample.display();
       System.out.println("Adding A,B,C,D to Collection");
       sample.add("A");
       sample.add("B");
       sample.add("C");
       sample.add("D");
       sample.display();
       System.out.println("Removing B from Collection");
       sample.remove("B");
       sample.display();
      
       System.out.println("Constructing Array with Parameterised Constructer");
       ArrayCollection<String> capacitysample = new ArrayCollection<String>(5);
       System.out.println("Displaying Collection");
       capacitysample.display();
       System.out.println("Adding A,B to Collection");
       capacitysample.add("A");
       capacitysample.add("B");
       capacitysample.display();
       System.out.println("Removing A from Collection");
       capacitysample.remove("A");
       capacitysample.display();
   }

}

Output

Constructing Array with Default Constructer
Displaying Collection
[]
Adding A,B,C,D to Collection
[A B C D ]
Removing B from Collection
[A D C ]
Constructing Array with Parameterised Constructer
Displaying Collection
[]
Adding A,B to Collection
[A B ]
Removing A from Collection
[B ]

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Related Solutions

PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is...
PYTHON ASSIGNMENT Problem: (1) The  __init__ method should initialize the values of the instance variables. Here is the beginning of __init__: def __init__(self, the_hr, the_min, the_sec): self.hr = the_hr # Also initialize min and sec. (2) Include a __str__ method that returns the current state of the clock object as a string. You can use the string format method format like this: return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min, self.sec) (3) When the tick method is executed, add one to sec. If the resulting value...
hat are the values of the elements in the vector named names_1 after the following code...
hat are the values of the elements in the vector named names_1 after the following code is executed? vector<string> names_1 { "Mike", "Ben", "Joel", "Anne" }; vector<string> names_2 { "Judy", "Samantha", "Kelly" }; names_1.insert(names_1.end(), "Mary"); names_1.erase(names_1.begin());         names_1.insert(names_1.begin() + 2, ++names_2.begin(), names_2.end()); names_1.swap(names_2); names_1.erase(++names_1.begin()); names_1.insert(names_1.begin(), ++names_2.begin(), names_2.begin() + 2); a. Joel, Judy, Samantha b. Judy, Mary, Joel, Mary c. Joel, Anne, Judy, Samantha d. Joel, Judy, Kelly 1.5 points    QUESTION 12 What are the values of the key/value pairs in...
The class Polynomials is a collection of polynomials of either implementation stored in an instance of...
The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List L; // Creates an empty list L of polynomials public Polynomials ( ) { … } (1 mark) // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) (1 mark) { … } // Inserts polynomial p into L public void Insert (Polynomial p) (1 mark) { … }...
Below are the values for two variables x and y obtained from a sample of size...
Below are the values for two variables x and y obtained from a sample of size 5. We want to build a regression equation based the sample data. ŷ = b₀ + b₁x y x 16 5 21 10 8 6 28 12 53 14 11 On average the observed y deviate from the predicted y by, a 10.73 b 10.04 c 9.53 d 8.76 12 Sum of squares total (SST) is, a 1096.3 b 1178.8 c 1296.7 d 1361.5...
Below are the values for two variables x and y obtained from a sample of size...
Below are the values for two variables x and y obtained from a sample of size 5. We want to build a regression equation based the sample data. ŷ = b₀ + b₁x y x 16 5 21 10 8 6 28 12 53 14 1 The sum product of x and y is, a 1416 b 1451 c 1466 d 1481 2 The value in the numerator of the formula to compute the slope of the regression equation is,...
Complete the following problems in a word document. Please show values for all variables and circle...
Complete the following problems in a word document. Please show values for all variables and circle the correct answer. Failure to do so will result in lost points. You borrow $15,000 for your tuition costs. You agree to make payments at the end of each month for the next 10 years. If the interest rate on this loan is 6%, how much is your monthly payment? How much do you still owe after 20 payments? Jill would like to plan...
1- The average is a way to characterize a collection of values. When the values refer...
1- The average is a way to characterize a collection of values. When the values refer to a fixed item (for example the number of oranges per tree) and we want to find the average number of oranges, the _______________ mean is appropriate. 2- The ___________________________ mean is used when we want to preserve the effect on the mean of a percentage change in a value independent of the values magnitude. 3- The word size of the IAS  computer (remember one...
After collecting a sample of 250 elements from a population with a known standard deviation of...
After collecting a sample of 250 elements from a population with a known standard deviation of 13.7, the mean is found to be 112.4. a) Find a 95% confidence interval for the mean. b) Find a 99% confidence interval for the mean. 2. Jon Jackobson, a very dedicated graduate graduate, has just completed a first 700 page version of his thesis. Jon typed the job himself and he's interested in knowing the average number of typographical errors per page, but...
Required: Ensure you show all formulas and state values of variables, substitute and solve. Provide a...
Required: Ensure you show all formulas and state values of variables, substitute and solve. Provide a concluding statement for all parts. Suppose that 31% of families in town own iPads. If sixteen families are surveyed at random, Determine the probability that exactly 4 families own an IPAD. Determine the probability that less than 3 families own iPads. Determine the probability that least 3 families own iPads. Determine the expected number of families with an iPad. There are ten cats and...
Certain variables contained in a company's annual report are continuous random variables. What is the best...
Certain variables contained in a company's annual report are continuous random variables. What is the best example of a continuous random variable from a Fortune 500 company annual report? number of company facilities in Alaska number of top executives energy usage at all facilities number of corporate jets owned
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT