THINK FIRST·CODE LATER

← All labs

An ArrayList toolbox

Problem

Write ListToolsApp with five static methods on ArrayList<Integer>:

  • readList(Scanner in) — reads a count, then that many numbers;
  • max(list) — the largest element, or null when the list is empty;
  • sum(list);
  • union(a, b) — a new list: the elements of a followed by those of b;
  • removeDuplicates(list) — keeps the first occurrence of each value, changing the list itself.

Write the loops yourself (no Collections.max). main reads two lists. For the input

5
3 5 45 5 11
3
33 22 5

the program prints

First: [3, 5, 45, 5, 11], maximum 45, sum 69
Second: [33, 22, 5], maximum 33, sum 60
Both together: [3, 5, 45, 5, 11, 33, 22, 5]
Without duplicates: [3, 5, 45, 11, 33, 22]
First is unchanged: [3, 5, 45, 5, 11]

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.

l13-7-data-1.txt

5
3 5 45 5 11
3
33 22 5
What your program must print for l13-7-data-1.txt
First: [3, 5, 45, 5, 11], maximum 45, sum 69
Second: [33, 22, 5], maximum 33, sum 60
Both together: [3, 5, 45, 5, 11, 33, 22, 5]
Without duplicates: [3, 5, 45, 11, 33, 22]
First is unchanged: [3, 5, 45, 5, 11]

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

Windows PowerShell does not understand <:

Get-Content l13-7-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.