Create a class named LibraryBook that contains fields to hold methods for settin
ID: 3783431 • Letter: C
Question
Create a class named LibraryBook that contains fields to hold methods for setting and getting a LibraryBook'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 the LibraryBooks should be sorted by (title, author, or page count). Perform the requested sort procedure, and display the LibraryBook objects. Save the file as LibraryBookSort. java.Explanation / Answer
// LibraryBook.java
import java.util.*;
public class LibraryBook
{
private String book_Title;
private String book_Author;
private int page_Count;
public LibraryBook()
{
book_Title = "";
book_Author = "";
page_Count = 0;
}
public LibraryBook(String bookTitle, String bookAuthor, int pageCount)
{
book_Title = bookTitle;
book_Author = bookAuthor;
page_Count = pageCount;
}
public String getBookTitle()
{
return book_Title;
}
public String getBookAuthor()
{
return book_Author;
}
public int getPageCount()
{
return page_Count;
}
public void setBookTitle(String bookTitle)
{
book_Title = bookTitle;
}
public void setBookAuthor(String bookAuthor)
{
book_Author = bookAuthor;
}
public void setPageCount(int pageCount)
{
page_Count = pageCount;
}
}
// LibraryBookSort.java
import java.util.*;
import java.util.Scanner;
public class LibraryBookSort
{
public static void sortByTitle(LibraryBook[] books)
{
LibraryBook t;
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(books[j].getBookTitle().compareTo(books[j+1].getBookTitle())>0)
{
t = books[j];
books[j] = books[j + 1];
books[j + 1] = t;
}
}
public static void sortByAuthor(LibraryBook[] books)
{
LibraryBook t;
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(books[j].getBookAuthor().compareTo(books[j+1].getBookAuthor())>0)
{
t = books[j];
books[j] = books[j + 1];
books[j + 1] = t;
}
}
public static void sortByPageCount(LibraryBook[] books)
{
LibraryBook t;
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(books[j].getPageCount() > books[j+1].getPageCount())
{
t = books[j];
books[j] = books[j + 1];
books[j + 1] = t;
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
LibraryBook[] librarybook = new LibraryBook[5];
for(int i = 0; i < 5; i++)
{
System.out.println(" Enter details of book " + (i+1));
System.out.print("Enter Title: ");
String title = sc.nextLine();
System.out.print("Enter Author: ");
String author = sc.nextLine();
System.out.print("Enter pageCount: ");
int pc = sc.nextInt();
sc.nextLine(); // Consume newline left-over
// librarybook[i].setBookTitle(title);
// librarybook[i].setBookAuthor(author);
// librarybook[i].setPageCount(pc);
librarybook[i] = new LibraryBook(title,author,pc);
}
System.out.print(" Sort Library Books by 1)Title 2)Author 3)Page Count Enter your choice: ");
int choice = sc.nextInt();
if(choice == 1)
sortByTitle(librarybook);
else if(choice == 2)
sortByAuthor(librarybook);
else if(choice == 3)
sortByPageCount(librarybook);
else
System.out.println("Invalid Input");
System.out.println(" Sorted Library books: ");
for(int i = 0; i < 5; i++)
{
System.out.print(librarybook[i].getBookTitle()+ " by ");
System.out.print(librarybook[i].getBookAuthor() + " with page count ");
System.out.println(librarybook[i].getPageCount());
}
System.out.println("");
}
}
/*
output:
Enter details of book 1
Enter Title: master of the game
Enter Author: Sidney Sheldon
Enter pageCount: 342
Enter details of book 2
Enter Title: Looking for Alsaka
Enter Author: John Green
Enter pageCount: 322
Enter details of book 3
Enter Title: Paper Towns
Enter Author: jonn alex
Enter pageCount: 456
Enter details of book 4
Enter Title: prisoner of bith
Enter Author: jeffery archer
Enter pageCount: 432
Enter details of book 5
Enter Title: Girl on train
Enter Author: gareth hopkins
Enter pageCount: 290
Sort Library Books by
1)Title
2)Author
3)Page Count
Enter your choice: 3
Sorted Library books:
Girl on train by gareth hopkins with page count 290
Looking for Alsaka by John Green with page count 322
master of the game by Sidney Sheldon with page count 342
prisoner of bith by jeffery archer with page count 432
Paper Towns by jonn alex with page count 456
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.