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

In Java create this Stack Based Virtual Machine, using the provided Int stack co

ID: 3693343 • Letter: I

Question

In Java create this Stack Based Virtual Machine, using the provided Int stack code.

CHART WITH INSTRUCTIONS:

INT STACK CODE:

interface IntStack
{
public void push(int element);
public int pop();
public int peek();
public boolean isEmpty();
public boolean isFull();
public int size();
public String toString();
}

public class Stack implements IntStack
{

private int[ ] stack;
   private int sp;
  

   public Stack()
   {
       stack = new int[10];
       sp = -1;
   }
  
   public Stack(int n)
   {
       stack = new int[n];
       sp = -1;
   }


public void push(int element)
   {
       if (!isFull())
       {
sp++;
stack[sp] = element;
}
       else   
{
System.out.println("Error...stack is full");
System.exit(0);
}
  
}
  
public int pop()
   {
int rv = 0;
  
       if (!isEmpty())
       {
       rv = stack[sp];
       sp--;
       }
       else
       {
       System.out.println("Error!");
System.exit(0);
       }
  
return rv;
}
  
public int peek()
   {
int rv = 0;
  
       if (!isEmpty())
       {
       rv = stack[sp];
   }
else
{
       System.out.println("Empty Stack Error!");
       }
return rv;
   }
     
public boolean isEmpty()
   {
       return sp == -1;
}

public boolean isFull()   
{
   return sp == stack.length - 1;
   }

   public int size()
   {
       return sp+1;
   }

public String toString()
   {  
       String rs = "stack pointer ->";
       if (!isEmpty())
       {
           for(int i = sp; i >=0; i--)
           {
           rs = rs + (stack[i]) + " ";
}
  
       }
       else
       {  
           rs = rs + "Empty stack!";
       }
  
return rs;
   }

Explanation / Answer

public void push(int element);
public int pop();
public int peek();
public boolean isEmpty();
public boolean isFull();
public int size();
public String toString();
}

public class Stack implements IntStack
{

private int[ ] stack;
   private int sp;
  

   public Stack()
   {
       stack = new int[10];
       sp = -1;
   }
  
   public Stack(int n)
   {
       stack = new int[n];
       sp = -1;
   }


public void push(int element)
   {
       if (!isFull())
       {
sp++;
stack[sp] = element;
}
       else   
{
System.out.println("Error...stack is full");
System.exit(0);
}
  
}
  
public int pop()
   {
int rv = 0;
  
       if (!isEmpty())
       {
       rv = stack[sp];
       sp--;
       }
       else
       {
       System.out.println("Error!");
System.exit(0);
       }
  
return rv;
}
  
public int peek()
   {
int rv = 0;
  
       if (!isEmpty())
       {
       rv = stack[sp];
   }
else
{
       System.out.println("Empty Stack Error!");
       }
return rv;
   }
     
public boolean isEmpty()
   {
       return sp == -1;
}

public boolean isFull()   
{
   return sp == stack.length - 1;
   }

   public int size()
   {
       return sp+1;
   }

public String toString()
   {  
       String rs = "stack pointer ->";
       if (!isEmpty())
       {
           for(int i = sp; i >=0; i--)
           {
           rs = rs + (stack[i]) + " ";
}
  
       }
       else
       {  
           rs = rs + "Empty stack!";
       }
  
return rs;
   }

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