Question: Code in java, please. Sort the books array using bubble sort based on the book title and the last name of the author, respectively. The
Code in java, please.
Sort the books array using bubble sort based on the book title and the last name of the author, respectively. The two sorted arrays should be called booksByTitle and booksByName, respectively. Compare the time it took by these two operations
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
Get step-by-step solutions from verified subject matter experts
