Skip Top Navigation Bar

AP Computer Science A Free Response Practice - 2D Array - Library Book

Mutual Connections

This single part question is worth 6 points.

AP CSA Alignment

Unit Alignment Learning Objectives Resources Skills
Unit 1: Using Objects and Methods 1.10.A Develop code to call class methods and determine the result of those calls. 2.C Write program code involving procedural abstractions.
Unit 2: Selection and Iteration 2.3.A Develop code to represent branching logical processes by using selection statements and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 4: Data Collections 4.12.A Develop code used to traverse the elements in a 2D array and determine the result of these traversals 2.B Write program code involving data abstractions.
4.13.A Develop code for standard and original algorithms for a particular context or specification that involves 2D arrays and determine the result of these algorithms.

Directions

The Book class is used to represent a book. Each book contains a title and a count for the number of times it has been checked out of a library. Each book title appears only one time in the library. A partial declaration of the Book class is shown.

public class Book {
    /**
     * Returns the title of the book.
     */
    public String getTitle() {
        /* implementation not shown */
    }

    /**
     * Returns the number of times this book has been checked out of the library.
     */
    public int getCheckoutCount() {
        /* implementation not shown */
    }

    // Other instance variables, constructors, and methods are not shown.
}

The Library class uses a 2D grid to model the shelves in a libary. Each element is a Book object or null for an empty slot. A partial declaration of the Library class is shown.

public class Library {
    private Book[][] shelves;

    /**
     * Returns an ArrayList of titles of books with a checkout count greater than the specified threshold.
     * Only non-null books are considered. Book titles appear at most once in shelves.
     *
     * @param threshold the minimum number of checkouts for inclusion
     * @return an ArrayList of book titles with a checkout count greater than threshold
     */
    public ArrayList<String> booksAboveThreshold(int threshold) {
        // You implement this method.
    }

     // Other instance variables, constructors, and methods are not shown.
}

Write the booksAboveThreshold method. The method should return an ArrayList of book titles where the number of times the book was checked out is greater than threshold. Each book title in shelves is unique.

Consider Library townLibrary has the following shelves object and recommendations is a properly declared ArrayList object.

The following are example calls to booksAboveThreshold and their results.

recommendations = townLibrary.booksAboveThreshold(10); would return the following values.


"Over Opal Cove" "Random Rainbows" "Threads of the Satin and Lace"

recommendations = townLibrary.booksAboveThreshold(0); would return the following values.


"Over Opal Cove" "Songs of the Canopy" "Random Rainbows" "Secret at Willow Gate" "Moonlight Among Swans" "Walking the Silver Waters" "Threads of the Satin and Lace" "When Lanterns Glow"

recommendations = townLibrary.booksAboveThreshold(30); would return an empty ArrayList.

Write Your Response

  • In order to test your program, we have provided some test cases for calls to booksAboveThreshold.
  • Write the code for the method booksAboveThreshold in the Java Playground as indicated by the comment add your code here.
  • All tests should return true if your code is correct.