0

My overall goal at the moment is to call a method that is in a different class, there is no inheritance between the classes.

After researching i found the best way to do this was to initialise an instance of the object in the method I am trying to call it in. Is there a standard way to do this?

Any help will be greatly appreciated.

  • Your best bet right now would be to read a tutorial, or the course book. You seem to be struggling with some basic concepts here, that _must_ be understood before starting to code. – Óscar López Oct 21 '13 at 16:45

2 Answers2

1

i get the error "Constructor Book in class Book cannot be applied to given types

Because you did'nt specified a no-args constructor in your Book class


I assume that you want to check if the book already exists

won't compile since this method (i.e the method checkForDuplicate()) doesn't return a boolean in all the cases (when b.equals() is false).

So you should do

if(b.equals() == true){
  return true;
}else {
  return false;
}

which is equivalent to return b.equals();

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

You have to pass the correct parameter values, not declare them again. Like this:

Book b = new Book(title, author, publisher, year);

Of course, you have to know the values for title, author, publisher, year before calling the constructor. Also, the method is expecting a boolean return value, so you must have a return true; or return false; at the end, depending on what the method is expected to do.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • @user2904018 after you've correctly called the constructor, you can call methods of the class `Book` on the object `b`, for instance: `b.getTitle()`. But first (and there's no escaping this!) you have to pass the right parameters to the constructor. – Óscar López Oct 21 '13 at 16:44
  • Looks like checkForDuplicate() needs some parameters too, like title, author, publisher, year. – Big Al Oct 21 '13 at 16:47
  • @user2904018 of course, because you have not defined it yet - it's in my answer: **you have to know the values**. Or simply pass the values directly: `new Book("The Hobbit", "J.R.R. Tolkien", "George Allen & Unwin", 1937)`. – Óscar López Oct 21 '13 at 16:48
  • @user2904018 I'm sorry, but you have to take a look at a tutorial first, before starting to code. – Óscar López Oct 21 '13 at 16:48