Lab Exercise for Assignment 8

COMP112 2020 Tri 1: Lab Exercise 8

Goals

By the end of the assignment, you will have gained experience in writing programs using ArrayLists and will also have had more practice at designing programs using loops.

Preparation

Download the zip file for lab exercise 8 and extract it to the COMP112-2020T1-Lab8 folder in your home folder. It should contain templates for the exercises program. Read through the exercises and run the demo so that you know what you need to do.

Look again at your answer and/or the model answer to the WaveformAnalyser program from assignment 7 and go over the code examples in slides from the lectures on arrays and ArrayLists.

Summary

A program with three methods which involve storing and manipulating values in ArrayLists.

ArrayList Exercises

The file ArrayListExercise.java has three methods that use ArrayLists. Each one is relevant to the main assignment program in a different way, so it will help to complete all of them, but this is not required. Watch the demo video before you work on each method.

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

(a) doPlotNumbersCompareToMiddle()

Reads a sequence of numbers from a file into an ArrayList, and then plots the numbers. This is similar to the GraphPlotter from Assignment 4.

The method should create an ArrayList, then read numbers from a file of numbers adding them to the end of the ArrayList. (The files numbers1.txt, numbers2.txt, numbers3.txt have three different sets of numbers. The lines don't mean anything - there are a different number of numbers on each line.)

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,
  • red if it is larger than the middle number, and
  • black if it is equal to the middle number.

(b) doPlotNumbersCompareToNext()

Just like (a), it reads a sequence of numbers from a file into an ArrayList, and then plots the numbers, but this method compares each number to the number after it, instead of to the middle number:

The rectangle should be
  • green if the number is less than the next number,
  • red if it is larger than the next number, and
  • black if it is the same as the next number.

The last number doesn't have a next number to compare to; draw it in red.

Hint: Use a numeric for(int i=0; i< list.size(); i++) loop rather than a foreach for(double num : list) loop.

(c) doReadAndReverse()

Asks the user for a file and reads the lines from the file storing them into a List of Strings. It then prints out the number of tokens it read and then prints the lines in reverse order. 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.