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

Implement MyArrayStack class by extending ArrayList. Please create some variable

ID: 3888256 • Letter: I

Question

Implement MyArrayStack class by extending ArrayList. Please create some variable to test whether whether each fuctions work or not.

import java.util.ArrayList;
import net.datastructures.*;


public class MyArrayStack<E> extends ArrayList<E> implements Stack<E> {

   @Override
   public void push(E e) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public E top() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public E pop() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public int size() {
       // TODO Auto-generated method stub
       return super.size();
   }
  
   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return super.isEmpty();
   }
  
}

Implement MyLinkedListQueue by extending LinkedList. Please create some variable to test whether whether each fuctions work or not.

import java.util.LinkedList;
import net.datastructures.*;

public class MyLinkedListQueue<E> extends LinkedList<E> implements Queue<E>{

   @Override
   public void enqueue(E e) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public E first() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public E dequeue() {
       // TODO Auto-generated method stub
       return null;
   }
  
   @Override
   public int size() {
       // TODO Auto-generated method stub
       return super.size();
   }
  
   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return super.isEmpty();
   }

}

Explanation / Answer

-----------------------------------
Source Code
-----------------------------------


package Sample;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.net.*; //No Need To Import this package
interface Stack<E>
{
void push(E e); //We have to Define Interface called Stack.
E top();
}
class MyArrayStack<E> extends ArrayList<E> implements Stack<E>
{
int key,data,size=0; // class Variables
int[] array=new int[12];
int element;
public void push(E e)
{
data=(int) e;
array[size]=data;
size++;
System.out.println("The Element is inserted Successfully");
}
public E top()
{
return null;
}
public E pop()
{
size--;
element=array[size];
System.out.println("The Element is Deleted:"+element);
return null;
}
public int size()
{
return super.size();
}
public boolean isEmpty()
{
return super.isEmpty();
}

}

interface Queue<T>
{
//we have to define a
Interface called Queue
}
class MyLinkedListQueue<E> extends LinkedList<E> implements Queue<E>
{
int key,data,size=0; // class Variables
int[] array=new int[12];
int element;
public void enqueue(E e)
{
data=(int) e;
array[size]=data;
size++;
System.out.println("The Element is inserted Successfully in
the queue");
}
public E first()
{
return null;
}
public E dequeue()
{
element=array[size];
System.out.println("The Element is Deleted from the queue:"+element);
return null;
}

public int size()
{

return super.size();
}

public boolean isEmpty()
{
return super.isEmpty();
}
}
public class ArrayListdemo
{

public static void main(String[] args)
{
int data;
MyArrayStack obj=new MyArrayStack(); //Create a
class called MyArrayStack

Scanner sc=new Scanner(System.in);
System.out.println("-----------------------------------------");
System.out.println("************Stack operations*************");
System.out.println("Enter The Element to insert in a stack");
data=sc.nextInt();
obj.push(data);
//Calling PushMethod
System.out.println("calling Pop Method To delete the Element");
obj.pop();
//calling Pop() Method
System.out.println("The size of Stack is "+obj.size());
//calling size Method

MyLinkedListQueue obj2=new MyLinkedListQueue(); //
create a class called MyLinkedListQueue
System.out.println("-----------------------------------------");
System.out.println("**************Queue Operations**********");
System.out.println("Enter The Element to insert in a queue");
data=sc.nextInt();
obj2.enqueue(data);
System.out.println("calling Dequeue Method To delete the Element");
obj2.dequeue();
//calling Pop() Method
System.out.println("The size of Queue is "+obj2.size());
//calling size Method
sc.close();
}

}
-------------------------------------------
Expected Output:



-----------------------------------------
************Stack operations*************
Enter The Element to insert in a stack
10
The Element is inserted Successfully
calling Pop Method To delete the Element
The Element is Deleted:10
The size of Stack is 0
-----------------------------------------
**************Queue Operations**********
Enter The Element to insert in a queue
20
The Element is inserted Successfully in the queue
calling Dequeue Method To delete the Element
The Element is Deleted from the queue:0
The size of Queue is 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote