1

i have a book class . I want to add a element at the end of the list

public class Book {
private String title;
private String authorName;
private double price;


/**
 * @param title
 * @param authorName
 * @param price
 */
public Book(String title, String authorName, double price) {
    setTitle(title);
    setAuthorName(authorName);
    setPrice(price);
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthorName() {
    return authorName;
}

public void setAuthorName(String authorName) {
    this.authorName = authorName;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = Math.abs(price);
}

}

and a Booklist class which has a method append to add the books into the list but i am not able to figure it out how to enter a value into a list

public class BookList {

/**
 * books will be stored in an array of strings
 */

private Book[] books;
// *** TODO *** you will need to add more private members variables

public BookList(int N) {
    // TODO Auto-generated method stub  
}



public void append(Book book) throws BookListFull {
    // TODO Auto-generated method stub 
}

}

i want to add element at the end of the list kindly help me how to do it thanks

1
  • You wont be needing a BookList class.Just Use list given by java.Refer this example Commented Jul 27, 2012 at 11:13

6 Answers 6

1
public class BookList {
  List<Book> bookList= new ArrayList<Book>();

  ...
  ...
  public void append(Book book) throws BookListFull {
    bookList.add(book); 
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use a List<Book> instead of a Book array. Lists grow automatically when needed. See the collections tutorial for more information.

Also, fix the comments in the code:

/**
 * books will be stored in an array of strings <-- of strings?
 */
private Book[] books;

Comments

1

1. I will encourage you to use Collection framework of Java instead of Arrays.

2. Collection will give you lots of flexibility and methods, so you can handle you data in a more expressive way.

List<Book> list = new ArrayList<Book>;

Eg:

     List<Book> list = new ArrayList<Book>;

     list.add(new Books(title, authorName, price));

Comments

0

Try this:

public void append(Book book) {
    Book[] buf = this.books;
    this.books = new Book[this.books.length + 1];
    for(int i = 0; i < buf.length; i++)
        this.books[i] = buf[i];
    this.books[buf.length + 1] = book;
}

1 Comment

how to use these methods in BookList Class public Book getBook(int index) { // TODO Auto-generated method stub return null; } public boolean contains(String title) { // TODO Auto-generated method stub return true; } public void delete(int index) { // TODO Auto-generated method stub } public void grow(int i) { // TODO Auto-generated method stub }
0

if you cannot change the type of Book[] books to some reason (seems like you specify a maximum size in your constructor):

  public void append(Book book) throws BookListFull {

    for (int i = 0; i < this.books.length; i++) {
        if (null == this.books[i]) {
            this.books[i] = book;
            return;
        }
    }
    throw new BookListFull();

}

maybe you should try to modify the name of your Exception to BookListFullException or something - just my 2 cents.

Comments

0

If you can't use a List<Book> you have to copy the existing array in a new one which have books.length +1:

public void append(Book book) throws BookListFull {
    Book[] newArray = new Book[books.length + 1];
    for(int i = 0; i < books.length; i++){
        newArray[i] = books[i];
    }
    newArray[books.length] = book;
    books = newArray;
}

Or more easily, use List and its add() and remove() methods.

1 Comment

and how to remove particular element > like if i want to delete element at index 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.