Fers47 Fers47 - 6 months ago 30
Java Question

Stack pushing() is not pushing

Hello stackoverflow users,

I have a 4 different types of stacks(pennies, nickels, dimes, and quarters).
Each of these stacks contains how many of each objects there are.
When I push a penny to its correct stack, it does not actually push it because the push method thinks the stack is full or it think it is empty. How can I correct my push method and my isFull() method? How can I tell the program that the stack is not full nor empty to continue to stack it without getting a thrown out of bound error?

I have only implemented pennies so far.

Here are my codes for the two classes:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CoinStack implements CoinStackInterface
{
static Coin[] array;
int index=0;
int capacityOfHolder;
public CoinStack(int capacityOfHolder)
{
array=new Coin[capacityOfHolder];
}
public boolean isEmpty()
{
return index==0;
}
public boolean isFull()
{
return index==array.length;
}
public Coin top()
{
if(isEmpty())
{
return null;
}
else
{
return array[index-1];
}
}
public void push(Coin coin)
{
if(!isFull())
{
array[index++]=coin;
index++;
}
}
public void pop()
{
if(!isEmpty())
{
index--;
}
}
public int getAmount()
{
return index;

}

}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;


.

public class Driver
{

public static void main(String[] args)throws FileNotFoundException
{
int counterpenny=0;
File file=new File(args[0]);
Scanner sc = new Scanner((file));
//Creates a new coin stack for pennies
CoinStack penny = new CoinStack(2);
CoinStack nickel= new CoinStack(0);
CoinStack dime= new CoinStack(0);
CoinStack quarter= new CoinStack(0);
//Creates a new type of coin Penny, with the value of it
Coin penny2= new Coin(1);
Coin nickel2= new Coin(5);
Coin dime2= new Coin(10);
Coin quarter2= new Coin(25);
int x=0;
//while the scanner has a number next to the space keep going
while(sc.hasNext())
{
int currentValue=sc.nextInt();
//if the next value is equal to 1
if(currentValue==1)
{
//creates a new penny with a value of 1
Coin penny3= new Coin(1);
//push the new penny to the penny stack, increasing its stack
penny.push(penny3);
}
if(currentValue==5)
{
nickel.push(nickel2);
}
if(currentValue==10)
{
dime.push(dime2);

}
if(currentValue==25)
{
quarter.push(quarter2);

}

}
System.out.println(penny.getAmount());
}
}

Answer Source

Comments inside the code:

public class CoinStack implements CoinStackInterface
{
  // don't make it static! Learn what static means!!!
  static Coin[] array;
  int index=0;

  // Why do you need this? 
  // Once created, you get the capacity from array.length
  // You never use this in your code anyway
  int capacityOfHolder;

  public CoinStack(int capacityOfHolder)
  {
    array=new Coin[capacityOfHolder];      
  }

  /// skipped

  public void push(Coin coin)
  {
    // Why do you disable pushing in an empty stack???
    if( ! isEmpty() && ! isFull() )
    {
      array[index++]=coin;
      index++;
    }
  }

  // skipped

}

In main:

  // this create a stack with what capacity?
  // How many coins you can push in here before the stack is full?
  CoinStack nickel= new CoinStack(0);