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 6 of 6

Thread: Stacks problem-help

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

    Default Stacks problem-help

    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.


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Stacks problem-help

    please do your own assignments and ask us for hints if you are sruck somewhere.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Stacks problem-help

    I will request Admins to block users who just sign up for getting someone else do their assignment or want to get spoon feed.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

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

    Default Re: Stacks problem-help

    Quote Originally Posted by RoshanDiMatteo View Post
    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.
    -------------------------------------------------------------------------------------------------------------------
    I am a beginner in Java
    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?
    BOOK CLASS

    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);

    }

    }

    }

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

    Default Re: Stacks problem-help

    sorry about last post, i won't repeat next time, but a simple question, is it possible to push objects on stacks? and what do i need to modify in my push method in Stack class?

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Stacks problem-help

    Yes, it's the default behaviour how a stack must be implemented. Though you can change.
    Yes, it is possible to push objects in stack.
    Depends upon the design of your problem, how you want the actual thing to be.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. Java Stacks question
    By sim18 in forum Java Theory & Questions
    Replies: 10
    Last Post: October 9th, 2012, 03:07 PM
  2. stacks
    By bd0652 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 10th, 2012, 12:48 PM
  3. Help with Stacks
    By animelook in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 12:58 AM
  4. help for stacks
    By msa0127a in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 3rd, 2010, 01:11 AM
  5. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 04:05 AM

Tags for this Thread