Task 7 of 7 · 0 solved · 7 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

Problem

9.3 Wildcards — capstone: produce and consume

Write a complete program with two wildcard methods: sumAll(List<? extends Number>) that reads and returns the total, and addRange(List<? super Integer>, int n) that writes the numbers 1 through n into a list.

Read n, fill an ArrayList<Integer> with addRange, print the list and its total. Then read a count and that many decimals into an ArrayList<Double> and print their total with the same sumAll.

For 3 then 2 1.5 2.5:

[1, 2, 3]
6.0
4.0

PECS in one program: extends for the producer you read from, super for the consumer you write into.

time 1000 ms memory 256 Mb
Solution
Attempts: 0 A hint guides you to the fix — it never writes the code for you.
✦
Ready to check

Press «Submit» to run your code against the task's test cases — that is what sets your score. «AI review» is an optional second opinion: it reads your code — logic and style, not just the output — and suggests the next step without handing over the answer. It never changes your score.

Working…