Originally Posted by
Norm
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.