Question

In: Computer Science

In Lecture 8(ArrayList, Generics) , we discussed about creating a MyStack<E> class using the ArrayList<E> class...

  1. In Lecture 8(ArrayList, Generics) , we discussed about creating a MyStack<E> class using the ArrayList<E> class that has the stack methods ( isEmpty(), getSize(), peek(), pop(), push(Object o), and toString( ) ) as shown in the following:

public class MyStack<E> {

private ArrayList<E> list = new ArrayList<>();

public int getSize() {

    return list.size();

}

public E peek() {

    return list.get(getSize() - 1);

}

public void push(E o) {

    list.add(o);

}

public E pop() {

    E o = list.get(getSize() - 1);

    list.remove(getSize() - 1);

    return o;

}

public boolean isEmpty() {

    return list.isEmpty();

}

@Override

public String toString() {

    return "stack: " + list.toString();

}

}

For this exercise, rewrite the MyStack <E> to the GenericStack<E> class that extends from ArrayList<E>.

import java.util.ArrayList;
import java.util.Iterator;

public class A6Q2 {
   public static void main(String[] args) {
/* After building your GenericStack from the ArrayList, add codes here to build the two Stacks
* (String and Integer) here
*/      
      
      
   }

   // Build a static class GenericStack<E> extends ArrayList

   static class GenericStack<E> extends ArrayList<E> {
  
         
         
   }
   }
Since this is a GenericStack<E> class, show that you can build a String (names) and Integer (Integer) stack using the same generic class.

In java please. Thank You

Solutions

Expert Solution

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


import java.util.ArrayList;

public class A6Q2 {
   public static void main(String[] args) {
       /* After building your GenericStack from the ArrayList, add codes here to build the two Stacks
       * (String and Integer) here
       */
      
       GenericStack<String> fruits = new GenericStack<String>();
       fruits.push("apple");
       fruits.push("mango");
       fruits.push("banana");
       System.out.println("Top of fruits stack: " + fruits.peek());
       System.out.println("Size of fruits stack: " + fruits.size());
       System.out.println("Popped " + fruits.pop());
       System.out.println("Fruits stack contents: " + fruits);
      
       System.out.println("-------");
       GenericStack<Integer> nums = new GenericStack<Integer>();
       nums.push(2);
       nums.push(3);
       nums.push(4);
       System.out.println("Top of number stack: " + nums.peek());
       System.out.println("Size of number stack: " + nums.size());
       System.out.println("Popped " + nums.pop());
       System.out.println("Number stack contents: " + nums);

   }

   // Build a static class GenericStack<E> extends ArrayList

   static class GenericStack<E> extends ArrayList<E> {
      
       public E peek() {
           return get(size()-1);

       }

       public void push(E o) {
           add(o);
       }

       public E pop() {
           return remove(size() - 1);
       }

      
       @Override

       public String toString() {
           String s = "stack: ";
           for(int i = size()-1; i >= 0; i--) {
               if(i != size() - 1)
                   s += ", ";
               s += get(i);
           }
           return s;
          
       }

   }
}

output
----
Top of fruits stack: banana
Size of fruits stack: 3
Popped banana
Fruits stack contents: stack: mango, apple
-------
Top of number stack: 4
Size of number stack: 3
Popped 4
Number stack contents: stack: 3, 2


Related Solutions

(1) Recall on February 6 in class we discussed e 0 + e 2πi/n + e...
(1) Recall on February 6 in class we discussed e 0 + e 2πi/n + e 4πi/n + · · · + e 2(n−1)πi/n = 0 and in order to explain why it was true we needed to show that the sum of the real parts equals 0 and the sum of the imaginary parts is equal to 0. (a) In class I showed the following identity for n even using the fact that sin(2π − x) = − sin(x):...
Describe how the “kidney” works in amphixous using the anatomical terms we discussed in lecture and...
Describe how the “kidney” works in amphixous using the anatomical terms we discussed in lecture and then a possible mechanism of how it may move materials from the blood
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new...
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new java.util.ArrayList<>(); public int size() { return list.size(); } public E peek() { return list.get(size() - 1); } public void push(E o) { list.add(o); } public E pop() { E o = list.get(size() - 1); list.remove(size() - 1); return o; } public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return "stack: " + list.toString(); } } package Modul02; public class TestGenericStack...
Problem 1 (Concepts of safe distances): In lecture 7 and 8, we discussed five (5) types...
Problem 1 (Concepts of safe distances): In lecture 7 and 8, we discussed five (5) types of safe distances, such as braking distance, deceleration distance, stopping sight distance, decision sight distance, and passing sight distance. Explain what they are in terms of definition, situation of concern, and possible applications for analysis, design and management (at least one example for each
1. We discussed in the lecture that the coronavirus is both an AS and AD shock....
1. We discussed in the lecture that the coronavirus is both an AS and AD shock. Based on the lecture, draw a graph showing both of the movements. You need a properly drawn graph as you did for homework 4. Update for clarity: For this question, we’re only capturing the effect of the virus itself. Don’t assume there are any particular policies associated with these shocks. It’s probably easiest to think of the AS shock as the reduced labor force...
In our lecture about the 2008 Financial Crisis, we discussed the “Financial Crisis Death Spiral” highlighting...
In our lecture about the 2008 Financial Crisis, we discussed the “Financial Crisis Death Spiral” highlighting the major disadvantages of the non-bank finance channel. Discuss these disadvantages.
In this week's lecture we have discussed the concept of insider trading as it exists and...
In this week's lecture we have discussed the concept of insider trading as it exists and is viewed around the world. Discuss your view on how insider trading should be viewed
Consider the field experiment with politicians that we discussed in the lecture. The experiment is based...
Consider the field experiment with politicians that we discussed in the lecture. The experiment is based on a modified dictator game. In Treatment 1 (i.e. T1), nature plays with high probability (equal to 0.8) and randomly assigns the endowment either to the politician-dictator or to the recipient. The politician--dictator plays with complementary probability (and knows, when making a decision, that this decision will be implemented); in contrast, a recipient who receives zero (or the full endowment), will not know whether...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
In reference to the lecture we had about the yield curve, the lecture note and the...
In reference to the lecture we had about the yield curve, the lecture note and the video posted in the topic 1 folder in the Course materials, discuss: 1. What is inverted yield curve? (2 points) 2. Why is it interpreted as the sign of imminent recession? (4 points) 3. What causes the inverted yield curve? (4 points) It is absolutely crucial to provide the logical reasoning & the schematic account of the financial market process.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT