首页 > 解决方案 > Java作业问题我一直没能解决

问题描述

问题:定义一个名为 Bookshelf 的类,它只包含一个 main() 方法。Bookshelf 类必须创建一打 (12) 个具有不同属性的 Book 对象,并且必须将它们存储在 Books 的 ArrayList 中。

然后,Bookshelf 类必须按照它们进入 ArrayList 的顺序列出 ArrayList 中所有书籍的所有属性。为 Bookshelf 创建一个排序函数,它将 ArrayList 中的书籍按名称升序排序,然后按出版年份排序。

提示:您需要定义一个比较器类,该类将两个 Book 对象作为 compareTo 方法的参数。此方法应进行两步比较并返回布尔值。第一次比较应该比较书名。如果名称相同,则第二次比较应比较出版年份。

将代码添加到 main() 以在第一个输出列表之后显示已排序的 Book 列表,即按照它们进入 ArrayList 的顺序显示 Books。

到目前为止,除了最后一部分,我已经完成了作业。我的代码如下所示:

public class Book implements Comparable<Book> {
  private String title; 
  private String isbnNumber;
  private String author;
  private String edition;
  private String publisher;
  private String yearPublished;

  public Book(String title, String isbnNumber, String author, String edition, String publisher, String yearPublished) {
    this.title = title;
    this.isbnNumber = isbnNumber;
    this.author = author;
    this.edition = edition;
    this.publisher = publisher;
    this.yearPublished = yearPublished;
  }

  public String getTitle() { constructor.
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getIsbnNumber() {
    return isbnNumber;
  }

  public void setIsbnNumber(String isbnNumber) {
    this.isbnNumber = isbnNumber;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public String getEdition() {
    return edition;
  }

  public void setEdition(String edition) {
    this.edition = edition;
  }

  public String getPublisher() {
    return publisher;
  }

  public void setPublisher(String publisher) {
    this.publisher = publisher;
  }

  public String getYearPublished() {
    return yearPublished;
  }

  public void setYearPublished(String yearPublished) {
    this.yearPublished = yearPublished;
  }

  public int compareTo(Book o1, Book o2){
    if(o1.getTitle().compareTo(o2.getTitle()) <0){
      System.out.println(o1.getTitle());
    } else if(o1.getTitle().compareTo(o2.getTitle()) > 0){
      System.out.println(o2.getTitle());
    } else if(o1.compareTo(o2) == 0){
      if(o1.getYearPublished().compareTo(o2.getYearPublished()) <0){
        System.out.println(o1.getTitle() + " " + o1.getYearPublished());
      } else {
        System.out.println(o2.getTitle() + " " + o2.getYearPublished());
      }

}}
}

public class BookShelf {
  public static void main(String[] args) {
      Book[] arrList = {
      new Book("My wife said you may want to marry me", "978-0-06-294059-9", "Jason B Rosenthal", "first",
              "HarperCollins", "2020"),
      new Book("The Archaic Revival", "978-0-06-250613-9", "Terrence McKenna", "first",
              "HarperCollins", "1991"),
      new Book("Thirteen", "978-1-4091-7076-9", "Steve Cavanagh", "first", "Orion", "2018"),
      new Book("The Gas We Pass", "978-1-929132-15-7", "Shinta Chow", "first", "Kane/Miller", "1994"),
      new Book("A Porcupine in a Pine Tree", "978-0-545-98663-2", "Helaine Becker", "first", "Scholastic", "2010"),
      new Book("Eddie's Monster", "0-89660-102-1", "Michael Abrams", "first", "Abbeville", "1996"),
      new Book("Harry Potter and the Philospher's Stone", "1-55192-398-x", "J.K. Rowling", "second", "Raincoast Books", "2000"),
      new Book("Purple, Green and Yellow", "1-55037-256-4", "Robert Munsch", "sixth", "Annick Press", "1992"),
      new Book("Franklin's Blanket", "1-55074-278-7", "Paulette Bourgeois", "first", "Kids Can Press", "1995"),
      new Book("Franklin Has a Sleepover", "1-55074-302-3", "Paulette Bourgeois", "first", "Kids Can Press", "1996"),
      new Book("Proud", "1-55285-274-1", "Fred Penner", "first", "Whitecap Books", "1997"),
      new Book("Goodnight, Little Hare", "978-1-84895-560-8", "Sheridan Cain", "second", "Little Tiger Press", "1999")};



      System.out.println("****BOOKSHELF****\n");
      
      for(int i=0;i<arrList.length;i++){

          System.out.println(arrList[i].getTitle() + ", " + arrList[i].getIsbnNumber() + ", " + arrList[i].getAuthor() + ", " + arrList[i].getEdition() + ", " 
          + arrList[i].getPublisher() + ", " + arrList[i].getYearPublished()+ '\n');

      for(int i = 0; i<arrList.length;i++){
        compareTo(arrList[i], arrList[i+1]);
      }
          

  }}
}

我不知道如何根据标题对数组进行排序,然后发布年份并将其打印出来。

标签: javacomparator

解决方案


以下是一些可能对您有所帮助的信息。

  • Comparable.compareTo()是对象列表自然排序的默认比较接口,并在类本身中实现,在您的情况下是Book类。

  • Comparator.compare()是一个比较接口,用于指定比较对象的不同方式,并作为排序例程的第二个参数提供。

两者都记录在 Java API 中,并且这个站点上有很多关于如何使用它们的示例。

请注意,您最好包含一些具有不同出版年份的重复标题,以便您可以compareTo根据您的作业充分展示您的方法。


推荐阅读