Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions