Question: Write a Book class that stores two fields: the book title; the Year in which the book was released. Be sure these fields are marked

Write a Book class that stores two fields:
the book title;
the Year in which the book was released.
Be sure these fields are marked private, and provide getter methods for each one.
Now provide a constructor that accepts as parameters (in this order):
the title
the releaseYear (type: Year)
Initialize both of these fields in the constructor and make the Book class implement Comparable (i.e.: you want the Books to have some natural ordering). When you go to implement the required compareTo(..) method, do the comparison on the basis of the book's release year.
Next, override toString to render the book like so: "[title]-[releaseYear]".
You'll also need to override the following methods:
@Override public boolean equals(Object o){/* todo */}
@Override public int hashCode(){/* todo */}
@Override public int compareTo(Book o){/* todo */}
The equals method should use releaseYear and title as significant fields for determining equality (use these fields to compute the hash value as well in hashCode()).
Practice writing equals yourself, don't use the IntelliJ generated version please.
Step 2: Tester class static methods
upDownOrNeither(..): Next, complete the static method upDownOrNeither. The method should be parameterized by a generic type T that extends Comparable (consult the chapter for syntax on this). The method should take four formal parameters: t1, t2, t3, and t4-- each of type T; the int returned by the method should be:
-1 if the parameters t1, t2, t3, and t4 are in ascending (up) order,
1 if all parameters are in descending (down) order and,
0 if the parameters are unordered (neither).
Hint: since the type variable T is 'bounded' by Comparable you can call compareTo(..) on variables of type T. For example, t1.compareTo(t2)<0, etc. Review the compareTo method contract if needed (you can find it in the Comparable interface).
rmDupsPreservingOrder(..): This method is provided for you, though the tests will only pass assuming you've implemented equals and hashCode correctly for the book class.
Here are some examples of this method in action (removes duplicates while preserving the original order):
rmDupsPreservingOrder( List.of(4,4,5,5,3,5))// prints: [4,5,3]
rmDupsPreservingOrder( List.of())// prints: []
See the Tester class for an example involving the Book class.
Step 3: More testing code (add to top of main(..) method)
Add the following to the top of the main(..) method -- you'll need to print each one out:
upOrDownOrNeither("a","d","b","g"); //(neither) should print 0 upOrDownOrNeither(2.3,3.4,4.5,5.6); //(up/increasing) should print -1 upOrDownOrNeither(5,3,2,1); //(down/decreasing) should print 1 upOrDownOrNeither('c','d','h','i'); //(up) should print -1 upOrDownOrNeither('c','d','h','a'); //(neither) should print 0 upOrDownOrNeither( Year.of(2002), Year.of(2001), Year.of(2000), Year.of(1990)); //(down) should print 1
Step 4(Optional): Checking Same-ness
Implement another (static) generic method in the Tester class called sameEntries. The method should be parameterized by a single generic type T and should take a List source as a parameter, and return true if all entries in source are the same; false otherwise.
Challenge: try to do this method in 1(or 2) line(s) of code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!