英文:
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+", "+ 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(books.contains(title)){
found.add(book);
}
}
return found;
}
public ArrayList<Book> searchByPublisher (String publisher){
ArrayList<Book> found=new ArrayList<Book>();
for(Book book: books){
if(books.contains(publisher)){
found.add(book);
}
}
return found;
}
public ArrayList<Book> searchByYear(int year){
ArrayList<Book> found=new ArrayList<Book>();
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("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);
}
}
}
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<Book> 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 = "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
}
答案2
得分: 0
在您的方法 searchByTitle、searchByPublisher 和 searchByYear 中,您正在对 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<Book> searchByTitle(String title){
ArrayList<Book> found=new ArrayList<Book>();
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 ==
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论