【问题标题】:correct the Book class to get output of getBooksByAuthor method更正 Book 类以获取 getBooksByAuthor 方法的输出
【发布时间】:2015-02-10 21:29:22
【问题描述】:

我正在尝试获取 getBooksByAuthor 方法的输出,但它没有返回任何其他方法正在工作

我需要一个无需更改 getBooksByAuthor 方法且仅更改 Book 类的解决方案。

import java.util.*;

class Book  {
    // this class can modified if necessary, including the class definition
    private String title;
    private String author;
    private double price;

 public Book(String title, String author, double price) {
    this.title = title;
    this.author = author;
    this.price = price;
 }

 public String getTitle() {
    return title;
 }

 public String getAuthor() {
    return author;
 }

 public double getPrice() {
   return price;
 }

}

class Bookstore  {
  List<Book> books = new ArrayList<Book>();

  public Bookstore(List<Book> books) {
  this.books = books;
}

  public List<Book> getBooksSortedByPrice(int order) {
     if(order==0){
         for(double outer=0; outer < books.size()-1; outer++)
         {
          for(double inner=outer; inner < books.size(); inner++)
          {
           if(Integer.toString((int) books.get((int) outer).getPrice()).compareTo(Integer.toString((int)books.get((int) inner).getPrice()))>0)  {  
              Book temp = books.get((int) outer);
              books.set((int) outer, books.get((int) inner));
              books.set((int) inner, temp);
          }
        }
 }}        

        if(order==1){
         for(double outer=0; outer < books.size()-1; outer++)
     {
     for(double inner=outer; inner < books.size(); inner++)
    {
       if(Integer.toString((int) books.get((int) outer).getPrice()).compareTo(Integer.toString((int) books.get((int) inner).getPrice()))<0)  {  
      Book temp = books.get((int) outer);
      books.set((int) outer, books.get((int) inner));
      books.set((int) inner, temp);
   }

}
 }}        




   return books;
   }

 public List<Book> getBooksSortedByTitle(int order) {

     if(order==0){
         for(int outer=0; outer < books.size()-1; outer++)
         {
     for(int inner=outer; inner < books.size(); inner++)
        {
        if(books.get(outer).getTitle().compareTo(books.get(inner).getTitle())>0)  {  
         Book temp = books.get(outer);
         books.set(outer, books.get(inner));
         books.set(inner, temp);
       }

    }
 }}
    if(order==1){
     System.out.println("isu = ");
    for(int outer=0; outer < books.size()-1; outer++)
  {
    for(int inner=outer; inner < books.size(); inner++)
    {
   if(books.get(outer).getTitle().compareTo(books.get(inner).getTitle())<0)  {  
      Book temp = books.get(outer);
      books.set(outer, books.get(inner));
      books.set(inner, temp);
   }

}
 }}        

return books;
}

 public List<Book> getBooksByAuthor(String author) {
// this method returns the list of books for a given author
// do not make any changes to this method
// change the Book class to make sure this method will work correctly
List<Book> result = new ArrayList<Book>();
Book helperBook = new Book("Dummy", author, 0.0);
for (Book b : books) {
    if (b.equals(helperBook)) {
        result.add(b);
    }
}
return result;
}
 }

 public class Q2 {

/**
 * @param args the command line arguments
*/
public static void main(String[] args) {



    List<Book> book = new ArrayList<Book>();

   book.add(new Book("Java Programming", "Joyce Farrell", 881.0));
   book.add( new Book("Team Of Rivals", "Dorris Kearns Goodwin", 994));
   book.add (new Book("1776", "Daivd McCullough", 400));
   book.add (new Book("No Ordinary Time", "Dorris Kearns Goodwin", 768));
   book.add( new Book("Steve Jobs", "Walter Isaacson", 656));

  book.add( new Book("Dummy","Walter",0.0));
  book.add( new Book("Dummy","Walter",0.0)); 
  Bookstore df=new Bookstore(book);


  List<Book> booksSorted = df.getBooksByAuthor("Walter");
  for (Book number : booksSorted) {




  System.out.println( number.getPrice()+"--as--"+number.getTitle()+"----"+number.getAuthor());



  }



}

}

【问题讨论】:

    标签: java arrays list class methods


    【解决方案1】:

    您应该通过实现 IComparable 将实体 Book 添加为 Comparable。

    class Book implements IComparable {
    

    您还应该指定要比较的内容。 因此,将此方法添加到您的 Book 类中。

    public int compareTo(object obj) {
        Book objBook = (Book)obj;
        return this.author.equals(objBook.author) ? 0 : -1;
    }
    

    这不是 compareTo 类的一个很好的实现,但由于你老师的奇怪要求,它就足够了。

    【讨论】:

    • 我还要说这个请求有点奇怪:对于给定请求的 compareTo 方法的正确实现与通常理解一本书等于另一本书意味着什么是矛盾的。
    【解决方案2】:

    您需要为您的图书类实现/覆盖 equals 和 hashCode 方法以便进行比较

    if (b.equals(helperBook)) {
        result.add(b);
    }
    

    返回匹配的书籍。

    像这样覆盖等于

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
    
        Book book = (Book) o;
    
        if (Double.compare(book.price, price) != 0) return false;
        if (!author.equals(book.author)) return false;
        if (!title.equals(book.title)) return false;
    
        return true;
    }
    

    您可能需要也可能不需要覆盖哈希码

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = title.hashCode();
        result = 31 * result + author.hashCode();
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
    

    this link 解释了每个 java 对象上的 equals、hashCode 和其他默认方法的详细信息。

    【讨论】:

      【解决方案3】:

      你需要在Book类中添加equals方法。

      如果两个作者相同,它将返回 true。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-27
        • 2019-03-02
        • 2017-06-16
        • 2020-03-01
        • 2018-12-08
        相关资源
        最近更新 更多