Reputation: 313
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.
Upvotes: 3
Views: 3673
Reputation: 155
Ok, for the sort, you would use code similer to this:
import java.util.Arrays;
import java.util.Scanner;
public class LibraryBookSort
{
LibraryBook[] book = new LibraryBook[5];
public static void main(String[] args)
{
LibraryBookSort run = new LibraryBookSort();
}
public LibraryBookSort()
{
readData();
displayArr();
System.out.println("-------------Sorted------------------");
sortArr();
displayArr();
}
private void readData()
{
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);
}
private void sortArr()
{
//Sort
for(int outer=0; outer < book.length-1; outer++)
{
for(int inner=outer; inner < book.length; inner++)
{
if(book[outer].getBookTitle().compareTo(book[inner].getBookTitle())>0) {
LibraryBook temp = book[outer];
book[outer] = book[inner];
book[inner] = temp;
}
}
}
}
private void displayArr()
{
for (int x = 0; x < book.length; ++x){
System.out.println(book[x].getBookTitle() + "\t" + book[x].getBookAuthor() + "\t" + book[x].getBookPageCount());
}
}
}
OK, as you can see the LibraryBookSort() and LibraryBookSort() are self explanatory, now the sortArr() is the trickey part: Firstly, the two loops, what it does it takes item 1, and compare it to all the items, then item 2, and the same story. We start the inner loop at the outer position in order to avoid useless clutter as the items before that were already tested. Now, using the getters and setters, we get the name or anything you want from the array and compare it to the other one, using > 0 will be A-Z, and <0 Z-A. Should the item in the innerLoop be "higher up" as the outerLoop item, then we have to move it. Create a new variable from your getSetClass and store the outerLoopItem (One now not in the right place anymore) in it. Then we put the innerItem (Because it is now "before" the outerItem) in the outerItem's pos. Then we just fill the pos of the Inner with the Temp.
Basically what we have done is we just switched the two variables around but we need a temp storage for that, else we will lose some data.
Hope it helps now even more, ask should you not understand.
Upvotes: -2
Reputation: 14053
Regarding the formatting, you just need one single for
loop that will display all the details like this:
for (int x = 0; x < book.length; ++x){
System.out.println(book[x].getBookTitle() + "\t" + book[x].getBookAuthor() + "\t" + book[x].getBookPageCount());
}
This way you are getting in one time all the necessary information about each book, outputted in one single sentence each time, with values separated by tabs (\t
).
Half the loops you are doing right now are useless, like this one:
for (int x = 0; x < book.length; ++x)
book[x].getBookTitle();
you are not getting the title in any variable, so you are just executing a loop on the whole array for nothing.
For the sorting option, you could either look into the sort
method of Array if you are allowed to use it, or try to implement a sorting method by yourself which would probably benefit to your knowledge on arrays and iteration over arrays.
Upvotes: 6
Reputation: 93030
To sort you can use Arrays.sort
method. You can pass a custom Comparator<LibraryBook>
to it which sorts by the corresponding field.
To display these correctly you only need to loop once over the array and just print the fields in order. You can use String.format
method for a pretty formatting.
Here is the code:
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);
printBooks(book);
Arrays.sort(book, new Comparator<LibraryBook>() {
@Override
public int compare(LibraryBook arg0, LibraryBook arg1) {
return arg0.getBookTitle().compareTo(arg1.getBookTitle());
}
});
printBooks(book);
Arrays.sort(book, new Comparator<LibraryBook>() {
@Override
public int compare(LibraryBook arg0, LibraryBook arg1) {
return arg0.getBookAuthor().compareTo(arg1.getBookAuthor());
}
});
printBooks(book);
Arrays.sort(book, new Comparator<LibraryBook>() {
@Override
public int compare(LibraryBook arg0, LibraryBook arg1) {
return arg0.getBookPageCount() - arg1.getBookPageCount();
}
});
printBooks(book);
}
private static void printBooks(LibraryBook[] book){
for (int x = 0; x < book.length; ++x) {
String title = book[x].getBookTitle();
String author = book[x].getBookAuthor();
int count = book[x].getBookPageCount();
System.out.println(String.format("%30s%30s%10s", title, author, count));
}
System.out.println();
}
}
Upvotes: 0
Reputation: 18492
Those loops are yuck, and three (half) of them aren't doing anything at all. They also won't produce the desired output, as you mentioned. Instead, use a single foreach
loop and use printf
with it's format strings for the formatting:
for (LibraryBook b : book)
System.out.printf(..., b.getBookTitle(), ...);
I'll let you finish that off, since it's homework.
In regards to sorting, you can use Array.sort(Object[] a, Comparator c)
, where you would have a Comparator
for each of Title, Author and Pages, that would be used to sort the array appropriately.
Upvotes: 0