THINK FIRST·CODE LATER

← All labs

Bounded type parameters

Problem

Write BoundedGenerics with four generic methods:

  • <T extends Comparable<T>> int countGreater(List<T> items, T limit)
  • <T extends Comparable<T>> boolean isSorted(List<T> items)
  • <T extends Comparable<T>> T clamp(T value, T low, T high)
  • <N extends Number> double average(List<N> numbers)

Input: n and n integers, an integer limit, m and m words, a word limit. Each method must work for both integers and strings where it makes sense.

For the input

5 3 9 4 12 7 6 4 pear apple kiwi fig hello

the program prints

numbers > 6: 3
numbers sorted: false
average: 7.00
clamp(6, 0, 10): 6
words > hello: 2
words sorted: false
clamp(hello, d, p): hello

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-3-data-1.txt

5 3 9 4 12 7 6 4 pear apple kiwi fig hello
What your program must print for d2-3-data-1.txt
numbers > 6: 3
numbers sorted: false
average: 7.00
clamp(6, 0, 10): 6
words > hello: 2
words sorted: false
clamp(hello, d, p): hello

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-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content d2-3-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.