Question

In: Computer Science

/* *       Suppose we want to implement a class IntArraySet. The difference of this...

/*
*

  
  
Suppose we want to implement a class IntArraySet.
The difference of this class from IntArrayBag is that each item can only occur once in the set
We will use the same instance variables.


*/

public class IntArraySet
{
private int[ ] data;
private int manyItems;


public IntArraySet()
{
this(10);
}

public IntArraySet(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("The initialCapacity is negative: " + initialCapacity);
data = new int[initialCapacity];
manyItems = 0;
}

public int size() {return manyItems;}
  
public void ensureCapacity(int minimumCapacity)
{
int[ ] biggerArray;

if (data.length < minimumCapacity)
{
biggerArray = new int[minimumCapacity];
System.arraycopy(data, 0, biggerArray, 0, manyItems);
data = biggerArray;
}
}

//2. (7 points) complete add method: if it is not in the set yet, add it, and return true;
// otherwise, return false
// Don't use any method not defined in the class IntArraySet.   
public boolean add(int element)
{
  
  
  
}
// 3. (2 points) What is the complexity of the add method in the worst case?
//
  
}

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

add method

//copy the rest of the code here    
public boolean add(int element) {
                
                //checking if any duplicate element is present or not
                for(int i=0; i<manyItems; i++) {
                        if(data[i] == element)
                                return false;
                }
                
                //adding the element to array
                for(int i=0; i<manyItems; i++) {
                        if(data[i] == 0) {
                                data[i] = element;
                                return true;
                        }
                }
                
                return false;
        }

Complexity

The complexity of the add method is big O(n) as in the worst case the duplicate element may be present in the last index of the array and will result in a false output. Same goes for adding the element to the array.


Related Solutions

Suppose we want to assess the effect of a one-day SAT prep class at a 5%...
Suppose we want to assess the effect of a one-day SAT prep class at a 5% level of significance. Scores on the SAT writing exam can range from 200 to 800. A random sample of 50 students takes the SAT writing test before and after a prep class. We test the hypotheses: LaTeX: H_0 H 0 : LaTeX: \mu=0 μ = 0 LaTeX: H_a H a : LaTeX: \mu>0 μ > 0 where LaTeX: \mu μ is the mean of...
2. Suppose that we want to test for the difference between two population means. Explain under...
2. Suppose that we want to test for the difference between two population means. Explain under what conditions we would use the t-test and under what conditions we would use the z-test. Explain the differences between the two tests. Type all your answers. Try to write at least one page
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Suppose we want to test whether or not three means are equal. We want to perform...
Suppose we want to test whether or not three means are equal. We want to perform this test with a 2% significance level. If we perform an ANOVA test, what is the probability of the test producing accurate results (avoiding a Type I error)? Suppose we, instead, run three separate hypothesis tests (t-tests), each with 2% significance level. Mean 1 = Mean 2 Mean 1 = Mean 3 Mean 2 = Mean 3 What is the probability that all three...
Suppose we want to test whether or not three means are equal. We want to perform...
Suppose we want to test whether or not three means are equal. We want to perform this test with a 2% significance level. If we perform an ANOVA test, what is the probability of the test producing accurate results (avoiding a Type I error)? Suppose we, instead, run three separate hypothesis tests (t-tests), each with 2% significance level. Mean 1 = Mean 2 Mean 1 = Mean 3 Mean 2 = Mean 3 What is the probability that all three...
Suppose we want to test whether or not three means are equal. We want to perform...
Suppose we want to test whether or not three means are equal. We want to perform this test with a 10% significance level. If we perform an ANOVA test, what is the probability of the test producing accurate results (avoiding a Type I error)? Suppose we, instead, run three separate hypothesis tests (t-tests), each with 10% significance level. Mean 1 = Mean 2 Mean 1 = Mean 3 Mean 2 = Mean 3 What is the probability that all three...
Suppose we want to test whether or not three means are equal. We want to perform...
Suppose we want to test whether or not three means are equal. We want to perform this test with a 2% significance level. If we perform an ANOVA test, what is the probability of the test producing accurate results (avoiding a Type I error)? Suppose we, instead, run three separate hypothesis tests (t-tests), each with 2% significance level. Mean 1 = Mean 2 Mean 1 = Mean 3 Mean 2 = Mean 3 What is the probability that all three...
Suppose we want to test whether or not three means are equal. We want to perform...
Suppose we want to test whether or not three means are equal. We want to perform this test with a 7% significance level. If we perform an ANOVA test, what is the probability of the test producing accurate results (avoiding a Type I error)? Suppose we, instead, run three separate hypothesis tests (t-tests), each with 7% significance level. Mean 1 = Mean 2 Mean 1 = Mean 3 Mean 2 = Mean 3 What is the probability that all three...
We want to know whether or not there is a difference in the proportion of A’s...
We want to know whether or not there is a difference in the proportion of A’s in math class received by students who participated in a tutoring program and those who did not participate. There are 40 kids who did the tutoring program and 14 of them got A’s. There are 52 who did not do the tutoring program and 12 of them also got A’s. Apply 2-sided test at 4% SL. What more is needed? This is what we...
We want to assess whether there is a difference in the impact that the predatory larvae...
We want to assess whether there is a difference in the impact that the predatory larvae of three damselfly species (Enallagma, Lestes and Pyrrhosoma) have on the abundance of midge larvae in a pond. We plan to conduct an experiment in which small (1 m2m2) nylon mesh cages are set up in the pond. All damselfly larvae will be removed from the cages and each cage will then be stocked with 20 individuals of one of the species. After 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT