173k views
1 vote
Oscar has given you a list of books. She is tired of deciding what to read by author names and now wants to be able to pick them by their unique ISBN label. Organize this list of books for Oscar! The books will be given as an array of Book objects. Each book object contains the information of String title, String ISBN, and String array authors. A Hash Map will need to be declared that keys each book in the array based on the ISBN String of each book.

Example: Book object, titled "Once Upon an Algorithm", authored by Martin Erwid, and with ISBN = "9780262036634" keys to "9780262036634" Book object, titled "Java Software Structures", authored by John Lewis and Joseph Chase, and with ISBN = "9780133250121" keys to "9780133250121"
Let us assume that we already have the Book class defined. The main part involved in the question is as follows: class Book String title, ISBN; ArrayList authors; public String getISBNC; //return the ISBN label of a Book object Please write missing statements to make the following method build Map complete.
The buildMap method takes a book arrays as the parameter and returns a HashMap instance that consists of a collection of pairs of the key ISBN coupling up with its corresponding book object. public HashMapbuildMap(Book[] s) //TODO
Write the statements here

1 Answer

5 votes

Answer:

Check the explanation

Step-by-step explanation:

Since We Know That HASHMAP Is Another Set Of Class Available Under Java Library So For That You Need To Import The Library.

HASHMAP Contain Two Attribute -

Key And Value.

In Our Question Key-> ISBN

Value - Book

So the Remaining Function Is As -

public HashMap<String,Book> buildMap(Book[] s)

{

// declaration and instantation of a new hasmap

HashMap<String,Book> h = new HashMap<String,Book>();

//for each loop to traverse through the array of books.

for(int i =0;i<s.length;i++)

{

//using the function defined in book class to get ISBN.

String k = s[i].getISBN();

//put function is used top form a new key-value pair in the hashmap

h.put(k,s[i]);

}

// after the hashmap is made return it.

return h;

}

User Iqbal Khan
by
5.3k points