THINK FIRST·CODE LATER

← All labs

Generic Minimum and Maximum with Pair<A, B>

Problem

Write a generic class Pair<A, B> (two private final fields, a constructor, getFirst() and getSecond()) and a single generic method

static <T extends Comparable<T>> Pair<T, T> minMax(List<T> items)

that returns a pair (minimum, maximum) of a list, or null for an empty list. Use compareTo only — no sorting, no Collections.min.

The program reads n followed by n integers, then m followed by m words, and uses the same minMax method for the List<Integer> and the List<String>. Output:

Input:

5
4 -3 12 0 7
3
pear apple fig

Output:

Numbers: min=-3 max=12
Words: min=apple max=pear

When a list is empty, print Numbers: empty or Words: empty. Words are compared with String.compareTo (so upper-case letters come before lower-case ones).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d2-1-data-1.txt

5
4 -3 12 0 7
3
pear apple fig
What your program must print for d2-1-data-1.txt
Numbers: min=-3 max=12
Words: min=apple max=pear

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d2-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content d2-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.