Question
Objective: In this JAVA activity, you will practice writing generic methods and working with generic classes (specifically a generic Pair class). First: write a Book
Objective: In this JAVA activity, you will practice writing generic methods and working with generic classes (specifically a generic Pair class).
First: write a Book class that stores two fields -- a String title and an int page count. Make the class implement Comparable
min(..): Write a static method, min that is parameterized by a generic type V that extends Comparable
Some examples (for testing purposes in the main):
--> min(4, 2) // returns 2
--> min(Year.of(1990), Year.of(1995)) // returns 1990
"Hint: since the type variable V is 'bounded' by Comparable
upOrDownOrNeither(..): Write a static method upOrDownOrNeither. The method should be parameterized by a generic type T that extends Comparable
--> -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).
Some examples (for testing in main):
--> upOrDownOrNeither("a", "d", "b", "g"); // (neither) should return 0
--> upOrDownOrNeither(2.3, 3.4, 4.5, 5.6); // (up/increasing) should return -1
isPal(..) Write a static method isPal that is parameterized by a generic type A (this type parameter doesn't require a bound). The method should accept a List as a parameter and should return true only if the list provided is a palindrome; false otherwise.
"a 'list palindrome' is just a list that reads the same forwards and backwards"
Some examples (for testing in main):
--> isPal( List.of(1, 2, 1) ) // returns true
--> isPal( List.of("r", "a", "c", "e", "c", "a", "r") ) // returns true
--> isPal( List.of('2.0', '1.0', '3.0') ) // returns false
-->isPal( List.of() ) // returns true
--> isPal( List.of(Year.of(1990), Year.of(1995) ) ) // returns false
pivot(..) Write a static method pivot that is parameterized by an orderable generic type U that extends Comparable.
--> the method should take as parameters a List source and a pivotItem of type U
--> the method's return type should be: Pair>
The method splits the source list into a Pair containing two lists:
--> a 'left' list whose items are <= than the pivotItem and,
--> a 'right' list whose items are > than the pivotItem.
Uncomment the following testing code in your main for the final compare output test:
Listbooks = List.of( new Book("A", 304), new Book("Z", 203), new Book("M", 495), new Book("B", 35), new Book("G", 107)); Pair > ans = Tester.pivot(books, new Book("X", 150)); System.out.println(ans);
Here are more examples (for testing in main):
--> pivot(List.of(1, 9, 4, 6, 7, 3), 5 ) // returns pair ( [1, 4, 3], [9, 6, 7] )
--> pivot(List.of('a', 'z', 'b', 'x', 'c', 'g'), 'm' ) // returns pair ( [a, b, c, g], [z, x] )
--> pivot(List.of(1), 0 ) // returns pair ( [], [1] )
--> pivot(List.of(1), 3 ) // returns pair ( [1], [] )
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