Question: I need help with this java project, please. AudioBook class: Modification 1: In the constructor that has 6 parameters, as well as in the setDuration
I need help with this java project, please.
AudioBook class:
Modification 1: In the constructor that has 6 parameters, as well as in the setDuration method, when the duration parameter is negative, throw an IllegalArgumentException. The message for the exception would be: "The audiobook duration cannot be negative."
Below is the header for the setDuration method:
/**
* The setDuration method stores a value in the duration field.
* @param duration The value to store in duration.
* @exception IllegalArgumentException When duration is negative.
*/
public void setDuration(double duration)
{
// provide implementation
}
Modification 2: Update the javadoc comments to include the @exception tag for both, the constructor and the method.
Request class:
Modification 1: Make the class implement the Comparable interface.
Modification 2: Provide an implementation for the compareTo method such that it compares the value of the memberID instance variable of the two objects. If the memberID variable of the calling object is greater, it returns a positive number, if its smaller it returns a negative number, and if they both have the same value, the method returns 0.
Notes:
Since the type of memberID is int, which is a primitive type, use the ==, >, and < operators.
Include the javadoc comment for the method.
The following is the method signature:
public int compareTo(Request request)
{
// provide implementation
}
Here the origianl code for modification:
AudioBook class
public class AudioBook extends Book { // instance variables private double duration;
/** * Constructs an ElectronicBook object and initializes the fields to the passed values. * @param id Book id. * @param title Book title. * @param genre Book genre. * @param author Book author. * @param isbn Book isbn. * @param duration Audio book's duration in minutes. */ public AudioBook(int id, String title, String genre, String author, String isbn, double duration) { super(id, title, genre, author, isbn); this.duration = duration; } /** * This is a copy constructor. It initializes the fields of the object being created to the same * values as the fields of the object passed as an argument. * @param bookObj The object being copied. */ public AudioBook(AudioBook bookObj) { super(bookObj); if( bookObj != null ) duration = bookObj.duration; }
/** * The getDuration method returns an AudioBook's duration in minutes. * @return The value in the duration field. */ public double getDuration() { return duration; }
/** * The setDuration method stores a value in the duration field. * @param duration The value to store in duration. */ public void setDuration(double duration) { this.duration = duration; }
/** * The toString method returns a string representing the state of an AudioBook object. * @return A string containing the AudioBook's information: id, title, genre, author, * isbn, and duration. */ @Override public String toString() { return super.toString() + String.format( " %5s %-24s %s ", "", "Duration:", duration); }
/** * The equals method compares the AudioBook object calling this method with the AudioBook object * passed as an argument. * @param obj The AudioBook object to compare with. * @return True if both objects have the same value for the title, genre, author, isbn, * and duration instance variables. False otherwise. */ @Override public boolean equals(Object obj) { if( !(obj instanceof AudioBook)) return false; // we already know that obj is of type AudioBook, so it's safe to cast AudioBook bookObj = (AudioBook) obj; // return true or false depending on whether title, genre, author, isbn, // and duration have the same value. return super.equals(obj) && duration == bookObj.duration; } }
Request class:
public class Request { // instance variables private int memberID; private int itemID; private String requestDate; private String status;
/** * Constructs a Request object and initializes the fields to the passed values. * @param memberID The id of the member checking out or downloading the item. * @param itemID The id of the item being checked out or downloaded. * @param requestDate Request date. * @param status Request status. */ public Request(int memberID, int itemID, String requestDate, String status) { this.memberID = memberID; this.itemID = itemID; this.requestDate = requestDate; this.status = status; } /** * This is a copy constructor. It initializes the fields of the object being created to the same * values as the fields in the object passed as an argument. * @param requestObj The object being copied. */ public Request(Request requestObj) { if( requestObj != null ) { memberID = requestObj.memberID; itemID = requestObj.itemID; requestDate = requestObj.requestDate; status = requestObj.status; } }
/** * The getMemberID method returns the id of the member making the request. * @return The value in the memberID field. */ public int getMemberID() { return memberID; } /** * The getItemID method returns the id of the item being requested. * @return The value in the itemID field. */ public int getItemID() { return itemID; }
/** * The getRequestDate method returns the request date. * @return The value in the requestDate field. */ public String getRequestDate() { return requestDate; }
/** * The getStatus method returns the request status. * @return The value in the status field. */ public String getStatus() { return status; }
/** * The setMemberID method stores a value in the memberID field. * @param id the value to store in memberID. */ public void setMemberID(int id) { memberID = id; } /** * The setItemID method stores a value in the itemID field. * @param id the value to store in itemID. */ public void setItemID(int id) { itemID = id; }
/** * The setRequestDate method stores a value in the requestDate field. * @param requestDt the value to store in requestDate. */ public void setRequestDate(String requestDt) { requestDate = requestDt; }
/** * The setStatus method stores a value in the status field. * @param requestStatus the value to store in status. */ public void setStatus(String requestStatus) { status = requestStatus; } /** * The toString method returns a string representing the state of a Request object. * @return A string containing the request information: item ID, request date, due date, * and status. */ @Override public String toString() { return String.format( " %5s %-24s %s %5s %-24s %s %5s %-24s %s %5s %-24s %s", "", "Member ID:", memberID, "", "Item ID:", itemID, "", "Request Date:", requestDate, "", "Status:", status); }
/** * The equals method compares the Request object calling this method with the Request object * passed as an argument. * @param obj The Request object to compare with. * @return True if both objects have the same value for the memberID, itemID, and requestDate * instance variables. False otherwise. */ @Override public boolean equals(Object obj) { // check that the type of the parameter is Request if( !(obj instanceof Request)) return false; // we already know that obj is of type Request, so it's safe to cast Request request = (Request) obj; // return true or false depending on whether memberID, itemID, and requestDate have the same value return this.itemID == request.itemID && this.memberID == request.memberID && this.requestDate.equals(request.requestDate); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
