Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Stacks

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stacks

    in this code snippet
    public void push(Book book) {

    if(size() == capacity)
    doubleStackArray();
    stack[++top] = book;//***

    if i want to push a a Book object, I am stuck at //***


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stacks

    Please explain what the problem is. What is the stack variable? It is coded like an array.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stacks

    Quote Originally Posted by Norm View Post
    Please explain what the problem is. What is the stack variable? It is coded like an array.

    In fact
    1.I have created a Book class which contains the above book attributes and accessor methods.
    2.Then, I created the BookApp class where I input the values for attributes and create an object of Book class and parameters.
    The problem comes here----->>>
    2. I created a BookStack class which contains the stack methods but for the push method, I cannot pass a book object as parameter..plz help.
    Is my logic OK?

    question was:
    Write a program that keeps the following information on books:
    • Book title
    • Barcode
    • Author Name
    • Price
    • Edition
    Books from ten different authors need to be piled in such a way that the user can access the
    information of one book at a time.
    Note: Use an array implementation for the stack.
    Your program should validate the following:
    • Addition of more than 10 books to your stack.
    • Deletion of books when your stack is empty.



    Please edit your post and wrap your code with
    [code=java]
    < public class Book {
    private String BookTitle;
    private int barcode;
    private String AuthorName;
    private int price;
    private String Edition;

    public Book(){
    BookTitle=" ";
    AuthorName=" ";
    barcode=0;
    price=0;
    Edition=" ";
    }

    public Book(String BookTitle,int barcode,String AuthorName,int price,String Edition){
    this.BookTitle=BookTitle;
    this.AuthorName=AuthorName;
    this.price=price;
    this.Edition=Edition;
    }
    public String gettitle(){
    return BookTitle;
    }
    public int getcode(){
    return barcode;
    }
    public String getName(){
    return AuthorName;
    }
    public int getPrice(){
    return price;
    }
    public String getEdition(){
    return Edition;
    }

    }

    BOOKSTACKCLASS
    class BookStack{
    private int capacity;
    private int[] stack;
    private int top;

    public BookStack(){
    capacity = 10;
    stack = new int[10];
    top = -1;
    }

    public BookStack(int size){
    capacity = size;
    stack = new int[size];
    top = -1;
    }

    public void display(){
    for(int j=0;j<stack.length;j++){
    System.out.println(stack[j]);
    }
    }

    public int size(){
    return top + 1;
    }

    public boolean isEmpty(){
    return top == -1;
    }

    public boolean isFull() { // returns true if stack is full
    return (size() == capacity);
    }

    public int peek(){

    // an exception is raised if stack is empty
    return stack[top];
    }

    public int pop(){

    // an exception is raised if stack is empty
    return stack[top--];
    }


    public void doubleStackArray(){
    int[] newStack = new int[capacity*2];
    System.arraycopy(stack, 0, newStack, 0, capacity);
    stack = newStack;
    capacity = capacity * 2;
    }

    public void push(Book book) {///THE PROBLEM IS HERE.. WHAT DO I NEED TO PASS?

    if(size() == capacity)
    doubleStackArray();
    stack[++top] = book;
    // TODO Auto-generated method stub

    }
    } // end class BookStack

    BOOKAPP CLASS
    import java.util.Scanner;


    public class BookApp {

    /**
    * @param args
    */
    public static void main(String[] args) {
    Scanner scan=new Scanner(System.in);
    // TODO Auto-generated method stub
    BookStack arraybook=new BookStack(10);
    for(int i=0;i<10;i++){
    System.out.println("Enter booktitle:");
    String booktitle=scan.next();
    System.out.println("Enter barcode:");
    int barcode=scan.nextInt();
    System.out.println("Enter authorname:");
    String authorname=scan.next();
    System.out.println("Enter price:");
    int price=scan.nextInt();
    System.out.println("Enter edition:");
    String edition=scan.next();

    Book book=new Book(booktitle,barcode,authorname,price,edition);
    arraybook.push(book);

    BookStack s1=new BookStack(10);
    s1.push(book);

    }

    }

    }

    Reply With Quote Reply With Quote >
    [/code]
    to get highlighting and preserve formatting.
    In fact
    1.I have created a Book class which contains the above book attributes and accessor methods.
    2.Then, I created the BookApp class where I input the values for attributes and create an object of Book class and parameters.
    The problem comes here----->>>
    2. I created a BookStack class which contains the stack methods but for the push method, I cannot pass a book object as parameter..plz help.
    Is my logic OK?

    question was:
    Write a program that keeps the following information on books:
    • Book title
    • Barcode
    • Author Name
    • Price
    • Edition
    Books from ten different authors need to be piled in such a way that the user can access the
    information of one book at a time.
    Note: Use an array implementation for the stack.
    Your program should validate the following:
    • Addition of more than 10 books to your stack.
    • Deletion of books when your stack is empty.


  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stacks

    I cannot pass a book object as parameter
    Please post the full text of any error messages.
    Or explain what the problem is.

    The code needs to be in code tags, not quote tags.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Stacks
    By RoshanDiMatteo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 3rd, 2012, 10:21 AM
  2. Stacks problem-help
    By RoshanDiMatteo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2012, 07:19 AM
  3. stacks
    By bd0652 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 10th, 2012, 11:48 AM
  4. Help with Stacks
    By animelook in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 12:58 AM
  5. help for stacks
    By msa0127a in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 3rd, 2010, 12:11 AM

Tags for this Thread