need help with object-oriented (OO) program

need help with object-oriented (OO) homework

Page 1 of 1

6 Replies - 1612 Views - Last Post: 22 April 2010 - 03:21 PM Rate Topic: -----

#1 ksaihat  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 11-March 10

need help with object-oriented (OO) program

Posted 21 April 2010 - 09:04 AM

Hi
i need help with my homework which is about writing object-oriented code
i have to design a simple library system , i have to write 3 classes
Book class, catalogue class and library class which is a driver class
Book class contain 1 method printdetail()This method summarizes (returns but does notprint) the title, author, ISBN number and year
published in a reader friendly manner.

catalogue class contain 3 methods
1-addBook() Adds new books to the catalogue array; you can
hardcode these books, in other words you donít have to ask for a user input
2- getBook() Retrieves a book in the catalogue given its index
(index of array)
3- printCatalogue() It calls the printDetails() method to print the details
on the console

library classcontain 2 methods
1-fillData() This method must be present inside the driver class
and should be called by the main method. This
method:
1. Creates a catalogue using the addBook()
method
2. Prints the details of the book using the
printCatalogue() method

2- searchBook() This method must ask the user for input on the
command line. The user enters a book author and
the method searches for the books in the Book
array. If there are no matching books, a message is
displayed stating that no matching books have
been found and the user is asked again to enter an
author. Any matching books, if found, are listed
with all its details. You may want to make use of
the printDetails() method to prepare for printing
these details.
________________________________
i did all the methods except SearchBook()i did not know how to do it
public Book SearchBook(String input){
		Scanner reader=new Scanner(System.in);
        Book books=new Book();
		
        for(int i=0; i< books.size(); i++)     {
        	Book book=books.get(i);
          

          if(book.getAuthor().equals(input))

                        {

         return book;
                        }
         
          else

        return null; //Book not in library




        
		}

can you help me please
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: need help with object-oriented (OO) program

#2 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: need help with object-oriented (OO) program

Posted 21 April 2010 - 02:06 PM

It looks like you are on the right track. You won't need a scanner, as the search string is read somewhere lese, and passed as an argument. You also don't need the local variable "books", but instead you should use the catalog variable of the library class. Don't use an else statement inside the for loop, because you need to try all the books in the library before you conclude that there is no match. A minor point, the method to get a book is supposed to be getBook(), not get().

Btw, you may be able to get more help if you post the rest of your code.

public Book SearchBook(String input){
        for(int i=0; i< books.size(); i++)     {
        	Book book=books.getBook(i);
          

          if(book.getAuthor().equals(input))

                        {

         return book;
                        }
         } // end for

        return null; //Book not in library




        
		}

Was This Post Helpful? 0
  • +
  • -

#3 ksaihat  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 11-March 10

Re: need help with object-oriented (OO) program

Posted 22 April 2010 - 12:52 AM

Hi
i did all this but the program dose not run, it says there are errors in



for(int i=0; i< books.size(); i++)
(the method size() is undefined for the type Book)

Book book=books.getBook(i);
(the method getBook(int) is undefined for the type Book)

i don't know what is the problem?

also
i the program find the author how can t get the information of his book?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Skick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Re: need help with object-oriented (OO) program

Posted 22 April 2010 - 02:51 AM

The problem is you are trying to iterate through an object. If you are meaning to have a bunch of books, you need to have an object for each Book, and a list to store all the books into. You can't do this:

Book books=new Book();
    for(int i=0; i< books.size(); i++) {
        Book book=books.get(i);

    ...
}



You need to do this instead:

List<Book> books = new ArrayList<Book>();

for (Iterator i = books.iterator(); i.hasNext();)/> {
    Book book = (Book)i.next();

    ...
    
}



I hope this helps.

This post has been edited by Skick: 22 April 2010 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,346
  • Joined: 27-December 08

Re: need help with object-oriented (OO) program

Posted 22 April 2010 - 12:49 PM

@ksaihat: Please, :code:
As for your problem, Lists (including ArrayList) have a get(index) method, but not a getBook(index) method.

@Skick: Lists actually return a Generic Iterator object, so you can have an Iterator<Book>, which will return references to Book objects when you invoke the next() method, so there is no need to cast (so long as you use Generics).
Was This Post Helpful? 0
  • +
  • -

#6 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: need help with object-oriented (OO) program

Posted 22 April 2010 - 02:18 PM

View Postmacosxnerd101, on 22 April 2010 - 11:49 AM, said:

@ksaihat: Please, :code:
As for your problem, Lists (including ArrayList) have a get(index) method, but not a getBook(index) method.

@Skick: Lists actually return a Generic Iterator object, so you can have an Iterator<Book>, which will return references to Book objects when you invoke the next() method, so there is no need to cast (so long as you use Generics).


The homework requires the catalog class to implement a getBook() method, which will then be called from searchBook() of the library class. The above details about handling lists would be used within the catalog class, but would be hidden in the searchBook() method.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,346
  • Joined: 27-December 08

Re: need help with object-oriented (OO) program

Posted 22 April 2010 - 03:21 PM

I completely missed that part about implementing this method via the Library class. Thanks for pointing it out!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1