I'm finding it hard to use my methods even if I correctly instantiated my objects. Any ideas on where I went wrong?
example: I tried compiling the java file but the error I get is
"incompatible types: String cannot be converted to Books"
I think the problem is my instantiated object is being forced into a string but the problem is I used the correct syntax to call on a string. However, it still doesn't read it as a string and says that the instantiated object cannot be converted to the "Books" class.
I've already searched about it but all they say is the object hasn't been created. However, I checked my code and I already instantiated my object even before putting it into the method parameter.
I even tried to print the object on its own with the specific characteristics and it turned out fine. So I guess it goes right up until it gets put into a method.
One thing I don't understand is that I need that object to be referenced into a method.
Here is my code:
class Books{
String type;
int pages;
Books[] booklist;
int bookcounter = 0;
//Constructor to initialize the object "book"
Books(int input){
if(input == 1){
this.type = "Math";
this.pages = 5;
}
if(input == 2){
this.type = "Physics";
this.pages = 9;
}
if(input == 3){
this.type = "Economics";
this.pages = 20;
}
}
//This method needs to add the instantiated object to the array list
void addbooktype(Books kind){
System.out.println("You chose: " + kind);
System.out.println("Adding to the list...");
booklist[bookcounter++] = kind;
}
void printbooks(){
for(int i = 0; i <= bookcounter; i++){
int y = i+1;
System.out.println("Book #"+ y + "is: " +this.booklist[i].type);
System.out.println("With pages of: " + this.booklist[i].pages);
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int bookchoice;
int choice;
String booktype;
int booknum = 0;
do{
System.out.println("===========Menu===========");
System.out.println("[1] - Pick a book \n[2] - Print book list\n[0] - Exit");
System.out.println("==========================");
System.out.print("Choice: ");
choice = sc.nextInt();
switch(choice){
//Selects and adds a book to the list
case 1:
System.out.println("Choose your book: ");
bookchoice = sc.nextInt();
Books book = new Books(bookchoice);
System.out.println(book.type);
booktype = book.type;
book.addbooktype(booktype);
booknum++;
break;
//Prints the book list
case 2:
System.out.println("List of Books: ");
book.printbooks();
break;
case 0:
System.out.println("Exit");
return;
default: System.out.println("Input not found.");
}
}while(choice!=0);
}
}
The errors I get is on the "book.addbooktype(booktype);"
This is where it bugs me, I printed the objected and even put it into a String container but it still rejects it. I don't know where I went wrong. And when it goes into the method it doesn't read the parameter. Any thoughts?
The issue is that your method accepts Object of Class Book only. However when you are calling that function
book.addbooktype(booktype);
You are giving it type String (In your book class type is String variable). To fix that you either need to pass book object or change the method itself
Passing the book object:
Books book = new Books(bookchoice);
book.addbooktype(book);
and in function you can do something like this
void addbooktype(Books book) {
System.out.println("You chose: " + book.type);
System.out.println("Adding to the list...");
booklist[bookcounter++] = book;
}
(Added Later) Using This:
This approach also utilizes the object however it is better than the approach described above. Instead of passing the object as a parameter which is redundant you can use java word this
.
According to java docs Using the this Keyword
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
So when you call function
book.addbooktype(book);
^ and ^ are same
| |
Inside the method addbooktype
void addbooktype(Books book) {
this and book would also be same.
}
so instead you could do this
book.addbooktype();
and addbooktype
would be
void addbooktype() {
System.out.println("You chose: " + this.type);
System.out.println("Adding to the list...");
booklist[bookcounter++] = this;
}
An example where passing object and this would be useful is when you have to compare objects. Lets say you have two objects book1 and book2. You would do something like this
int compareResult = book1.compareTo(book2);
Your method would be
public int compareTo(Book book) {
Now here this would be book1
and book would be book2
Also keep in mind this.type would be same as type. type and this.type are both
referring to same object i.e who we called function on (book1).
}
Changing the function:
or you can just change you function like this though I suggest you pass the object itself. If you just pass the String you won't be able to store it in Books[] booklist
void addbooktype(String type) {
System.out.println("You chose: " + type);
System.out.println("Adding to the list...");
// The line below will give error unless you change Books[] booklist;
// to String[] booklist;
booklist[bookcounter++] = kind;
}