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

/* StackArray * Anderson, Franceschi */ import java.awt.Graphics; import javax.s

ID: 3778082 • Letter: #

Question

/* StackArray
* Anderson, Franceschi
*/

import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Color;

public class StackArray
{
public static final int CAPACITY = 10;

private int[] stack;
private int top;

public StackArray()
{
stack = new int[CAPACITY];
top = -1;
}

public int get(int index)
{
return stack[index];
}

public int getTop()
{
return top;
}

/**
* push method
*
* @param value value to be pushed onto the stack
* @return true if successful, false if unsuccessful
*/
public boolean push(int value)
{
// ***** 1. Student code starts here *****
// stack is an int array instance variable representing
// the array that stores our stack

// top is an instance variable representing
// the index of the top of the stack
// CAPACITY is a constant instance variable representing
// the size of the array stack
// The push method adds the argument value
// to the top of the stack, if it is possible
// code the push method here
// Part 1 student code starts here:

return true; // replace this dummy return statement

// Part 1 student code ends here.

Explanation / Answer

package snippet;

public class StackArray {
   public static final int CAPACITY = 10;
   private int[] stack;
   private int top;
   public StackArray()
   {
   stack = new int[CAPACITY];
   top = -1;
   }
   public int get(int index)
   {
   return stack[index];
   }
   public int getTop()
   {
   return top;
   }
   /**
   * push method
   *
   * @param value value to be pushed onto the stack
   * @return true if successful, false if unsuccessful
   */
   public boolean push(int value)
   {
      
       if(top==CAPACITY-1)
       {
           return false;
       }
       else
       {
           stack[++top] = value;
           return true;
       }
      
      
   }
  
   // Part 1 student code ends here.

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       StackArray o=new StackArray();
       o.push(10);
       o.push(20);
       o.push(30);
       o.push(40);
       o.push(50);
       o.push(60);
       o.push(70);
       o.push(80);
       o.push(90);
       o.push(100);
       System.out.println("10 elements inserted");
       for(int i=0;i<10;i++)
       {
           System.out.println("element is "+o.get(i));
       }
       if(o.push(110))
       {
           System.out.println("11 elements inserted successfully");
       }
       else
       {
           System.out.println("Stack full");
       }

   }

}

===========================================================================

OpenJDK 64-Bit Server VM warning: Insufficient space for shared memory file:
5469
Try using the -Djava.io.tmpdir= option to select an alternate temp location.

10 elements inserted
element is 10
element is 20
element is 30
element is 40
element is 50
element is 60
element is 70
element is 80
element is 90
element is 100
Stack full