1. (4 marks) Consider the Polynomial and the generic ArayBag classes outlined to
ID: 3915560 • Letter: 1
Question
1. (4 marks) Consider the Polynomial and the generic ArayBag classes outlined to the right. Show the statements required to complete the following publie elass Aecaybag Objeot] data: int manytems pablie Areaybag(int initiaicapecity) (... public void add CE lemant) public roid addMany (E..2menta). a. Create a new ArrayBag of Polynomials called bawhabie tnt ountocourrns an initial capacity of 12 (E target) (... public int getcapacityD pablic boolean zenove (E target) publie int size publio void triatosineO public String toString) private void growkstieded.. b. Add 2 Polynomials to your ArrayBag (fewer statements better here). publie class Polysonialt int degree; double coefficients: publio Polynonial (int degree).. public int getbegree)... public double evaluate (double x).. public void setcoefficient(int index, double coefficient).. Count and store the number of Polynomials with a degree of 10 in your ArrayBag c. Determine the number of "free spots" resize itself. in your bag -ie, the number of elements can you add to the bag before it must d. s for the generic bag class described above, Use 19. (6 marky) Wite appropriate addl) and growäsleeded 0 methods comments to state any assumptions you are makingExplanation / Answer
here is your Answers: ----------->>>>>>>>
Question 18 Answers : --------------->>>>>>>>>>>>>>>>
a) Answer :
ArrayBag<Polynomials> bag1 = new ArrayBag<>();
b) Answer :
bag1.add(new Polynomials(10));
bag1.add(new Polynomials(14));
c) Answer :
int count = bag1.countOccurences(new Polynomials(10));
d) Answer :
int free_spot = bag1.getCapacity() - bag1.size();
public void add(E element){
if(size() >= capacity()){
growAsNeeded();
}
//i suppose that there is variable count which count the size of bag
data[count++] = element;
}
public void growAsNeeded(){
manyItems = manyItems * 2;
Object[] temp = new Object[manyItems];
for(int i = 0;i<count;i++){
temp[i] = data[i];
}
data = temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.