MOOC JAVA第1部分 练习101(搜索功能)。未获得所期望的输出。

huangapple go评论80阅读模式
英文:

MOOC JAVA part 1 EXERCISE 101 (search functionality). Not getting the desired output

问题

以下是代码部分的翻译:

public class Book {
    private String title;
    private String publisher;
    private int year;

    public Book(String title, String publisher, int year){
        this.title = title;
        this.publisher = publisher;
        this.year = year;
    }

    public String title(){
        return this.title;
    }

    public String publisher(){
        return this.publisher;
    }

    public int year(){
        return this.year;
    }

    public String toString(){
        return this.title + ", " + this.publisher + ", " + this.year;
    }
}

import java.util.ArrayList;

public class Library {

    private ArrayList<Book> books;

    public Library(){
        this.books = new ArrayList<Book>();
    }

    public void addBook(Book newBook){
        this.books.add(newBook);
    }

    public void printBooks(){
        for(Book book: books){
            System.out.println(book);
        }
    }

    public ArrayList<Book> searchByTitle(String title){
        ArrayList<Book> found = new ArrayList<Book>();
        for(Book book: books){
            if(book.title().contains(title)){
                found.add(book);
            }
        }
        return found;
    }

    public ArrayList<Book> searchByPublisher(String publisher){
        ArrayList<Book> found = new ArrayList<Book>();
        for(Book book: books){
            if(book.publisher().contains(publisher)){
                found.add(book);
            }
        }
        return found;
    }

    public ArrayList<Book> searchByYear(int year){
        ArrayList<Book> found = new ArrayList<Book>();
        for(Book book: books){
            if(book.year() == year){
                found.add(book);
            }
        }
        return found;
    }
}

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        // 在这里测试你的程序

        Library library = new Library();

        library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007));
        library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992));
        library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952));
        library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));

        ArrayList<Book> result = library.searchByTitle("Cheese");
        for (Book book : result) {
            System.out.println(book);
        }

        System.out.println("---");
        for (Book book : library.searchByPublisher("Penguin Group")) {
            System.out.println(book);
        }

        System.out.println("---");
        for (Book book : library.searchByYear(1851)) {
            System.out.println(book);
        }
    }
}

你的搜索方法中有一个错误:你正在检查整个书籍列表是否包含某个字符串或整数,而不是检查单个书籍的属性。我已经在翻译后的代码中修复了这个错误,现在它会检查书的标题、出版商和年份是否包含你提供的搜索条件。

英文:

So the exercise is : "Add to the class Library the methods public ArrayList<Book> searchByTitle(String title), public ArrayList<Book> searchByPublisher(String publisher) and public ArrayList<Book> searchByYear(int year). The methods return the list of books that match the given title, publisher or year."

We have to use contains() method.
This is what I have done so far

public class Book {
private String title;
private String publisher;
private int year;
public Book(String title, String publisher, int year){
this.title=title;
this.publisher=publisher;
this.year=year;
}
public String title(){
return this.title;
}
public String publisher(){
return this.publisher;
}
public int year(){
return this.year;
}
public String toString(){
return this.title+&quot;, &quot;+ this.publisher+ &quot;, &quot;+this.year;
}
}
import java.util.ArrayList;
public class Library {
private ArrayList&lt;Book&gt; books;
public Library(){
this.books=new ArrayList&lt;Book&gt;();
}
public void addBook(Book newBook){
this.books.add(newBook);
}
public void printBooks(){
for(Book book: books){
System.out.println(book);
}
}
public ArrayList&lt;Book&gt; searchByTitle(String title){
ArrayList&lt;Book&gt; found=new ArrayList&lt;Book&gt;();
for(Book book: books){
if(books.contains(title)){
found.add(book);
}
}
return found;
}
public ArrayList&lt;Book&gt; searchByPublisher (String publisher){
ArrayList&lt;Book&gt; found=new ArrayList&lt;Book&gt;();
for(Book book: books){
if(books.contains(publisher)){
found.add(book);
}
}
return found;
}
public ArrayList&lt;Book&gt; searchByYear(int year){
ArrayList&lt;Book&gt; found=new ArrayList&lt;Book&gt;();
for(Book book: books){
if(books.contains(year)){
found.add(book);
}
}
return found;
}
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// test your program here
Library Library = new Library();
Library.addBook(new Book(&quot;Cheese Problems Solved&quot;, &quot;Woodhead Publishing&quot;, 2007));
Library.addBook(new Book(&quot;The Stinky Cheese Man and Other Fairly Stupid Tales&quot;, &quot;Penguin Group&quot;, 1992));
Library.addBook(new Book(&quot;NHL Hockey&quot;, &quot;Stanley Kupp&quot;, 1952));
Library.addBook(new Book(&quot;Battle Axes&quot;, &quot;Tom A. Hawk&quot;, 1851));
ArrayList&lt;Book&gt; result = Library.searchByTitle(&quot;Cheese&quot;);
for (Book book : result) {
System.out.println(book);
}
System.out.println(&quot;---&quot;);
for (Book book : Library.searchByPublisher(&quot;Penguin Group  &quot;)) {
System.out.println(book);
}
System.out.println(&quot;---&quot;);
for (Book book : Library.searchByYear(1851)) {
System.out.println(book);
}
}
}

Whats wrong with my search methods? The only thing it prints is "---". Any suggestions how to do this. I am a beginner in programming, and they want me to use ArrayList&lt;Book&gt; found in the search methods.

答案1

得分: 3

if(books.contains(year))if(books.contains(title))if(books.contains(publisher)) 永远不会成立,因为 books 是一个包含 Book 类实例的 ArrayList

你可能想要进行以下操作:

按年份搜索

if(book.year() == year) {
   found.add(book);
}

按出版商搜索

if(book.publisher().equals(publisher)) {
   found.add(book);
}

按标题搜索

if(book.title().equals(title)) {
   found.add(book);
}

在按标题搜索功能中,如果你不仅想要匹配的标题,还想要那些包含 title 作为子字符串的书籍标题,你还可以使用 .contains() 方法。

if(book.title().contains(title)) {
   found.add(book);
}

示例:

public static void main(String args[]) {
   String str = "In Search of Lost Tim";
  
   System.out.println(str.contains("In Search of Lost Tim"));   // true
   System.out.println(str.contains("Lost Tim"));                // true
   System.out.println(str.contains("In Search"));               // true
}
英文:

if(books.contains(year)), if(books.contains(title)) and if(books.contains(publisher)) will never be true because books is an ArrayList that contains instances of Book class.

You probably meant to do the following:

search by year

if(book.year() == year) {
found.add(book);
}

search by publisher

if(book.publisher().equals(publisher)) {
found.add(book);
}

search by title

if(book.title().equals(title)) {
found.add(book);
}

In search by title functionality, you could also use .contains() method if you not only want matching titles but also those book titles which contain title as a sub-string.

if(book.title().contains(title)) {
found.add(book);
}

Example:

public static void main(String args[]) {
String str = &quot;In Search of Lost Tim&quot;;
System.out.println(str.contains(&quot;In Search of Lost Tim&quot;));   // true
System.out.println(str.contains(&quot;Lost Tim&quot;));                // true
System.out.println(str.contains(&quot;In Search&quot;));               // true
}

答案2

得分: 0

在您的方法 searchByTitlesearchByPublishersearchByYear 中,您正在对 Books 列表进行字符串搜索,而不是您的 book 对象。应更改为

public ArrayList<Book> searchByTitle(String title){
    ArrayList<Book> found = new ArrayList<Book>();
    for(Book book : books){
        if(book.title().contains(title)){
            found.add(book);
        }
    }
    
    return found;
}

对于其他方法也是类似的。对于按年份搜索,您可以直接使用 == 进行比较。

英文:

In your methods searchByTitle, searchByPublisher and searchByYear, you are searching for the string in the Books list instead of your book object. It should be

public ArrayList&lt;Book&gt; searchByTitle(String title){
ArrayList&lt;Book&gt; found=new ArrayList&lt;Book&gt;();
for(Book book: books){
if(book.title().contains(title)){
found.add(book);
}
}
return found;
}

Similarly for the other methods.For searching by year, you can do a straight up ==

huangapple
  • 本文由 发表于 2020年7月24日 00:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63059342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定