Bounded Types

Lab Java Advanced 30 points

Write static <T extends Comparable<T>> T maxOf(List<T> list). Use it to print the largest integer and the alphabetically last word.

Input

Line 1: integers. Line 2: words.

Output

Two lines.

Example

Input

3 -1 42 7
pear apple zebra fig

Output

42
zebra
Sample input available Sample output available
Editor
Java

Hints

extends in a type bound means 'is a subtype of' - here, 'can be compared'.

Solution explanation

The bound guarantees that compareTo exists for any T passed in.

Show solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    static <T extends Comparable<T>> T maxOf(List<T> list) {
        T best = list.get(0);
        for (T item : list) {
            if (item.compareTo(best) > 0) {
                best = item;
            }
        }
        return best;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Integer> nums = new ArrayList<Integer>();
        for (String t : sc.nextLine().trim().split("\\s+")) {
            nums.add(Integer.parseInt(t));
        }
        List<String> words = new ArrayList<String>();
        for (String w : sc.nextLine().trim().split("\\s+")) {
            words.add(w);
        }
        System.out.println(maxOf(nums));
        System.out.println(maxOf(words));
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD