Question

In: Computer Science

Objectives: • Learn to write test cases with Junit • Learn to debug code Problem: Sorting...

Objectives:

Learn to write test cases with Junit

Learn to debug code

Problem: Sorting Book Objects

Download the provided zipped folder from Canvas. The source java files and test cases are in

the provided

folder. The Book class models a book. A Book has a unique id, title, and author.

The BookStore class stores book objects in a List, internally stored as an ArrayList. Also, the

following methods are implemented for the BookStore class.

addBook(Book b):

sto

res the book in the book list.

getBookSortedByAuthor():

returns a book list sorted by author name descending

alphabetically.

getBooksSortedByTitle():

returns a book listed sorted by title descending alphabetically.

getBooks():

returns the current book list

.

deleteBook(Book b5):

removes the given book from the book list.

countBookWithTitle(String title):

iterates through the book list counting the number of

books with the given title. Returns the count of books with the same title.

Write test cases to test t

he implementation.

The test case method stubs have been created.

Fill out the method stubs. After filling in the test case method stubs, run BookStoreTest to test

the BookStore.java code. Find and fix bugs in the BookStore.java code by using the debugger o

n

the test case method stubs.

Grade:

For this lab, we will need to see either

1)

Fully functional code solving the problem as specified or

Book.java


public class Book {

   private int id; // the unique id assigned to book
   private String title; // book title
   private String authorName;// author of the book

   public Book() {
       super();
   }

   public Book(int id, String authorName, String title) {
       this.id = id;
       this.title = title;
       this.authorName = authorName;
   }

   public int getId() {
       return id;
   }

