0

I am suppose to create a Book class with the instance fields of title and number of pages.

I am then suppose to complete 14 tasks. I won't list them all but it includes adding books, then replacing a book then adding more.

 The Tasks: 
 1. Add (book 1 - 350 pages)
 2.Add (book 2 - 72 pages)
 3.Replace Book 2 with (book 3 - 220 pages)
 4.Add (book 4 - 120pages)
 5.Add (book 5 - 90 pages at index position 1)
 6.Add (Book 6 - 120 pages)
7. Add (book 7 - 170 pages)
8. Add(Book 8 - 400 pages)
9. Output the title of the book at index position 3
10. Output the size of the ArrayList. 
11. Remove all books that contain 120 pages.
12. Find total num of pages in remaining books in the ArrayList.
12. Output the total number of pages in the remaining books in the ArrayList.
14. Using and enhanced for loop, ouput the current books in the ArrayLIst.

EDIT: THANK YOU ALL SO MUCH! I have fixed, refined and almost finished my code. Only problem is the last part which I am suppose to use enhanced for loop to output the current books. I get weird numbers whenever I do. package Book;

import java.util.ArrayList;

public class Book {
    private String title;
    private int pages;

    public static void main(String[] args) {
        ArrayList<Book> booklib = new ArrayList<Book>();
        booklib.add(new Book("Book 1", 350));
        booklib.add(new Book("Book 2", 72));
        booklib.set(1, new Book("Book3", 220));
        booklib.add(new Book("Book 4", 120));
        booklib.add(1, new Book("Book 5", 90));
        booklib.add(new Book("Book 6", 120));
        booklib.add(new Book("Book 7", 170));
        booklib.add(new Book("Book 8", 400));

        System.out.println(booklib.get(3).gettitle());
        System.out.println(booklib.size());

        for (int i = 0; i < booklib.size(); i++)
            if (booklib.get(i).getpages() == 120)
                booklib.remove(i);

        int sum = 0;
        for (int i = 0; i < booklib.size(); i++) {
            sum = sum + booklib.get(i).getpages();
        }

        System.out.println(sum);
        for (Book books : booklib)
            System.out.println(books);
    }

    public Book(String n, int p) {
        title = n;
        pages = p;
    }

    public int getpages() {

        return pages;
    }

    public String gettitle() {

        return title;
    }

}
1
  • 1
    I would create a class Library since it's a collection of books. Commented Nov 27, 2013 at 3:18

2 Answers 2

2

From your code, there are some mistakes:

1 You declared an ArrayList named booklib, where Book objects are stored.

ArrayList<Book> booklib = new ArrayList<Book>();

You CANNOT add the books like this:

  booklib.add("Book 1", 350);
  booklib.add("Book 2", 72);

You SHOULD add the books using the following way

  booklib.add(new Book("Book 1", 350));
  booklib.add(new Book("Book 2", 72));

2 If you would like to print a book's title

You CAN NOT use:

  System.out.println(booklib.gettitle(3));

You SHOULD use the following way instaed.

  System.out.println(booklib.get(0).gettitle());//Populate the first book's title

3 Regarding the for-loop and its if-condition inside.

Change

 for(int i=0; i<booklib.size; i++)
    if(booklib[i] == 120)

to

 for(int i=0; i<booklib.size(); i++)
    if(booklib.get(i).getpages() == 120)

If remove elements from List, you'd better use iterator instaed.

Sign up to request clarification or add additional context in comments.

Comments

1

You should .add(new Book("Book 1", 350)); since the ArrayList has a Book type param.

ArrayList<Book> booklib = new ArrayList<Book>();
booklib.add(new Book("Book 1", 350));
booklib.add(new Book("Book 2", 72));

To get the size of an ArrayList use size() not size

for(int i=0; i<booklib.size; i++)

// instead use the code below

for(int i = 0; i < booklib.size(); i++)

Also in the same for loop, if you want the loop to break after a number is found just do this (you don't need the else, and also you need getPages()):

for(int i=0; i<booklib.size; i++)
    if(booklib[i].getPages() == 120) {
        booklib.remove(i);
        break;
    }
}

Also you should enclose the main method as follows

public static void main(String[] args)
{
    ArrayList<Book> booklib = new ArrayList<Book>();
    booklib.add(new Book("Book 1", 350));
    booklib.add(new Book("Book 2", 72));

    System.out.println(booklib.gettitle(3));
    System.out.println(booklib.size());

    for(int i=0; i<booklib.size; i++) {    // You should have brakets for loops that
        if(booklib[i] == 120)              // have multiple statements. This version
            booklib.remove(i);             // only has one, but your original code 
                                           // had multiple statements
    }
}

You current have a Book construct and get methods inside the main method. That's a no no

And last thing I noticed, gettitles() doesn't take an argument. You currently have this

System.out.println(booklib.gettitle(3));

// Should be something like this

System.out.println(booklib.get(0).gettitle());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.