ArrayList other classes

hi there, i am pretty new to Java, i have developed a book class pretty easily as you can see in the code below, but i have an activity to do in adding books to a class called library. the requirements are:

Create a Library class that is able to fulfill the following responsibilities:

It allows the storing of a collection of books

Use an ArrayList for the collection.

It allows Books to be added to the collection

It allows Books to be removed from the collection

It will list all the books in the collection

It will list all books with less than a specified number of pages

It will list all books written by a specified author

or print a message if there are no books by this author

i am able to add, remove, and view books, but i dont understand how to check for a specific number of pages, i did try it but i get errors similar to te errors that i get with the searchByAuthor part. the error is in "if(book.getAuthor().equals(author))" and it is "cannot find symbol - method getAuthor()". can someone please point me in the right direction, thank you for your help.

Libray class code:

import java.util.ArrayList;

import java.util.Iterator;

/**

* Write a description of class library here.

*

* @author (your name)

* @version (a version number or a date)

*/

public class Library

{

// instance variables - replace the example below with your own

private ArrayList<String> books;

//private String authorC;

private ArrayList<String> authors;

private int nextLotNumber;

/**

* Constructor for objects of class library

*/

public Library()

{

// initialise instance variables

books = new ArrayList<String>();

nextLotNumber = 1;

}

/**

* An example of a method - replace this comment with your own

*

* @param ya sample parameter for a method

* @returnthe sum of x and y

*/

public String searchByAuthor(String author)

{

for(String book : books)

{

if(book.getAuthor().equals(author))

{

return book;

}

}

return null;

}

public void enterBook(String book)

{

//books.add(newbook);

books.add(book);

// nextLotNumber++;

}

public void viewBooks()

{

for(String book : books)

System.out.println(book.toString());

System.out.println("****************");

}

public void deleteBook(String book)

{

books.remove(book);

if (books.size() == 0)

{

System.out.println("You have removed the last book, the library is now empty");

}

}

public int numberOfBooks()

{

return books.size();

}

}

Book class code:

/**

* This class defines details of the book "Java Fun".

*

* @author Jason

* @version 2007.13.10

*/

public class Book

{

private String author;

private String title;

private String reference;

private int pages;

private int refLength;

/**

* Constructor for objects of class book

*/

public Book(String bookTitle, String bookAuthor, String bookReference, int bookPages)

{

author = bookAuthor;

title = bookTitle;

reference = bookReference;

pages = bookPages;

}

//prints name of author to screen

public String getAuthor()

{

return author;

}

//prints name title of book to the screen

public String getTitle()

{

return title;

}

//prints reference to the screen

public String getReference()

{

return reference;

}

//prints number of pages to the screen

public int getPages()

{

//System.out.println(title + " contains: " + pages + " pages");

return pages;

}

//prints length of reference to the screen

public void getRefLength()

{

System.out.println("The length of reference is " + reference.length() + " characters");

}

/**

*changes reference string

*if newRef > 2 characters the new reference string is accepted and printed to the screen

*else the reference is not changed and an error message is printed to the screen

*/

public void changeRef(String newRef)

{

if (newRef.length() > 2){

reference = newRef;

System.out.println("The reference will be updated to: " + newRef);

}

else if(newRef.length() < 3){

//reference = reference;

System.out.println("error!!! " + newRef + " is incorrect Please enter a reference of atleast 3 characters");

System.out.println("The reference will be kept as: " + reference);

}

}

//the details of title, author, reference, pages and reference length are printed to the screen

public void showDetails()

{

System.out.println("***********************************************");

System.out.println(title + " is written by " + author);

System.out.println("It contains: " + pages + " pages ");

System.out.println("It has a reference of: " + reference);

System.out.println("Length of the reference is " + reference.length() + " characters ");

System.out.println("***********************************************");

}

}

Message was edited by:

zipdrive

Message was edited by:

zipdrive

[5583 byte] By [zipdrivea] at [2007-11-27 0:33:16]
# 1
Well, you defined "book" to be a String. Strings don't have a getAuthor() method. Maybe you should be handling Book references instead.
CeciNEstPasUnProgrammeura at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 2
http://forum.java.sun.com/help.jspa?sec=formatting
CeciNEstPasUnProgrammeura at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 3

Now you're storing String's in two Lists in your Library. Your requirement says "It allows Books to be added to the collection", so I'm pretty sure you should create a Book class which has a title and an author as attributes. Add those Book objects in a List in your Library class.

prometheuzza at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 4

thank you for your time. i changed my ArrayList to Book but now i get:

Book@4ee70b

****************

edit: Book@4ee70b displays instead of Book1

as a result from my:

public void viewBooks()

{

for(Book book : books)

System.out.println(book.toString());

System.out.println("****************");

}

regardless of if i have System.out.println(book.toString()); or System.out.println(book);

again, cheers for the help :)

Message was edited by:

zipdrive

zipdrivea at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 5

> thank you for your time. i changed my ArrayList to

> Book but now i get:

>

> Book@4ee70b

> ...

This is because you have not overridden the toString() method from the super class of Book (the java.lang.Object class).

Implement your own toString() method inside your Book class.

prometheuzza at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 6
In the Book class implement a methodpublic String toString() {return "some string identifying the book";}
pbrockway2a at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 7
thank you very much everybody, your assistance has allowed me to understand a whole lot more about the classes
zipdrivea at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 8

sorry, i have a couple of final questions, all my code seems to be working good except for 2 things.

1. the numPages prints out the information that I want as many times as there are books matching the criteria, i.e. if 2 seperate books have less than 10 pages it says:

[surfing, arts]

These books contain less than 10 pages

[surfing, arts]

These books contain less than 10 pages

2. the authorSearch prints outs the same type of result and it also prints null even if i get a positive result.

The books [surfing, arts] contains Jack

The books [surfing, arts] contains Jack

Here is the code, cheers:

import java.util.ArrayList;

import java.util.Iterator;

/**

* Write a description of class library here.

*

* @author (your name)

* @version (a version number or a date)

*/

public class Library

{

// instance variables - replace the example below with your own

private ArrayList<Book> books;

private int nextLotNumber;

/**

* Constructor for objects of class library

*/

public Library()

{

// initialise instance variables

books = new ArrayList<Book>();

nextLotNumber = 1;

}

/**

* An example of a method - replace this comment with your own

*

* @param ya sample parameter for a method

* @returnthe sum of x and y

*/

public String authorSearch(String author)

{

for(Book book : books)

{

if(book.getAuthor().equals(author))

{

System.out.println("The books " + books + " contains " + author);

}

else

{

}

}

return null;

}

public void numPages(int p)

{

for(Book bookp : books)

{

if(bookp.getPages() < p)

{

System.out.println(books);

System.out.println("These books contain less than " + p + " pages");

}

else

{

System.out.println("No books contain less than " + p + " pages");

}

}

}

public void enterBook(Book book)

{

//books.add(newbook);

books.add(book);

// nextLotNumber++;

}

public void viewBooks()

{

for(Book book : books)

System.out.println(book.toString());

System.out.println("****************");

}

public void deleteBook(String book)

{

books.remove(book);

if (books.size() == 0)

{

System.out.println("You have removed the last book, the library is now empty");

}

}

public int numberOfBooks()

{

return books.size();

}

}

zipdrivea at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 9

Could you post using "code" tags (as ceci asked before)? They are described here:

http://forum.java.sun.com/help.jspa?sec=formatting

The idea is that you put [code] at the start of your code and [/code] at the end.

That way your code remains readable when it appears in the forum.

For instance:

[code]if(tagsUsed) {

?outputGood = true;

}[/code]

will appear like:

if(tagsUsed) {

outputGood = true;

}

> 2. the authorSearch prints outs the same type of result and it also prints null even if

> i get a positive result.

> The books [surfing, arts] contains Jack

> The books [surfing, arts] contains Jack

Here's your code for this method /**

* An example of a method - replace this comment with your own

*

* @param y a sample parameter for a method

* @return the sum of x and y

*/

public String authorSearch(String author)

{

for(Book book : books)

{

if(book.getAuthor().equals(author))

{

System.out.println("The books " + books + " contains " + author);

}

else

{

}

}

return null;

}

We see a few things:

(1) You didn't change the comment! You may know what it's supposed to do, but

I'm not 100% percent sure. What follows, therefore, has a a bit of guesswork about

it. Writing comments is necessary to explain your method to others, but it is also

useful in sharpening your own idea of what the method does.

I am assuming this method "prints the books written by a given author."

(2) If my assumption is right, the name should change to showBooksBy() or

something that says clearly what the method does.

(3) Why does it return a String? If it's intended purpose is to display there

is no need for it to return anything.

(4) Linked with this is the fact that the method returns null. Why does it do this?

"Because it only works that way" is not a reason. "works" only has meaning in terms

of the method's purpose. Once the question of this method's purpose is settled - and

it should be settled before you write a line of code - then it will be clear that "return

null" is out of place.

(OK - you talk about "null" being printed. This must be happening in code that you

haven't posted. Since your methods use System.out.println() I'm going to stick with

the assumption that you intend them to print and not return a list for some other

method to print.)

(5) What is the purpose of the else block?

(6) Finally - and this is small beer compared with the other stuff - you search through

books looking to see if a book matches and then you print information about

books instead of about the matching book. Here is an ammended version of

the method (look at how books has been changed to book) /**

* Prints information about books written by a given author.

*

* @param author The author we are interested in

*/

public void authorSearch(String author)

{

for(Book book : books)

{

if(book.getAuthor().equals(author))

{

System.out.println("The book " + book + " contains " + author);

}

}

}

I'll leave the other method for you to have a crack at. Post (with tags) if you have a

question, but start by having a clear and clearly described purpose for the method.

(Also you might want to check revise the Nuts and Bolts:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html to remind

yourself what your options are.)

Finally I should point out that none of this is easy. For many of us it involves

changing, and adapting new mental habits: being specific and designing a

solution. Good luck.

pbrockway2a at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...
# 10
thank you very much, i am sorry, i didnt notice the bit about tags, stupid of me, but now i know and its great that you guys help like this. because of how stuff has been explained to me i was able to finish it off with more ease. thank you greatly jason
zipdrivea at 2007-7-11 22:40:02 > top of Java-index,Java Essentials,New To Java...