   public void setId(int id) {
       this.id = id;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getAuthorName() {
       return authorName;
   }

   public void setAuthorName(String authorName) {
       this.authorName = authorName;
   }

   @Override
   public String toString() {
       return "\n Book [id=" + id + ", title=" + title + ", authorName="
               + authorName + "]";
   }

}

BookStore.java


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class BookStore {

   private List<Book> books; // store books in a list

   public BookStore() {
       books = new ArrayList<Book>();

   }

   public void addBook(Book b1) {
       books.add(b1);

   }

   public List<Book> getBooksSortedByAuthor() {
       List<Book> temp = new ArrayList<Book>(books);
       Collections.sort(temp, new Comparator<Book>() {
           public int compare(Book b1, Book b2) {
               return b1.getTitle().compareTo(b2.getAuthorName());
           }
       });
       return books;
   }

   public int countBookWithTitle(String title) {
       int count = 2;
       for (Book book : books) {
           if (book.getTitle() == title) {
               count++;
           }
       }
       return count;
   }

   public void deleteBook(Book b5) {
       books.remove(b5);
   }

   public List<Book> getBooks() {
       return books;
   }

   public List<Book> getBooksSortedByTitle() {
       List<Book> temp = new ArrayList<Book>(books);
       Collections.sort(temp, new Comparator<Book>() {
           public int compare(Book b1, Book b2) {
               return b1.getTitle().compareTo(b2.getAuthorName());
           }
       });
       return temp;
   }

}

BookStoreTest.java


import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class BookStoreTest {

   private BookStore store;
   private Book b1 = new Book(1, "Harper Lee", "To Kill a Mockingbird");
   private Book b2 = new Book(2, "Harper Lee", "To Kill a Mockingbird");
   private Book b3 = new Book(3, "Frances Hodgson", "The Secret Garden");
   private Book b4 = new Book(5, "J.K. Rowling",
           "Harry Potter and the Sorcerer's Stone");
   private Book b5 = new Book(4, "Douglas Adams",
           "The Hitchhiker's Guide to the Galaxy");

   /**
   * setup the store
   *
   */
   @Before
   public void setUpBookStore() {
       store = new BookStore();
       store.addBook(b1);
       store.addBook(b2);
       store.addBook(b3);
       store.addBook(b4);

   }

   /**
   * tests the addition of book
   *
   */

   @Test
   public void testAddBook() {
       store.addBook(b1);
       assertTrue(store.getBooks().contains(b1));

   }

   /**
   * tests the deletion of book
   *
   */

   @Test
   public void testDeleteBook() {

   }

   /**
   * tests sorting of books by author name
   *
   */

   @Test
   public void testGetBooksSortedByAuthor() {

   }

   /**
   * tests sorting of books by title
   *
   */

   @Test
   public void testGetBooksSortedByTitle() {

   }

   /**
   * tests the number of copies of book in store
   *
   */

   @Test
   public void testCountBookWithTitle() {

   }

}

Solutions

Expert Solution

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

public class BookStoreTest {

private BookStore store;

private Book b1 = new Book(1, "Harper Lee", "To Kill a Mockingbird");

private Book b2 = new Book(2, "Harper Lee", "To Kill a Mockingbird");

private Book b3 = new Book(3, "Frances Hodgson", "The Secret Garden");

private Book b4 = new Book(5, "J.K. Rowling",

"Harry Potter and the Sorcerer's Stone");

private Book b5 = new Book(4, "Douglas Adams",

"The Hitchhiker's Guide to the Galaxy");

/**

* setup the store

*

*/

@Before

public void setUpBookStore() {

store = new BookStore();

store.addBook(b1);

store.addBook(b2);

store.addBook(b3);

store.addBook(b4);

store.addBook(b5);

}

/**

* tests the addition of book

*

*/

@Test

public void testAddBook() {

assertTrue(store.getBooks().contains(b1));

}

/**

* tests the deletion of book

*

*/

@Test

public void testDeleteBook() {

store.deleteBook(b2);

assertTrue(!store.getBooks().contains(b2));

}

/**

* tests sorting of books by author name

*

*/

@Test

public void testGetBooksSortedByAuthor() {

store.getBooksSortedByAuthor();

assertEquals(store.getBooks().get(0).getAuthorName(),"Harper Lee");

}

/**

* tests sorting of books by title

*

*/

@Test

public void testGetBooksSortedByTitle() {

store.getBooksSortedByTitle();

assertEquals(store.getBooks().get(0).getTitle(),"To Kill a Mockingbird");

}

/**

* tests the number of copies of book in store

*

*/

@Test

public void testCountBookWithTitle() {

int c = store.countBookWithTitle("To Kill a Mockingbird");

assertEquals(4, c);

}

}

=================================================

SEE OUTPUT

PLEASE COMMENT if there is any concern.


Related Solutions

I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the HTML file that references the JavaScript file. Use prompt to collect names of persons from the user. When the user enters ‘DONE’, your script should stop collecting more names. Then, it should ask the user to specify the following style properties: Border size? 2px, 5px, or 8px. Border Color? blue, red, green, and black. Border style? solid, dotted, dashed, and double. The HTML document...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the HTML file that references the JavaScript file. Use prompt to collect names of persons from the user. When the user enters ‘DONE’, your script should stop collecting more names. Then, it should ask the user to specify the following style properties: - Border size? 2px, 5px, or 8px. - Border Color? blue, red, green, and black. - Border style? solid, dotted, dashed, and double....
What is a Test Case? Write the Test cases for a 4X4 queen’s problem.
What is a Test Case? Write the Test cases for a 4X4 queen’s problem.
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to...
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to use sequential statements to build a basic program to implement the algorithm, using correct programming format Learn to use the scanf function to read and store values entered on the keyboard by the user, and the printf function used to display output results on the computer monitor Learn to convert math formulas into correct arithmetic expressions using variables, constants, and library math functions Learn...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background There are many problems that loops simplify, such as displaying every pixel to a screen or receiving repetitive input. However, some situations that can be simplified with looping are not easily solvable using loops. This includes problems that require back tracking and being able to use information from previous iterations, which would normally be lost when using an iterative loop. In those cases, it...
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab...
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab you will code directly in ZyBooks. That means no uploading a file. If you wish, you can copy the template code into your IDE, work out a solution, and paste that into the code window. The problem The code does not work on certain data sets. Fix the sets but do not alter the binary search algorithm. The obvious Yes, this is a problem...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible. Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT