I am completing a homework problem for a Java course that has to do with instantiation, arrays and sorting of array data. Here is the question:
Create a class named
LibraryBook
that contains fields to hold methods for setting and getting aLibraryBook
's title, author, and page count. Save the file as LibraryBook.java.Write an application that instantiates five
LibraryBook
objects and prompts the user for values for the data fields. Then prompt the user to enter which field theLibraryBooks
should be sorted by - title, author, or page count. Perform the requested sort procedure and display theLibraryBook
objects. Save the file as LibraryBookSort.java.
The professor also added the following criteria in addition to the book:
Declare an array of
LibraryBook
objects and sort them either by title, author or page count, as the user requests.
Here is the code I have for LibraryBook.java:
public class LibraryBook
{
String bookTitle;
String bookAuthor;
int bookPageCount;
public LibraryBook(String title, String author, int count)
{
bookTitle = title;
bookAuthor = author;
bookPageCount = count;
}
public String getBookTitle()
{
return bookTitle;
}
public String getBookAuthor()
{
return bookAuthor;
}
public int getBookPageCount()
{
return bookPageCount;
}
}
Here is the code I have so far for LibraryBookSort.java:
import java.util.Arrays;
import java.util.Scanner;
public class LibraryBookSort
{
public static void main(String[] args)
{
LibraryBook[] book = new LibraryBook[5];
book[0] = new LibraryBook("Java Programming", "Joyce Farrell", 881);
book[1] = new LibraryBook("Team Of Rivals", "Dorris Kearns Goodwin", 994);
book[2] = new LibraryBook("1776", "Daivd McCullough", 400);
book[3] = new LibraryBook("No Ordinary Time", "Dorris Kearns Goodwin", 768);
book[4] = new LibraryBook("Steve Jobs", "Walter Isaacson", 656);
for (int x = 0; x < book.length; ++x)
book[x].getBookTitle();
for (int x = 0; x < book.length; ++x)
System.out.println(book[x].getBookTitle());
for (int x = 0; x < book.length; ++x)
book[x].getBookAuthor();
for (int x = 0; x < book.length; ++x)
System.out.println(book[x].getBookAuthor());
for (int x = 0; x < book.length; ++x)
book[x].getBookPageCount();
for (int x = 0; x < book.length; ++x)
System.out.println(book[x].getBookPageCount());
}
}
The code above seems to work and displays all of the data, although not formatted correctly. I want the data to look like the following:
Java Programming Joyce Farrel 881
Team Of Rivals Dorris Kearns Goodwin 994
1776 Daivd McCullough 400
No Ordinary Time Dorris Kearns Goodwin 768
Steve Jobs Walter Isaacson 656
In addition to the output being formatted like the above, I need to have each of the data types (Title, Author, Pages) sortable by the users selection.
At this point, I am just lost. This is above my skill level thus far. I am hoping that someone could give me some pointers/direction on where to go at this point.