Lab Exercise for Assignment 9

COMP112 2020 Tri 1: Lab Exercise 9

Goals

This assignment will give you more experience in developing programs that use arrays and ArrayLists of objects.

Preparation

Download the zip file for lab exercise 9 and extract it to the COMP112-2020T1-Lab9 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.

Summary

  • 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 DominoGame.

Overview

The assignment involves two programs to submit:

  • The first program analyses data about a collection of 4335 NZ earthquakes from May 2016 to May 2017.
  • The second program lets a player play a simple solitaire dominoes game.

There is one exercise to give you practice with arrays and another exercise (TileExercise) closely related to DominoGame.

Make sure that you do TileExercise before starting the domino game program.

Array Exercise

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

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 programme 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. 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 redraw method for drawing all the tiles, the doMouse method to let the user select the tiles and the setting up of the GUI in the main method. You have to complete the methods for the other 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.