I keep receiving an error that says that there are incompatible types. I copied this directly out of a book because we are supposed to make changes to the code to enhance the game of War. I have all of the other classes complete and compiled but this one is giving me fits. Here is the code:
public class ArrayStack<E> implements Stack<E> {
private E[] data;
private int size;
public ArrayStack() {
data = (E[])(new Object[1]);
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public Object peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
protected boolean isFull() {
return size == data.length;
}
public void push(Object target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]);
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
The error is occurring in the push() method at the data[size] = target; line.
EDIT:::
I'm now receiving this error.
"type Stack does not take parameters
public class ArrayStack implements Stack"
The stack class is as follows.
public interface Stack<E> {
public boolean isEmpty();
public E peek();
public E pop();
public void push(E target);
}
Change Object to E as the push() method's parameter type.
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
Likewise, you should also change the declare return type of pop() and peek() to E.
public E pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public E peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
Now your class is fully generic.
push method is not generic like the rest of the class, change it to:
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
In any case the JDK ships with the class ArrayDeque which fulfill your requirements without being a piece o code pasted from a book.
ArrayDeque<YourObj> stack = new ArrayDeque<YourObj>();
stack.push(new YourObj());
YourObj head = stack.peek();
head = stack.pop();
Related
For this assignment I need to create a linked stack class that contains a getMin() and getMax(). I cannot change the class header which was provided by the instructor. Both getMin and getMax should be O(1) time.
My thought is that I need to use the compareTo method to compare entries as they are pushed or poped so that I can set variables minValue and maxValue equal to their respective values. However, I don't understand the section in the class header <T extends Comparable<? super T>> nor do I know how or where to implement Comparable. I tried having my class Node<E> implement Comparable but it asked me to override the compareTo method and I'm not sure how that would work.
Any help would be greatly appreciated! Below is my code for this assignment:
public class MinMaxStack <T extends Comparable<? super T>> implements StackADT<T> {
private Node<T> top;
private int size;
public MinMaxStack() {
clear();
}
private class Node<E>{
E data;
Node<E> previous;
}
public T getMin() {
if(isEmpty()) {
throw new EmptyCollectionException("The stack is empty but is trying to getMin.");
} else {
return null;
}
}
public T getMax() {
return null;
}
#Override
public T pop() {
if(isEmpty()) {
throw new EmptyStackException("Stack is empty but trying to pop.");
}else {
T dataToReturn = top.data;
top = top.previous;
size -= 1;
return dataToReturn;
}
}
#Override
public T peek() {
if(isEmpty()) {
throw new EmptyStackException("Stack is empty but trying to peek");
}else {
return top.data;
}
}
#Override
public void push(T newItem) {
Node<T> newNode = new Node<>();
newNode.data = newItem;
if(!isEmpty()) {
newNode.previous = top;
}
top = newNode;
size += 1;
}
#Override
public int getSize() {
return size;
}
#Override
public void clear() {
while(!isEmpty()) {
top = null;
size = 0;
}
}
#Override
public boolean isEmpty() {
return size == 0;
}
}
I am currently working on a Stack project where I am creating a generic Stack class. I've been looking on stack overflow for this, but couldn't find it. I need help creating a pop method in my code.
Here is what I have so far:
public class Stack<E>
{
public static final int DEFAULT_CAPACITY = 10;
private E [] elementData;
private int size;
#SuppressWarnings("unchecked")
public Stack()
{
this.elementData = (E[]) new Object[DEFAULT_CAPACITY];
}
#SuppressWarnings("unchecked")
public Stack(int capacity)
{
if(capacity < 0)
{
throw new IllegalArgumentException("capacity " + capacity);
}
this.elementData = (E[]) new Object[capacity];
}
public boolean isEmpty()
{
if(size == 0)
{
return true;
}
else
{
return false;
}
}
/*
The push method should add its parameter to the top of the stack.
*/
public void push(E item)
{
ensureCapacity(size+1);
elementData[size] = item;
size++;
}
private void ensureCapacity(int capacity)
{
if(elementData.length < capacity)
{
int newCapacity = elementData.length * 2 + 1;
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
I need help right here. I need to have the pop method remove and return the element at the top of the stack. If no items
are present, it should throw an "EmptyStackException".
public E pop()
{
if(isEmpty())
{
throw EmptyStackException
}
else
{
}
}
}
I figured it out, I give the credit to #ScaryWombat. The code is:
public E pop()
{
if(isEmpty())
{
throw new EmptyStackException();
}
else
{
return elementData[--size];
}
}
public E pop()
{
E item;
if(isEmpty())
{
throw new EmptyStackException();
}
else
{
item = elementData[size];
size--;
}
return item;
}
You need to make your return variable equal to the top of your stack array and then decrement your stack array. Also, you need to initialize your stack by setting size=0.
Completely stuck on this error. Here's the class the error is coming from.
/** An array-based Stack. */
public class ArrayStack<E> implements Stack {
/** Array of items in this Stack. */
private E[] data;
/** Number of items currently in this Stack. */
private int size;
/** The Stack is initially empty. */
public ArrayStack() {
data = (E[])(new Object[1]); // This causes a compiler warning
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public Object peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
/** Return true if data is full. */
protected boolean isFull() {
return size == data.length;
}
public void push(Object target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
/** Double the length of data. */
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
Here's the Stack class just in case it's needed:
/** A last-in, first-out stack. */
public interface Stack<E> {
/** Return true if this Stack is empty. */
public boolean isEmpty();
/**
* Return the top item on this Stack, but do not modify the Stack.
* #throws EmptyStructureException if this Stack is empty.
*/
public E peek();
/**
* Remove and return the top item on this Stack.
* #throws EmptyStructureException if this Stack is empty.
*/
public E pop();
/** Add target to the top of the Stack. */
public void push(E target);
}
The error is in regards to the line data[size] = target; in the ArrayStack class, in the push(Object target) method.
data[size] = target;
1) Here data refers to E array and target refers to Object.
Generics brings type safety. So you cannot cannot convert from Object to E.
2) public class ArrayStack<E> implements Stack {
is not correct because you don't provide the parameterized type for the interface you are implementing.
Writing public class ArrayStack<E> implements Stack<E> { would be safer and will force you to implement the Stack methods by respecting the parameterized type used in the Stack methods.
For example : public void push(E target);
3) To be conform to declared parameterized type in ArrayStack you should should use the parameterized type in your method instead of Object.
so you should replace
public void push(Object target) {
by
public void push(E target) {
And you should do the same thing in all methods you declare that manipulate as declared type Object instead of E. For example :
public Object peek()
and
public Object pop() {
Since your data array is of type E:
private E[] data;
You need to modify:
public void push(Object target) {
to have parameter type E.
E can be String, Integer etc determined at runtime. Object is not equivalent to the runtime type.
Target and data are not of the same type. You can use E wherhever you need a type. This makes your code more type safe.
Change the push method to:
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
Try this:
/**
* An array-based Stack.
*/
public class ArrayStack<E> implements Stack<E> {
/**
* Array of items in this Stack.
*/
private E[] data;
/**
* Number of items currently in this Stack.
*/
private int size;
/**
* The Stack is initially empty.
*/
public ArrayStack() {
data = (E[]) (new Object[1]); // This causes a compiler warning
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public E pop() {
if (isEmpty()) {
throw new RuntimeException();
}
size--;
return data[size];
}
public E peek() {
if (isEmpty()) {
throw new RuntimeException();
}
return data[size - 1];
}
/**
* Return true if data is full.
*/
protected boolean isFull() {
return size == data.length;
}
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
/**
* Double the length of data.
*/
protected void stretch() {
E[] newData = (E[]) (new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
I've been trying to turn this generic arraylist class into an array but I haven't been able to get it to work. I've hit a roadblock at the push() and pop() methods. Any help is appreciated.
Here's the original class:
public class GenericStack<E> {
private java.util.ArrayList<E> list = new java.util.ArrayList<E>();
public int getSize() {
return list.size();
}
public E peek() {
return list.get(getSize() - 1);
}
public E push(E o) {
list.add(o);
return o;
}
public E pop() {
E o = list.get(getSize() - 1);
list.remove(getSize() - 1);
return o;
}
public boolean isEmpty() {
return list.isEmpty();
}
}
And here's my revised class so far:
public class GenericStack<E> {
public static int size = 16;
#SuppressWarnings("unchecked")
private E[] list = (E[])new Object[size];
public void add(int index, E e) {
ensureCapacity();
for (int i = size - 1; i >= index; i--) {
list[i + 1] = list[i];
list[index] = e;
size++;
}
}
public int getLength() {
return list.length;
}
public E peek() {
E o = null;
o = list[0];
return o;
}
public E push(E o) {
ensureCapacity();
list.append(o);
size++;
return o;
}
public E pop() {
E o = null;
for (int i = 0; i > list.length; i++) {
o = list[i - 1];
}
list[list.length - 1] = null;
size--;
return o;
}
private void ensureCapacity() {
if (size >= list.length) {
#SuppressWarnings("unchecked")
E[] newlist = (E[])(new Object[size * 2 + 1]);
System.arraycopy(list, 0, newlist, 0, size);
list = newlist;
}
}
public boolean isEmpty() {
if (list.length > 0) {
return false;
}
else {
return true;
}
}
}
NB: You must first correct your code like mentioned in comments.
It's recommended to use name method like of official Stack class, so there are 5 methods: empty() peek() pop() push(E item) search(Object o).
You should declare initial size of your array as a constant and an other variable for current size and all your attributes should be private like that:
private final int MAX_SIZE = 16;
private int currentSize=0;
There is the code of peek() method:
public E peek() {
E o = null;
o = list[currentSize-1];
return o;
}
There is the code of push(E o) method:
public E push(E o) {
list[currentSize]=o;
currentSize++;
return o;
}
There is the code of pop() method this method must throw EmptyStackException - if this stack is empty:
public E pop() {
E o = null;
if(currentSize>0){
o=list[currentSize - 1];
list[currentSize - 1] = null;
currentSize--;
return o;
}else{
throw new EmptyStackException();
}
}
There is the code of empty() method:
public boolean empty() {
if (currentSize > 0) {
return false;
}
else {
return true;
}
}
This question already has answers here:
Incompatible Types Error in Java
(2 answers)
Closed 9 years ago.
I'm receiving the error "type Stack does not take parameters public class ArrayStack implements Stack" from this code:
public class ArrayStack<E> implements Stack<E> {
private E[] data;
private int size;
public ArrayStack() {
data = (E[])(new Object[1]);
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public Object peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
protected boolean isFull() {
return size == data.length;
}
public void push(Object target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]);
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
"type Stack does not take parameters public class ArrayStack implements Stack"
The stack class is as follows.
public interface Stack<E> {
public boolean isEmpty();
public E peek();
public E pop();
public void push(E target);
}
Your peek() method should like this
public E peek() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
return (E)data[size - 1];
}
Your push() method should like this
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
Your pop() method should like this
public E pop() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return (E)data[size];
}
Now your interface look like below
public interface Stack<E> {
public boolean isEmpty();
public E peek() throws EmptyStructureException;
public E pop() throws EmptyStructureException;
public void push(E target);
}