Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

follow instructions and make sure it runs in A2 tester, and add more tests in A2 tester. java public class A2Tester { private static int

follow instructions and make sure it runs in A2 tester, and add more tests in A2 tester.

java

public class A2Tester { private static int testPassCount = 0; private static int testCount = 0; public static void main(String[] args) { // Uncomment and test one method at a time. // Write additional tests as you see fit. /* Part 1: The Date class */ testBookConstructor(); // testEquals(); // testPercentageRead(); /* Part 2: The A2Exercises class */ // testAddBook(); // testNumberOfPages(); // testBooksWithRating(); // testNumOlderAuthors(); System.out.println("Passed " + testPassCount + " / " + testCount + " tests"); } public static void testBookConstructor() { System.out.println(" Testing Book Constructor"); Author a1 = new Author("Gillian", "Flynn", 49); Author a2 = new Author("Veronica", "Roth", 32); Book b1 = new Book("Gone Girl", a1, 5, 432); Book b2 = new Book("Divergent", a2, 4, 487); int result = 0; int expected = 0; result = b1.getTotalPages(); expected = 432; displayResults("b1 construcutor totalPages initialized", result==expected); result = b2.getRating(); expected = 4; displayResults("b1 construcutor rating initialized", result==expected); /* uncomment the following lines to test if the author was initialized correctly: */ // Author resultAuthor = null; // Author expectedAuthor = null; // resultAuthor = b1.getAuthor(); // expectedAuthor = a1; // displayResults("b1 construcutor author initialized", resultAuthor.equals(expectedAuthor)); // resultAuthor = b2.getAuthor(); // expectedAuthor = a2; // displayResults("b2 construcutor author initialized", resultAuthor.equals(expectedAuthor)); } public static void testEquals() { System.out.println(" Testing Book equals method"); Author a1 = new Author("Gillian", "Flynn", 49); Author a2 = new Author("Veronica", "Roth", 32); Book b1 = new Book("Gone Girl", a1, 5, 432); Book b2 = new Book("Divergent", a2, 4, 487); Book b3 = new Book("Allegiant", a2, 5, 526); Book b4 = new Book("Gone Girl", a1, 5, 456); Book b5 = new Book("Divergent", a2, 3, 487); boolean result = false; boolean expected = false; result = b1.equals(b2); displayResults("b1 equals b2", result==false); result = b1.equals(b3); displayResults("b1 equals b3", result==false); result = b2.equals(b3); displayResults("b2 equals b3", result==false); result = b1.equals(b4); displayResults("b1 equals b4", result==true); result = b2.equals(b5); displayResults("b2 equals d5", result==true); result = b4.equals(b5); displayResults("b4 equals b5", result==false); } public static void testPercentageRead() { System.out.println(" Testing Book percentageRead method"); Author a1 = new Author("Gillian", "Flynn", 49); Author a2 = new Author("Veronica", "Roth", 32); Book b1 = new Book("Gone Girl", a1, 5, 432); Book b2 = new Book("Divergent", a2, 4, 487); double result = 0.0; double expected = 0.0; result = b1.percentageRead(164); expected = 37.96; displayResults("b1.percentageRead(164)", Math.abs(expected-result) 

image text in transcribed

image text in transcribed

image text in transcribed

public class A2Exercises { Purpose: Add b to the given array of books Parameters: Book[] array - the array to add the book to Book b - the book to add to the array Returns: Book [] - a new array containing all of the books found in arr plus book b public static Book[] addBook (Book[] array, Book b) { 7/ Fix me! Book[] result = null; return result; // so it compiles } /* Purpose: get the sum of all the page counts from all of the books in the array Parameters: Book[] array Returns: int - the total number of pages found in all books in the array */ public static int numberOfPages(Book [] array) { // Fix me! int result = 0; return result; // so it compiles } Purpose: get the number of books in the array with a rating equal to the given rating Parameters: Book [] arr - the array of books int rating - the rating to search for * Returns: int - the number of books with the given rating */ public static int booksWithRating(Book [] arr, int rating) // Fix me! int result = 0; return result; // so it compiles ) + / Purpose: get the number of books in the array written by an author who is older than the given author * Parameters: Book [] arr - the array of books Author auth - the author to compare with * Returns: int - the number of books written by an older author */ public static int numolderAuthors(Book [] arr, Author auth) { // Fix me! int result = 0; return result; // so it compiles ) public class Author { private String firstName; private String lastName; private int age; public Author(String firstName, String lastName) { this.firstName firstName; this.lastName = lastName; this.age = @ } public Author(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public int getAge() { return this.age; } public boolean equals(Author other) { return this.firstName.equals(other.getFirstName() && this.lastName.equals(other.getLastName() && this.age other.getAge(); } BE public boolean olderThan (Author other) { return this. age > other.getAge(); } public class Book { private String title; private Author author; private int rating; private int totalPages; public Book(String title, Author author, int rating, int totalPages) { this.title = "unknown"; this.author = null; this.rating = 0; this.totalPages = 0; ) public Author getAuthor() { return author; } public int getTotalPages() { return totalPages; } public int getRating() { return rating; Purpose: determine if the current book is equal to other Parameters: Book other - the other book to compare to Returns: boolean - true if the two books have the same title and author (the same book might have a different number of pages depending on whether the copy is hard-cover or paperback, and might receive different ratings from different reviewers). public boolean equals(Book other) { return false; // so it compiles 1 Purpose: determine the percentage the book has been read Parameters: int pagesRead - the number of pages read in this book Returns: double - the percentage of the way the reader has progressed through the book. */ public double percentageRead(int pages Read) { return e.e; // so it compiles

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

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions

Question

What is an interface? What keyword is used to define one?

Answered: 1 week ago