Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Author.java: Book.java: Library.java Im having trouble with this coding task, looking for help in java. We have provided a Java project that simulates a library
Author.java:
Book.java:
Library.java
Im having trouble with this coding task, looking for help in java.
We have provided a Java project that simulates a library collections system. It has several classes already defined to model authors, books, a library (a collection of books) and a text-based interface which allows you to search the collection, add books to the collection, and list the collection. 3.1 Creating Constructors 1. Run the library program to familiarize yourself with its functionality. Note that printing the collection is not fully operational 2. Complete each of the accessor (getter) methods in the class. Best Practice Tip: always use the this keyword to disambiguate the scope of variables and prevent potential problems when subclassing. 3. Observe that the Book class does not have a constructor defined. Examine the code and determine how it is possible to create instances of the Book class without a constructor. 4. Modify the Book class by adding and implementing the following constructor. public Book(String title, Author author, String isbn, String publishDate) \{ //TODO: your code here \} 5. Adding this constructor will cause syntax errors in other parts of the program. Think about why and then fix these problems by modifying the code appropriately. 3.2 Enforcing Good Encapsulation The class is well-designed: it logically groups data and methods together that semantically define what a book is and how you can use it. The Author class however, is not well-designed. Its data members are publicly exposed and it has no methods at all. 1. Redesign the class and make its member variables private. 2. Create and use getter methods to make the members accessible to the outside world. Use these methods where appropriate. 3. Create setter methods (also called mutator methods) to enable code outside of the Author class to change the member variables. Add some data validation: for example, do not allow "invalid" values for member variables. 4. Add and make use of an appropriate constructor to this class. 5. Add a method to return a string that is the author's last name/first name separated by a comma and then utilize it where appropriate (modify the printBooks() method to use this new method instead of formatting the last name/first name directly). 3.3 Adding and Using Methods The method prints out the title, author, and ISBN in a formatted manner one per line. In this activity, you will modify it to output additional information: the year of its publication and its age (the number of years since its publication date). 1. Modify the printBooks() method in the class to output the publication year of each book in another column. Note that the Book class has a getPublishDate() method already implemented for you. 2. To add the "age" column, you will need to add code (somewhere) to compute the number of years between the publish date and today. Well-designed encapsulation means that code that operates on the data of a class belongs inside the class. Bad encapsulation would mean you locate this code outside the class. For example: you could write code in the printBooks() method that extracted the publish date from the class and calculated the age. This would be an example of bad encapsulation as it locates code acting on a class's data ( publishDate ) outside the class. Instead, do this correctly using good encapsulation by adding a new method to the Book class that returns the number of years between the publish date and today. Name your method getAge(). You may find the following code snippet useful (it utilizes the date/time library provided by Java 8): int years = Period between(this.publishDate, LocalDate. now()). getYears () ; \begin{tabular}{rl|l} 12 & lines (9 sloc) & 160 Bytes \\ 1 & package unl.cse.library; \\ 2 & & \\ 3 & / \\ 4 & Models an author of a book \\ 5 & & \\ 6 & / & \\ 7 & public class Author \{ \\ 8 & & \\ 9 & & public String firstName; \\ 10 & public String lastName; \\ 11 & \\ 12 & \} & \end{tabular} import java.time. LocalDateStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started