If you worked in a pair, be sure to have both your full names and student numbers on everything. Both of you will need to submit (the same copy) on Blackboard Connect, even though one copy will be graded and you will both get the same mark.
Type up your answers using a text editor as instructed in the labs, or use Eclipse if you are already comfortable with it. The file format of the Java program you are to submit must be .java.
This assignment is marked out of 37 points. If you get 37/37, you will get 5% towards the course grade. There is a max possible of 45/37 if you complete the bonus successfully.
For this reason, please be sure to attempt the assignment early, and try to complete it as if it were due two days prior to the real deadline.
In any case, we will try to answer all the questions that show up on the discussion forum. Note that questions sent to emails will not be answered, as we want all course related material to be viewed for everyone's benefit.
We will still provide enough instructions and expected output so that the requirements are reasonably clear. If there is anything that needs further clarification, remember to ask.
Later, we will store books in a library. For this reason, each book must also have the following attributes: branch (the name of the library) and status (whether a book is "in", "out", or "lost").
[1 point] There will be two scenarios for creating a Book object. In some cases, you will create a book without any of the library information. This means you are given information for the book's title, author, publisher name, and year of publication only. So to initialize the remaining attributes in this case, you just have to give those attributes default values that may later be overwritten by mutators. Define a constructor method for this scenario.
[1 point] Since you will overwrite the two attributes with default values, create the associated mutator methods for them: one mutator for each of the two attributes.
[2 points] In some other cases, you will create a book with all the information for all six attributes given. Define a second constructor method for this scenario. (This is called overloading.)
[2 points] Write a toString() method that gathers all the information about the book into a String. When your method is called, the expected output if the book doesn't have a real branch name will look like this:
Software Engineering (I. Sommerville, 2010) -- Addison Wesley [Available]The output shows the title, then author name, comma, publication year in brackets, dashes, followed by the publisher name, and finally its status. On the other hand, if the book has a branch name, the output will look like this:
Software Engineering (I. Sommerville, 2010) -- Addison Wesley [Available at UBCO]The output is the same as above, except what's in the square brackets indicates the status, and the branch that the book is at.
[2 points] Create a TestBook class that tests your Book class. In particular, you must show that your Book class works for the following cases:
Save your output into a file called Output-TestBook.txt.
[1 point] Make sure your library stores the name of the library (this becomes the book's branch name) and an array of Book objects called books. Define a constant called CAPACITY which describes the maximum number of books that can be stored in the library. For simplicity, we set the capacity to 2 (this means our libraries are really tiny, but it'll make testing easier later).
[1 point] Create a constructor method that initializes the class attributes. Since we don't have the books yet to be added, we won't initialize the array here. But be sure to finish declaring your array so that the appropriate amount of memory is allocated to books.
[4 points] Write a method called addBook() that takes a Book object as input and adds it to the library's collection (i.e., the array of Book objects) if it isn't already full. You know the library is full when the number of books currently in your array is the same as CAPACITY. When you add the Book object to the library, you will need to set its branch name to the library name, and make sure its status is available. If the library is full, then just print out a simple message to let the user know the book cannot be added.
[2 points] Write a method called toString() that gathers all the info in the library into a String. In particular, you must include the branch name, and all the Book objects in the array. To do this, you will need to use a loop that goes through each Book object in the array and call that object's toString(). When your method is called, the expected output of a library will look like this:
Library: UBCO Software Engineering (I. Sommerville, 2010) -- Addison Wesley [Available at UBCO] Artificial Intelligence: A Modern Approach (S. Russell, 2009) -- Prentice Hall [Available at UBCO]
[4 points] Create a TestLibrary class that thats your Library class. In particular, you must show that your Library class works for the following cases:
By the end of this question, you will have created a library that we can add books to.
[1 point] In your Library class, add three integer constants to represent when a book is available, out, and lost. Name these constants AVAILABLE, OUT (i.e., borrowed), and NOSUCHBOOK (i.e., lost) respectively.
[1 point] In your Library class, write a static method called prStatus() that takes an integer value (one of the status values) and returns a meaning string. For example, if the "OUT" status is represented as 0 in your constant definition, return "Borrowed".
[2 points] In your Book class, when you set the default value of status, make sure you set it to one of the constants defined in the Library class. Also in your Book class's toString(), when you concatentate the status of the book, make sure you change it to concatenating the string version of the status by calling the prStatus() method in your Library class. In your TestBook class, anytime you had to give it a status in the constructor or in the mutator, make sure you set it to one of the constants defined in the Library class. Run your TestBook class to make sure everything still works.
By the end of this question, your program should function exactly the same as in Question 2 (i.e., if you run TestBook and TestLibrary, your output should be the same as before). All we did was clean up the code here.
[4 points] In your Library class, write a method called findBook() that checks to see if a given book title (i.e., "target") matches any of the books in the library. The given book title will be specified as an input parameter. If the target is found, return the index position of the array at which that book is found. If the target is not found, return an impossible array index (such as -1). Note that when you use a loop to search, you cannot search through the entire array up to the index books.length because there may be fewer books than that. Also note that once you have found a match for the target book, you no longer have to search the rest of the array so you should use break in your loop.
[3 points] In your Library class, write a method called borrowBook() that calls findBook() to see if the desired book is in the library. If it is not, print out a message indicating that the book is not in this library. If it is, then check the status of the book to see if it's available. If not, print out a message indicating that the book is currently out and the user should check back later. If it is available, then:
[2 points] Create a new test class called TestBorrowing. In this class, create the same 4 Book objects and the two sfu and ubc Library objects. Add b1 to sfu, add b2 to sfu, and add b3 to ubc. Print out what's in the libraries by calling the toString() method like before. Next, add the following three test cases in this test class:
Library: UBC Introduction to Algorithms (CLR, 2009) -- The MIT Press [Available at UBC] Library: SFU Computer Networks (A. Tanenbaum, 2010) -- Prentice Hall [Available at SFU] Software Engineering (I. Sommerville, 2010) -- Addison Wesley [Available at SFU] ********* Testing Borrowing Scenario #1: book doesn't exist ... Checking 'Computer Networks' at UBC: The book you want is not in this library. Try another library. ********* Testing Borrowing Scenario #2: book exist and available ... Checking 'Introduction to Algorithms' at UBC: Please return the book in two weeks Library: UBC Introduction to Algorithms (CLR, 2009) -- The MIT Press [Borrowed at UBC] ********* Testing Borrowing Scenario #3: book exist but not available ... Checking 'Introduction to Algorithms' at UBC: The book you want is currently on loan. Try another time.
[4 points] In the Library class, add a method called returnBook() that takes a book title as input parameter and sets the status of the book to available (use the constant) again. Once a book has been returned, display a "thank you" message and print out the library again to ensure it shows the status of the book is now available. If the user tries to return a book that doesn't belong to the library, you should display an appropriate message letting the user know. Add two test cases to the TestBorrowing class to ensure your method works. Test it by returning a book that was borrowed out and a book that doesn't belong in the library. When your program works, you will get additional output that looks like the following:
********* Testing Returning Scenario #1: book belongs to library and it is returned ... Thank you for returning the book Library: UBC Introduction to Algorithms (CLR, 2009) -- The MIT Press [Available at UBC] ********* Testing Returning Scenario #2: book doesn't belong to library ... We have no records of this book in this library. Try another library.Save your output into a file called Output-TestBorrowing.txt.
[4 points] Write a method called interlibraryLoan() with the following header:
public void interlibraryLoan( String title, Library lib )Similar to borrowBook(), you are now trying to borrow a book with the specific title from library lib. So when you search for the book, search in lib's collection, and if you are changing the book status, you need to change the book in lib's collection. Be sure to print out lib by calling its toString() when a book has been borrowed. Add a test case in TestBorrowing to show this method works.
[4 points] Write a method called returnInterlibraryLoan() with the following header:
public void returnInterlibraryLoan( String title, Library lib )Similar to returnBook(), you are now trying to return a book with the specific title to library lib. So when you search for the book, search in lib's collection, and if you are changing the book status, you need to change the book in lib's collection. Be sure to print out lib by calling its toString() when a book has been returned. Add a test case in TestBorrowing to show this method works.
When your two methods and the new test cases have been added, your working program should show additional output similar to the following:
********* Testing Interlibrary loan: borrowing ... UBC will borrow 'Computer Networks' from SFU: You are borrowing the book from SFU Please return the book in two weeks Library: SFU Computer Networks (A. Tanenbaum, 2010) -- Prentice Hall [Borrowed at SFU] Software Engineering (I. Sommerville, 2010) -- Addison Wesley [Available at SFU] ********* Testing Interlibrary loan: returning ... UBC will return 'Computer Networks' to SFU: Thank you for returning the book to SFU Library: SFU Computer Networks (A. Tanenbaum, 2010) -- Prentice Hall [Available at SFU] Software Engineering (I. Sommerville, 2010) -- Addison Wesley [Available at SFU]Save your output into a file called Output-Bonus.txt.