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
List
List
List
Random rand = new Random();
List
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
List
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started