Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Interface Implementation Lab Working in pair programming teams, define a class to model a book. Class MyBook has five String fields: title, author FirstName, authorLastName,
Interface Implementation Lab Working in pair programming teams, define a class to model a book. Class MyBook has five String fields: title, author FirstName, authorLastName, and two forms of identifiers ISBN10 and ISBN13. In Eclipse create a new Java Project with a Project name: CMSC256-Lab2. In this project, create a new Java Class with a Package of lab2 (this must be all lower case) and a Name of MyBook that implements the java.lang.Comparable interface. The MyBook class will implement the Comparable Interface, so the class header is: public class MyBook implements Comparable { Change the type parameter to MyBook, if necessary. There are five private data fields (instance variables) in the MyBook class: String title, String authorFirstName, String authorLastName, String ISBN10, and String ISBN13 All instance variables must be declared private MyBook class has two constructors. The default constructor sets the title, author FirstName, and authorLastName fields to the string, "None Given" and both isbn10 and isbn13 to a string containing all zeroes of the appropriate length. Before writing the parameterized constructor, implement accessor (getter) and mutator (setter) methods for each of the instance variables with the following constraints: Null is not permitted for any fields . the setters should check for null before assignments and throw an IllegalArgumentException if null is passed to the method isbn 10 and isbn13 these are String variables, however they may contain ONLY digits isbn 10 should always have a length of 10 . isbn13 should always have a length of 13 . Throw an IllegalArgumentException if an invalid argument is passed to the setter method The overloaded constructor has five String parameters and initializes title, author FirstName, authorLastName, ISBN10, and ISBN13 with these values. In order to validate the parameters to this constructor, the body of the method should call the setter methods. Use this method header for the parameterized constructor: public MyBook (String title, String authorFirstName, String authorLastName, String ISBN10, String ISBN13) Define the following methods: public String toString() o Format should look like this: Title: Trigger Warning: Short Fictions and Disturbances Author: Neil Gaiman ISBN10: 0062330268 ISBN13: 9780062330260 public boolean equals (Object otherBook) The equals method overrides the one that is inherited from the object class; therefore, the method signature must match exactly - the parameter must be of type Object. Two books are equal if they have the same ISBN numbers. Guidelines for overriding the equals method: Use the == operator to check if the argument is a reference to itself. Use the instanceof operator to check if the argument has the correct data type. Cast the argument to the correct type. For each significant data member, test for equality. Test these three properties: o Is it symmetric? o Is it transitive? O is it consistent? public int compareTo (MyBook other) o order by author last name, first name, then title Here is an example of implementing a compare To method when there is more than one instance variable to compare: Consider a class called PhoneNumber that represents a telephone number as three data elements, the area code (example: 804) the prefix (example: 828) and the line number (example: 7135). public class Phone Number implements Comparable { private int areaCode, prefix, lineNumber; // constructor and other methods not shown // Comparison starts with the most general data field // (areaCode) and works to the more specific data fields public int compareTo (PhoneNumber other) { int result = areaCode - other.getAreaCode(); if (result == 0) { // At this point area codes are equal, compare prefixes result = prefix - other.getPrefix(); if (result == 0) { // Area codes & prefixes are equal, compare line numbers result = lineNumber - other.getLineNumber(); return result; For the MyBook class, compare by last name, then first name, then title. When you are confident in the correctness of your class, Export the file to the file system and then submit it to Gradescope for grading. You may add your pair programming partner in Gradescope using the "Add Group Member" link
Step 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