Skip to main content
deleted 51 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

A simple Simple array-based Stack class in Java, what are my mistakes?

I am testing this for mistakes, but since I am learning Java and, my tests may not be as comprehensive as they should. So I decided to ask help from CodeReview users.

A simple array-based Stack class in Java, what are my mistakes?

I am testing this for mistakes, but since I am learning Java and my tests may not be as comprehensive as they should. So I decided to ask help from CodeReview users.

Simple array-based Stack class in Java

I am testing this for mistakes, but since I am learning Java, my tests may not be as comprehensive as they should.

edited tags
Link
Eva
  • 1.1k
  • 8
  • 21
Tweeted twitter.com/#!/StackCodeReview/status/256590481662832641
added int size() method
Source Link
malhobayyeb
  • 307
  • 3
  • 10
import java.util.*;

public class SJUStack<E> {
    // Data Fields
    private E[] theData;
    private int topOfStack = -1;
    private static final int INITIAL_CAPACITY = 10;
    private int size = 0;
    private int capacity = 0;
    
    // Constructors
    public SJUStack(int initCapacity) {
        capacity = initCapacity;
        theData = (E[]) new Object[capacity];
    }
    
    public SJUStack() {
        this(INITIAL_CAPACITY);
    }
    
    // Methods
    public E push(E e) {
        if(size == capacity) {
            reallocate();
        }
        theData[size] = e;
        size++;
        topOfStack++;
        
        return e;
    } // End push(E e) method
    
    public E peek() {
        if(empty()) {
            throw new EmptyStackException();
        }
        return theData[topOfStack];
    } // End peek() method
    
    public E pop() {
        E result = peek();
        theData[topOfStack] = null;
        size--;
        topOfStack--;
        if(size <= (capacity/4) && capacity >= INITIAL_CAPACITY) {
            shrink();
        }
        return result;
    } // End pop() method
    
    public boolean empty() {
        return size == 0;
    } // End empty() method
    
    private void reallocate() {
        capacity *= 2;
        theData = Arrays.copyOf(theData, capacity);
    } // End reallocate() method
    
    private void shrink() {
        capacity /= 2;
        theData = Arrays.copyOf(theData, capacity);
    } // End shrink() method
    
    public String toString() {
        return Arrays.toString(theData);
    } // End toString() method 

    public int size() {
        return size;
    } // End size() method
}
import java.util.*;

public class SJUStack<E> {
    // Data Fields
    private E[] theData;
    private int topOfStack = -1;
    private static final int INITIAL_CAPACITY = 10;
    private int size = 0;
    private int capacity = 0;
    
    // Constructors
    public SJUStack(int initCapacity) {
        capacity = initCapacity;
        theData = (E[]) new Object[capacity];
    }
    
    public SJUStack() {
        this(INITIAL_CAPACITY);
    }
    
    // Methods
    public E push(E e) {
        if(size == capacity) {
            reallocate();
        }
        theData[size] = e;
        size++;
        topOfStack++;
        
        return e;
    } // End push(E e) method
    
    public E peek() {
        if(empty()) {
            throw new EmptyStackException();
        }
        return theData[topOfStack];
    } // End peek() method
    
    public E pop() {
        E result = peek();
        theData[topOfStack] = null;
        size--;
        topOfStack--;
        if(size <= (capacity/4) && capacity >= INITIAL_CAPACITY) {
            shrink();
        }
        return result;
    } // End pop() method
    
    public boolean empty() {
        return size == 0;
    } // End empty() method
    
    private void reallocate() {
        capacity *= 2;
        theData = Arrays.copyOf(theData, capacity);
    } // End reallocate() method
    
    private void shrink() {
        capacity /= 2;
        theData = Arrays.copyOf(theData, capacity);
    } // End shrink() method
    
    public String toString() {
        return Arrays.toString(theData);
    } // End toString() method
}
import java.util.*;

public class SJUStack<E> {
    // Data Fields
    private E[] theData;
    private int topOfStack = -1;
    private static final int INITIAL_CAPACITY = 10;
    private int size = 0;
    private int capacity = 0;
    
    // Constructors
    public SJUStack(int initCapacity) {
        capacity = initCapacity;
        theData = (E[]) new Object[capacity];
    }
    
    public SJUStack() {
        this(INITIAL_CAPACITY);
    }
    
    // Methods
    public E push(E e) {
        if(size == capacity) {
            reallocate();
        }
        theData[size] = e;
        size++;
        topOfStack++;
        
        return e;
    } // End push(E e) method
    
    public E peek() {
        if(empty()) {
            throw new EmptyStackException();
        }
        return theData[topOfStack];
    } // End peek() method
    
    public E pop() {
        E result = peek();
        theData[topOfStack] = null;
        size--;
        topOfStack--;
        if(size <= (capacity/4) && capacity >= INITIAL_CAPACITY) {
            shrink();
        }
        return result;
    } // End pop() method
    
    public boolean empty() {
        return size == 0;
    } // End empty() method
    
    private void reallocate() {
        capacity *= 2;
        theData = Arrays.copyOf(theData, capacity);
    } // End reallocate() method
    
    private void shrink() {
        capacity /= 2;
        theData = Arrays.copyOf(theData, capacity);
    } // End shrink() method
    
    public String toString() {
        return Arrays.toString(theData);
    } // End toString() method 

    public int size() {
        return size;
    } // End size() method
}
Source Link
malhobayyeb
  • 307
  • 3
  • 10
Loading