import java.util.*; // Class to help you test your question 4 public class TestQ4 { public static void main(String args[]) { System.out.println("===Testing class basics from example given in spec==="); System.out.println(); BookData book0 = new BookData("1984", "George Orwell"); book0.review(4.7); book0.review(5); book0.review(4.9); book0.review(4.9); BookData book1 = new BookData("Percy Jackson & the Olympians: The Lightning Thief", "Rick Riordan"); book1.review(4.9); BookData book2 = new BookData("No reviews", "No reviews"); checkEquals("book0.getRating()", book0.getRating(), 4.875); checkEquals("book1.getRating()", book1.getRating(), 4.9); checkEquals("book2.getRating()", book2.getRating(), 0.0); checkEquals("book0.toString()", book0.toString(), "1984, by George Orwell, 4.8 (4 reviews)"); checkEquals("book1.toString()", book1.toString(), "Percy Jackson & the Olympians: The Lightning Thief, by Rick Riordan, 4.9 (1 review)"); checkEquals("book2.toString()", book2.toString(), "No reviews, by No reviews, 0.0 (0 reviews)"); System.out.println(); System.out.println("=== Testing compareTo ==="); System.out.println(); BookData[] books = setupBooks(); Arrays.sort(books); checkEquals("Comparing sorted list", Arrays.toString(books), "[A, by Author, 4.0 (1 review), C, by Author, 1.3 (6 reviews), D, by Author, 1.3 (3 reviews), B, by Author, 0.0 (0 reviews)]"); } // Returns an array of movies with special values to help test sorting behavior public static BookData[] setupBooks() { BookData[] books = { new BookData("C", "Author"), new BookData("D", "Author"), new BookData("A", "Author"), new BookData("B", "Author") }; String[] allRatings = { "112112", "112", "4", "", "00044455", "00224455", "01", "00224455", "00144445" }; for (int i = 0; i < books.length; i++) { BookData book = books[i]; String ratings = allRatings[i]; for (int j = 0; j < ratings.length(); j++) { int rating = Character.getNumericValue(ratings.charAt(j)); book.review(rating); } } return books; } // Checks if the expcted value matches the value received. Prints out information // (including a description) explaining what was tested and if it succeded. public static void checkEquals(String description, E received, E expected) { checkEquals(description, received, expected, null); } // Checks if the expcted value matches the value received. Prints out information // (including a description) explaining what was tested and if it succeded. // Also prints a note in the case of test failure to indicate any special cases. public static void checkEquals(String description, E received, E expected, String notes) { System.out.print("Testing " + description + " ... "); if (expected.equals(received)) { System.out.println("Passed!"); } else { System.out.println("Does not match!"); System.out.println(" Received: " + received); System.out.println(" Expected: " + expected); if (notes != null) { System.out.println(); System.out.println(" Note: " + notes); } } } }