Stack - File Stack Author Mithali Shenoy([email protected]

This preview shows page 1 - 2 out of 2 pages.

/* File: Stack * Author: Mithali Shenoy ([email protected]) * Date: 5/29/2013 * Class: CS 112, Summer Term 01, 2013 * Assignment: HW 02 * Purpose: This is a class that implements the interface Stackable. * Platform: Dr. Java */ public class Stack implements Stackable { public static void main (String[] args){ Stack K = new Stack(); K.push(5); K.push(10); System.out.println("Stack should have 5 and 10"); System.out.println(K.toString()); K.pop(); System.out.println("Now stack should only have a 5 after popping the 10"); System.out.println(K.toString()); System.out.println("boolean statement should be false:"); System.out.println(K.isEmpty()); } private int[] A = new int[100]; //array created private final int DEFAULT_SIZE = 20; private int next; //acts as a pointer for the next open slot in an array
Image of page 1

Subscribe to view the full document.

Image of page 2

Unformatted text preview: private int size; //size of array //constructor for Stack that constructs an array public Stack() { A = new int[DEFAULT_SIZE]; size = DEFAULT_SIZE; next = 0; } //push inputs an integer into the array public void push(int n) { if(next >= size) { int B = new int[size*2]; for(int i = 0; i < size; ++i) B[i] = A[i]; A = B; size = size * 2; } A[next++] = n; } //pop removes the last inputted element in an array public int pop() { return A[--next]; } //boolean that determines if the array is empty public boolean isEmpty() { return ( next == 0 ); } //toString method that prints out what is in the stack every operation public String toString(){ String s="Stack:"; for(int j = 0; j < next; ++j){ s= s+ A[j]+" "; } return s; } }...
View Full Document

  • Spring '13
  • DaveSullivan
  • new int, private int, private final int, public Stack

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern