Ich habe ein Büchereiprojekt mit den Klassen Book und Category und den Interfaces IBook und ICategory. In Category scheint die rekursive Methode containsBook(IBook book) Probleme zu machen(Die Methode schaut ob ein entsprechendes Buch in der Category oder einer SubCategory enthalten ist.). Was ist bei dieser Methode falsch? Ich habe einen Zähler k, der vielleicht dafür verantwortlich ist, aber es könnte auch ein anderer Denkfehler sein.
Es gibt keine Compilerfehler!
[Java]
import java.util.*;
public class Category implements ICategory {
String name;
ArrayList<ICategory> subCat = new ArrayList<ICategory>();
ArrayList<IBook> subBook = new ArrayList<IBook>();
int k;
public Category(String name) {
this.name = name;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
@Override
public void addCategory(ICategory cat) {
// TODO Auto-generated method stub
subCat.add(cat);
}
@Override
public void addBook(IBook book) {
// TODO Auto-generated method stub
subBook.add(book);
}
@Override
public boolean containsBook(IBook book) {
// TODO Auto-generated method stub
int a = this.subBook.indexOf(book);
boolean m = false;
if (a == -1) {
if (k < subCat.size()-1)
m = this.subCat.get(++k).containsBook(book);
System.out.println(book);
}
else {
m = true;
}
return m;
}
}
[/code]
Fehlermeldung:The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
The title of this Book is Machine Learning and the name of the author is Tom Mitchel
The title of this Book is Herr der Ringe and the name of the author is J. R. R. Tolkien
The title of this Book is Der Hobbit and the name of the author is J. R. R. Tolkien
true
true
The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
Exception in thread "main" java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertTrue(Assert.java:54)
at TestBooks.testStore(TestBooks.java:56)
at TestBooks.main(TestBooks.java:9)
Es gibt keine Compilerfehler!
[Java]
import java.util.*;
public class Category implements ICategory {
String name;
ArrayList<ICategory> subCat = new ArrayList<ICategory>();
ArrayList<IBook> subBook = new ArrayList<IBook>();
int k;
public Category(String name) {
this.name = name;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
@Override
public void addCategory(ICategory cat) {
// TODO Auto-generated method stub
subCat.add(cat);
}
@Override
public void addBook(IBook book) {
// TODO Auto-generated method stub
subBook.add(book);
}
@Override
public boolean containsBook(IBook book) {
// TODO Auto-generated method stub
int a = this.subBook.indexOf(book);
boolean m = false;
if (a == -1) {
if (k < subCat.size()-1)
m = this.subCat.get(++k).containsBook(book);
System.out.println(book);
}
else {
m = true;
}
return m;
}
}
[/code]
Java:
public class Book implements IBook {
private String author;
private String title;
public Book(String author, String title){
this.title=title;
this.author=author;
}
@Override
public String getAuthor() {
// TODO Auto-generated method stub
return author;
}
@Override
public String getTitle() {
// TODO Auto-generated method stub
return title;
}
public String toString(){
return "The title of this Book is " + this.title +" and the name of the author is "+this.author;
}
public boolean equals(Object b){
if(!((b instanceof Book) && (b!=null))){
return false;
}else{
Book special=(Book) b;
return this.author.equals(special.author)&&this.title.equals(special.title);
}
}
}
Java:
import org.junit.Assert;
import org.junit.Test;
/**
* Testcase for the books and categories.
*/
public class TestBooks {
public static void main(String[] args) {
new TestBooks().testStore();
}
@Test
public void testStore() {
IBook book1 = (IBook) new Book("Cormen",
"Algorithmen - Die Einführung");
IBook book2 = (IBook) new Book("Cormen",
"Algorithmen - Die Einführung");
IBook book3 = new Book("Tom Mitchel", "Machine Learning");
IBook book4 = new Book("J. R. R. Tolkien", "Herr der Ringe");
IBook book5 = new Book("J. R. R. Tolkien", "Der Hobbit");
System.out.println(book1);
System.out.println(book2);
System.out.println(book3);
System.out.println(book4);
System.out.println(book5);
Assert.assertTrue(book1.equals(book1));
Assert.assertTrue(book1.equals(book2));
Assert.assertFalse(book1.equals(book3));
Assert.assertFalse(book1.equals(null));
ICategory gedruckteBuecher = new Category("gedruckte Bücher");
ICategory lehrbuecher = new Category("Lehrbücher");
ICategory fantasy = new Category("Fantasy");
ICategory informatik = new Category("Informatik");
gedruckteBuecher.addCategory(informatik);
gedruckteBuecher.addCategory(lehrbuecher);
gedruckteBuecher.addCategory(fantasy);
lehrbuecher.addCategory(informatik);
informatik.addBook(book1);
informatik.addBook(book2);
informatik.addBook(book3);
fantasy.addBook(book4);
fantasy.addBook(book5);
Assert.assertTrue(informatik.containsBook(book1));
System.out.println(informatik.containsBook(book1));
Assert.assertTrue(informatik.containsBook(book2));
Assert.assertTrue(informatik.containsBook(book3));
Assert.assertTrue(fantasy.containsBook(book4));
System.out.println(fantasy.containsBook(book4));
Assert.assertTrue(fantasy.containsBook(book5));
Assert.assertTrue(gedruckteBuecher.containsBook(book1));
Assert.assertTrue(gedruckteBuecher.containsBook(book2));
System.out.println(gedruckteBuecher.containsBook(book2));
Assert.assertTrue(gedruckteBuecher.containsBook(book4));
System.out.println(gedruckteBuecher.containsBook(book4));
Assert.assertTrue(gedruckteBuecher.containsBook(book5));
Assert.assertFalse(lehrbuecher.containsBook(book5));
Assert.assertFalse(lehrbuecher.containsBook(book4));
}
}
Fehlermeldung:The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
The title of this Book is Machine Learning and the name of the author is Tom Mitchel
The title of this Book is Herr der Ringe and the name of the author is J. R. R. Tolkien
The title of this Book is Der Hobbit and the name of the author is J. R. R. Tolkien
true
true
The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
The title of this Book is Algorithmen - Die Einführung and the name of the author is Cormen
Exception in thread "main" java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertTrue(Assert.java:54)
at TestBooks.testStore(TestBooks.java:56)
at TestBooks.main(TestBooks.java:9)
Zuletzt bearbeitet: