Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using page count as a natural ordering (toString() override instructions in the template file).

min(..): Write a static method, min that is parameterized by a generic type V that extends Comparable. The method should take two parameters, x and y, each of type V. The return type of the method should be V. The method should return the smaller of x and y.

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 you can call compareTo(..) on variables of type V. For example, x.compareTo(y) < 0, etc. Review the compareTo method contract if needed (you can find it in the Comparable interface)."

upOrDownOrNeither(..): Write a static method upOrDownOrNeither. 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).

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:

List books = 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Students also viewed these Databases questions