Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

stack- based JAVA - 1) Create an array-based bag that contains 100,000 random in

ID: 3595352 • Letter: S

Question

stack- based JAVA -

1) Create an array-based bag that contains 100,000 random integers between 0 and 100,000  

2) Get the bag size (it should match 100,000) (how long did this take in nanoseconds?)

3) Test whether the bag contains the perfect number 8128 (how long did this take in nanoseconds?)

4) Remove the last number from the bag, even if it is 8128 (how long did this take in nanoseconds?)

5) Add the number 8128 (the size of the bag should not have changed at this point. (how long did this take in nanoseconds?)

6) Determine the frequency with which 8128 occurs (this could be 1). (how long did this take in nanoseconds?)

7) Double the size of the bag to 200,000 and fill the the bag with 100.000 more random integers while maintaining the same 100,000 integers that are already there. Clearly a call to the add method will return false if a fixed size array is used to to implement the ADT bag. Therefore we need to re-size the array, but maintain the name of the old array.

8) Repeat steps b to f and determine how long getCurrentSize(); contains(T anEntry); add(T anEntry); and remove(T anEntry) take now?

9) Do your findings support the table above wherein the methods are compared? Use the times in nanoseconds you obtained for the execution of the methods to support your conclusion.

COMPARING IMPLEMENTATIONS LinkedBag Implementation ADT Bag method ArrayBag getCurrentSize () isFull () isEmpty() add (T anEntry) remove () remove (T anEntry) clear ( getFrequencyof (T anEntry) contains (T anEntry) toArray () (1) to Q(n) (0(n) 0(1) to (n) ((1) or C(n 0(1) to (n) O(n) 0(1) to (n) 0 (n)

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.*;
/**
*
* @author Prashant Tomer
*/
public class UseBag {
  
public static void main(String[] args)
{
ArrayList bag=new ArrayList(); ///
Random num=new Random();
//num.setSeed(System.currentTimeMillis());
//int a=num.nextInt(10000);
for(int i=0;i<100000;i++)
{

Integer r = num.nextInt(100000);
bag.add(r);
}
//System.out.println(a);
//bag.add("8128");
System.out.println(bag.size());
  
System.out.println(bag.contains(8128));
if(bag.contains(8128))  
{
bag.remove(8128);
}
else
{
System.out.println("not found 8128");
}

System.out.println(bag.size());
  
  
//first showing the last number and after that
//remove last number

System.err.println("bag retrieve last number in bag:"+bag.get(bag.size()-1));
bag.remove(bag.size()-1); //removing last number
  
//After Removing last number
System.out.println("bag after removing last number:"+bag.size());
  
  
  
  
}

}