so i need help on Modifying the source code to sort on the Title (in ascending order) and display the book title, comma space, author’s first name, comma space, author’s last name. Ensure all your variable names are appropriately named.

and i also need help Modifying the source code to display and sort on the Title in reverse

any help would be welcome

{
      // Declarations
      Book[] bkarray = new Book[4];
      String firstName, lastName, title;
      BufferedReader in=new BufferedReader(
                            new InputStreamReader(System.in));

      // Loop to get information on the 5 books
      for (int i=0; i<4; i++)
         {
            // Prompts user questions
            System.out.print("Please enter author #"+(i+1)+
                 "'s first name: ");
            firstName = in.readLine();
            System.out.print("Please enter author #"+(i+1)+
                 "'s last name: ");
            lastName = in.readLine();
            System.out.print("Please enter author #"+(i+1)+
                 "'s book title: ");
            title = in.readLine();
            bkarray[i] = new Book();
            bkarray[i].setFirstName(firstName);
            bkarray[i].setLastName(lastName);
            bkarray[i].setTitle(title);
         }

      // Loop to sort the books by Last Name
      for (int i=1; i<4; i++)
         {
            int j=i;
            String tempLN = bkarray[i].getTitle();
            Book tempBk = bkarray[i];
            while ((j>0) &&
             (bkarray[j-1].getTitle().compareTo(tempLN)>0))
            {
               bkarray[j] = bkarray[j-1];
               j--;
            }
         bkarray[j] = tempBk;
         }
      // Loop to Print results to the screen
      for (int i=0; i<4; i++)
         {
            System.out.print(bkarray[i].getLastName()+", ");
            System.out.print(bkarray[i].getFirstName()+", ");
            System.out.println(bkarray[i].getTitle());
         }
   }
}

Recommended Answers

All 2 Replies

The toString method does not get overridden enough, and is good for displaying your books. Just override your own version which displays the variables you want displayed. Then you can simply pass the object you want displayed directly to System.out.println(obj) and it will print in string form the object. Here is an example:

    //A snippet from a different implementation of a book class
    public String toString(){
        return super.toString() +
            "Author:  " + author + "\n";
    }//end method

There are built in methods for sorting arrays of objects that implement the Comparable interface. In order to use this interface you need to change the book class to implement this interface, and implement the method compareTo. Here is a link to the Comparable interface:
Click Here

import Java.Util.Arrays;
//sort regular order
Arrays.sort(array);
//sort reverse order
Arrays.sort(array, Collections.reverseOrder());

you need our help to modify the source code ...
who wrote the original source code ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.