CS162 Lab 4 Create a class named LibraryBook that contains fields to hold the title, author, and pagecount.  It should have set and get methods for each field. Write an application that instantiates five LibraryBook objects and prompts the user for values for the data fields.  The objects should be stored in either an Array or an ArrayList.  Then prompt the user to enter which field the libraryBooks should be sorted by – title, author, or pageCount.  Perform the requested sort procedure and display the LibraryBook objects in the sorted order. Our book has information about sorting an Array using the Array class sort method, which uses a comparator.  The ArrayList class also has the ability to use a comparator, though it is set up slightly differently, using the Collections.sort.  A quick google search got me these web sites, which seem pretty clear and use something similar to what we did with the Hand class in our Card Game project. Include full plans as usual.

Introduction:

The purpose of this assignment is to create a class named LibraryBook that holds information about a book’s title, author, and page count. User inputs will be requested to fill in the data fields for five LibraryBook objects. The objects will then be stored in either an Array or an ArrayList. The application will prompt the user to choose whether to sort the LibraryBooks by title, author, or page count. The selected sort procedure will be performed, and the LibraryBook objects will be displayed in the sorted order.

Objectives:

1. Create a class named LibraryBook with fields for title, author, and page count
2. Implement set and get methods for each field
3. Instantiate five LibraryBook objects and prompt the user to enter values for the data fields
4. Store the objects in an Array or an ArrayList
5. Prompt the user to choose a field for sorting the LibraryBooks – title, author, or page count
6. Perform the selected sort procedure using the appropriate method (Array.sort() or Collections.sort())
7. Display the LibraryBook objects in the sorted order

Implementation Steps:

1. Create the LibraryBook class with private fields for title, author, and page count.
2. Implement set and get methods for each field to allow access and modification of the data.
3. Instantiate an Array or an ArrayList in the main application to store the LibraryBook objects.
4. Prompt the user to enter values for the data fields of each LibraryBook object.
5. Repeat step 4 until all five objects have been initialized.
6. Display a menu to the user to choose a field for sorting the LibraryBooks.
7. Based on the user’s input, use the appropriate sorting method (Array.sort() or Collections.sort()) to sort the objects.
8. Display the sorted LibraryBook objects to the user.

Comparison between Array and ArrayList:

Both Array and ArrayList can be used to store objects, but they have some differences in terms of flexibility and functionality. Arrays have a fixed size and require the size to be declared at the time of initialization. Once the size is set, it cannot be changed. On the other hand, ArrayLists are dynamic in size and can grow or shrink as needed. ArrayLists also provide methods for adding, removing, and modifying elements, which makes them more flexible compared to Arrays.

In this assignment, both Array and ArrayList can be used to store the LibraryBook objects. If an Array is chosen, the size should be initialized to five. If an ArrayList is preferred, it can be initialized without specifying a size. Consider the specific requirements and limitations of each data structure when making a choice.

Sorting the LibraryBooks:

To sort the LibraryBook objects, the appropriate sorting method needs to be applied based on the field chosen by the user (title, author, or page count). The Array class provides a sort() method that can use a comparator to compare and sort the objects. If an ArrayList is used, the Collections class provides a sort() method that also accepts a comparator.

To implement the sorting process, a comparator class should be created to define the comparison logic based on the chosen field. The comparator will be passed to the sort() method to perform the sorting operation.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer