Lab Exercise for Assignment 9

COMP102/112 2022 Tri 1: Lab Exercise 9

Goals

This assignment has two programs. The first program will give you experience in manipulating values in ArrayLists. The second program uses arrays and ArrayLists of objects.

Preparation

Download the zip file for lab exercise 9 and extract it to the COMP-102-112-2022T1-Lab9 folder in your home folder. It should contain templates for the exercises program.

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

Later when the model answer to assignment 8 becomes available, make sure you understand all the components of the programs.

Summary

  • ArrayList Exercises.
    right A program with three methods which involve storing and manipulating values in ArrayLists.
  • Array Exercise.
    right Two methods which involve storing and manipulating values in arrays: doDuckGame and doCountScores.
  • Tile Exercise.
    right A program letting the user move Scrabble tiles around on a rack. This exercise is closely related to BattleGame. Make sure that you do TileExercise before starting the BattleGame program.

ArrayList Exercises

The file ArrayListExercise.java has three methods that use ArrayLists. Each one is relevant to the main assignment programs 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. In addition, if you are using a computer in a lab, you can run the ArrayListExercise demo.

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) plotNumbersCompareToMiddle()

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) plotNumbersCompareToNext()

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) readAndReverse()

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.

Array Exercise

The ArrayExercise program has two methods to write: doDuckGame and doCountScores.

  • If you are using a computer in a lab, you can run the ArrayExercise demo to see what your program should do (look in the assig9-demos folder).
  • Or, you can watch the demo video.

doDuckGame is a little game with an array of five toy Duck objects. The method should first of all set up the array of Ducks, then it should ask the user to enter the index of a Duck to shoot. If that duck is still in the array, the program should shoot the Duck, remove it from the array, and make each remaining duck jiggle. Run the demo to see how it works.

Duck objects have
  • A constructor with one double parameter, specifying the x position of the Duck on the screen.
  • A jiggle method with no parameters that will make the duck jiggle up and down a few times. You shouldn't jiggle a dead duck.
  • A shoot method with no parameters that will make the duck fall upside down.

doCountScores should read all the scores in a file called scores.txt, and count them. Each score is an integer between 0 and 10 (inclusive). Your program should count how many 0's, how many 1's, how many 2's, etc. It should use an array to keep the 11 different counts in. (For example, the count of how many 4's the program has read should be stored at index 4 in the array).

doCountScores should
  • Construct a new array to hold the 11 counts.
  • Read the scores in the file, increasing the appropriate count every time it reads a score.
  • Print out all 11 counts.

Tile Exercise

The TileExercise program lets the user move Scrabble tiles around on a rack.

  • If you are using a computer in a lab, you can run the TileExercise demo to see what your program should do (look in the assig9-demos folder).
  • Or, you can watch the demo video.

The rack is represented by an array of 7 Tile objects. (The Tile class is written for you.) Part of the program is written for you, including the fields, the setupGUI method, the redraw method for drawing all the tiles, and the doMouse method to let the user select the tiles and. You have to complete the methods for the buttons:
  • reset which should fill the array in the tiles field with new Tile objects.
  • left which should move the currently selected tile one step to the left in the array (by swapping it with the tile on its left) and redrawing.
  • right which should move the currently selected tile one step to the right in the array (by swapping it with the tile on its right) and redrawing.
  • remove which should remove the currently selected tile from the array, and redrawing.

The doMouse method, which is written for you, allows the user to select a Tile, and stores the index of the tile in the selectedIndex field, which should be used in the methods that you write.