Question

Collections (a) What are generic types and what is their importance in ADTs for collections? (b) How are the ADTS of stacks a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

a. A generic type is used to allow data type in the parameter declaration.

b. It allows a programmer to compile-time safety. Invalid type caught at compile time itself

ex ArrayList<String> list =new String<String>(); //this list can only be used to add string type in this list

//if any other you try to add it will throw compile time error.

Importance of Generic Type :

a. Type check happens at compile time. Avoid type mismatched exception at run time.

b. No need for typecasting.

c. Programmer can also implement a Generic type Algorithm

2. Stack and Queue similarity :

a. Both internally implement Array or LinkedList.

b. Both have the same super class.

Difference :

a. A stack is LIFO but Queue is FIFO.

b. Stack supports pop() to remove and push() to insert element. Queue supports add() to insert and remove() to remove element.

3. Static and dynamic data structure:

Static data structure:

a. Size is fixed.

b. Memory allocation happens at class loading time.

c. Access time is less

Dynamic data structure

a. Size is not fixed.

b. Size can be modified at run time

c. Difficult access to element with respect to the static data structure.

ArrayList used to make easier accessibility of element. update. add like operation happen in O(1) time in an ArrayList, which is possible because internally in implement array. Having known that array provides add like operation happen in O(1) time.

Element added is ArrayList, internally gets add in an array

But ArrayList is Dynamic data type and its size can be changed.

So when Array size gets full, another array of bigger size gets created and all previous element will get inserted in new array

4.

Stack<Integer> s=new Stack ();
   s.push(5); //add 5 to stack
   while(!s.isEmpty()){
       int n=s.pop(); //pop top element
       System.out.print(n+" _ "); //print n
       if(n>=1){ //
           s.push(n/3); //if n is greater than or equal to 1 divide it by 3 push it
           s.push(n/2); //divide it by 2 push it
       }
   }
   System.out.println();
   Queue<Integer> q =new queue();
   q.add(5);
   while(!q.isEmpty()){
       int n=q.remove();
       System.out.print(n+" _ ");
       if(n>=1){
           q.add(n/3);
           q.add(n/2);
       }
   }
      
      
   }

output

5 _ 2 _ 1 _ 0 _ 0 _ 0 _ 1 _ 0 _ 0 _
5 _ 1 _ 2 _ 0 _ 0 _ 0 _ 1 _ 0 _ 0 _

Add a comment
Know the answer?
Add Answer to:
Collections (a) What are generic types and what is their importance in ADTs for collections? (b)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • ArrayQueue

    Implement the ArrayQueue classIn the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.Many classes are given to youDownload and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT