ArrayList exercises

The file Exercise.java has three methods that use ArrayLists. Each one is relevant to the WaveformAnalyser program in a different way, so it will not hurt to complete all of them, but this is not required.

Note: Several of the methods involve reading values from a file. You can use the Show file button to display the contents of a file.

plotNumbers()

Reads a sequence of numbers from a file into an ArrayList, and then plots the numbers.

The method should create an ArrayList, then read numbers from the file numbers.txt, adding them to the end of the ArrayList.

The method then finds the number half way through the sequence of numbers. If there are count numbers, then the middle number is at index (count-1)/2

It then iterates through the ArrayList, plotting each number as rectangle. The number specifies the height of a rectangle; the width of the rectangle is 5. The rectangle should be green if the number is less than the middle number, black if it is the same as the middle number, and red otherwise.

readAndReverse()

Asks the user for a file and reads the tokens from the file storing them in an ArrayList of Strings. It then prints out the number of tokens it read and then prints the tokens in reverse order, one per line. Try your method out on the file a-story.txt and b-story.txt; it should print out a coherent story if your method works correctly.

sieve()

The Sieve of Erastothenes is a very old way of finding prime numbers. sieve implements this technique. It constructs an ArrayList containing the numbers 1 to 100, and prints them out. It then repeatedly asks the user for a number, and removes all multiples of that number (except for the number itself) from the list, and prints the list out again.

For example, if the user selects 5, it will remove 10, 15, 20, 25, 30, etc from the list It should quit asking when the user selects 0.

Hints: