Question: Code in java, please Use the Java API Arrays.sort() method to sort the books array. Sort the array by book titles and authors last names,

Code in java, please

Use the Java API Arrays.sort() method to sort the books array. Sort the array by book titles and authors last names, respectively. Compare the time it took between the two operations Here is what I have (Main.java and Book.java):

Here is what I have (Main.java and Book.java):

Main.java

package problem1;

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

public class Main {

public static void main(String[] args) throws Exception {

List titles = readFile("textbook_titles.txt");

List isbns = readFile("textbook_isbns.txt");

List firstNames = readFile("First Names.txt");

List lastNames = readFile("Last Names.txt");

Random rand = new Random();

List books = new ArrayList<>();

for (int i = 0; i < titles.size(); i++) {

String title = titles.get(i);

String isbn = isbns.get(i);

String firstName = firstNames.get(rand.nextInt(firstNames.size()));

String lastName = lastNames.get(rand.nextInt(lastNames.size()));

double price = rand.nextDouble() * 99.99;

books.add(new Book(title, isbn, firstName, lastName, price));

}

for (Book book : books) {

System.out.println(book);

}

}

private static List readFile(String fileName) throws Exception {

List lines = new ArrayList<>();

BufferedReader reader = new BufferedReader(new FileReader(fileName));

String line;

while ((line = reader.readLine()) != null) {

lines.add(line);

}

reader.close();

return lines;

}

}

Book.java

package problem1;

public class Book {

private String title;

private String isbn;

private String firstName;

private String lastName;

private double price;

public Book(String title, String isbn, String firstname, String lastname, double price) {

super();

this.title = title;

this.isbn = isbn;

this.firstName = firstname;

this.lastName = lastname;

this.price = price;

}

@Override

public String toString() {

return "Title: " + title + ", ISBN: " + isbn + ", Author: " + firstName + " " + lastName + ", Price: $" + price;

}

public int compareTo(Book o) {

return Double.compare(this.price, o.price);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!