Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

trouble creating stack using array

i was trying to create a stack using a array of objects of a class called "stock".
public class stock{
int price;
int qty;
}

in public static void main(), i was creating an object of the class:
stock a= new stock();
and accepting values for a.price and a.qty from user.
i would then push the object 'a' into stack.

This was in a loop. i would ask the user to enter fresh values for a.price and a.qty and then perform a push operation, stack1.push(a).
However, i noticed that when i printed the elements of the stack, they were all the same, which was the last value a.

were all the elements of the stack somehow pointing to 'a'?

Comments

  • Avenger625Avenger625 Member Posts: 8
    You should have posted your code otherwise it becomes really difficult to understand what you did and you are trying to do.
    Anyways, I'm posting this code as far as I have understood your problem-

    Code:

    import java.io.*;

    class Stock
    {
    int price;
    int qty;

    Stock()
    {
    price=qty=-1;
    }
    //Define any methods as applicable.
    }

    class Stack
    {
    Stock db[];
    int pos;

    Stack()
    {
    db= new Stock[10];
    pos=-1;
    }

    void push(Stock a)
    {
    if(pos==9)
    System.out.println("Stack Overflow");
    else
    {
    db[++pos]=new Stock();
    db[pos].price=a.price;
    db[pos].qty=a.qty;
    }
    }

    Stock pop()
    {
    if(pos<0)
    {
    System.out.println("Stack Underflow");
    return (new Stock());
    }
    else
    return db[pos--];
    }
    }


    class StackTest
    {
    public static void main(String A[]) throws IOException
    {
    DataInputStream br=new DataInputStream(System.in);
    Stock a=new Stock();
    Stack myDB=new Stack();

    System.out.print("Enter Qntity:");
    a.qty=Integer.parseInt(br.readLine());
    System.out.print("
    Enter Price:");
    a.price=Integer.parseInt(br.readLine());
    myDB.push(a);

    //Modify as needed....
    System.out.println("Qnty:" + myDB.db[0].qty+" Price:"+ myDB.db[0].price);

    }
    }



    Hope, this helps......!!!

  • j2me64j2me64 Member Posts: 20
    try my code. if you have questions, let me know.

    [code]class Stock {
    // the price and qty are private, so provide getters to get values
    private int price;
    private int qty;

    // the constructor accept a price and a qty
    Stock(int price, int qty) {
    this.price = price;
    this.qty = qty;
    }

    public int getPrice() {
    return price;
    }

    public int getQty() {
    return qty;
    }
    }

    class Stack {
    Stock[] db;
    int pos;

    Stack() {
    db = new Stock[10];
    pos = 0;
    }

    void push(Stock stock) {
    if (pos == 10)
    System.out.println("Stack Overflow");
    else {
    // you can assign a stock, no need to assign single values
    db[pos] = stock;
    pos++;
    }
    }

    public Stock pop() {
    if (--pos < 0) {
    System.out.println("Stack Underflow");
    return null;
    } else
    return db[pos];
    }
    }

    public class StockTest {
    public static void main(String[] args) {
    Stack myDB = new Stack();

    Stock stock1 = new Stock(10, 100);
    Stock stock2 = new Stock(20, 200);
    myDB.push(stock1);
    myDB.push(stock2);
    Stock stock;
    while ((stock = myDB.pop()) != null) {
    System.out.println("Qnty: " + stock.getQty() + " price: "
    + stock.getPrice());
    }
    }
    }
    [/code]
Sign In or Register to comment.