Assignment 9

COMP112 2020 Tri 1: Assignment 9

  • Due 18 Jun 10am

Goals

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

Summary

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.

To Submit:

  • Your version of EarthquakeAnalyser.java
  • Your version of DominoGame.java
  • Your UnderstandingJava.txt file

Earthquake Analyser

EarthquakeAnalyser analyses data about a collection of 4335 New Zealand earthquakes from May 2016 to May 2017. The data is from http://quakesearch.geonet.org.nz/. You are to complete the EarthquakeAnalyser program which does some useful analysis of this data.

Each line of the file "earthquake-data.txt" has a description of one earthquake:
  • ID, time, longitude, latitude, magnitude
eg

2016p385555  2016-05-22T15:43:56.619Z  175.6988525  -39.39247894  1.0
2016p415555  2016-06-02T18:07:49.213Z  175.3636322  -39.28754807  1.0

Note the file bigearthquake-data.txt is a subset of earthquake-data.txt. It has just the 421 earthquakes of magnitude 4.0 and above which may be useful for testing, since it is not as big as the full file.

The overall design of the EarthquakeAnalyser program is provided, along with the declaration of a field to keep track of the earthquakes. You have to complete the definitions of the methods that deal with ArrayList.

A Earthquake class is also provided. It contains a constructor for creating a new Earthquake object. The constructor has five parameters: ID, time, longitude, latitude, magnitude.

The file "example-output.txt" has sample output for the "bigearthquake-data.txt" file, which only contains earthquakes with magnitude 4 and above.

COMP 102 lecture videos #1 and #3 for week 10, may be helpful for the load() method.

Core

Complete two methods:
  • loadData
    • Loads the data from a file into a field containing an ArrayList of Earthquake objects.
    • Hint: read all the lines from the file, and then take each line apart into the values to pass to the Earthquake constructor.
  • findBigOnes
    • Prints out all the earthquakes in the ArrayList that have a magnitude 5.5 and over.
    • Hint: see the methods in the Earthquake class, especially getMagnitude and toString

Completion

Complete the findPairs method:
  • Compares each Earthquake in the list with every other Earthquake and finds every pair of "close" earthquakes - earthquakes that are within 1km of each other.
  • For each "close" pair, it should print
    • their ID's,
    • the distance between them

Challenge

Complete the findFollowOns method:
  • Constructs a new ArrayList containing every earthquake with a magnitude that is at least 6.0.
  • For each such earthquake on this list:
    • Finds a list of all the "follow-on" earthquakes: later earthquakes within a distance of 10km.
    • If there are at least two follow-on earthquakes, then it prints out the full details of the big earthquake followed by ID, magnitude and days since the big one for each follow-on earthquake.

Domino Game

Dominoes are rectangular tiles that have two numbers on them, usually represented by patterns of dots (no dots represents 0). There are many games using dominoes, many of which involve a player picking up a "hand" of dominoes, and then placing them on the "table" according to some rules.

The DominoGame program allows a player to play one of these games. The program displays the set of six dominoes in their hand, and the dominoes on the table. It allows the user to
  • Pick up a new domino to fill an empty space in the hand;
  • Select a domino in the hand (with the mouse)
  • "Flip" the selected domino
  • Place the selected domino on the table.
  • Rearrange the order of the dominoes on their hand;

The program has eight buttons ("Pickup", "Place", "Flip", "Left", "Right", "Suggest", Restart", and "Quit") and also responds to the mouse.

The overall design of the DominoGame program is provided, along with declarations of all the fields and constants you need, and some of the code to respond to the buttons and mouse as well as the code for drawings. You have to complete the definitions of the methods that do all the work!

A Domino class is also provided. It contains
  • a constructor for creating a new Domino,
  • a draw(double left, double top) method for drawing a Domino at a specified position, and
  • constants specifying the size of the dominoes.
  • getLeftNum() and getRightNum() methods for returning the left and right number of the domino
  • a flipNums() method to turn the domino over
Read through the provided code to make sure you know what it does. The COMP 102 lectures for week 10 are very relevant to this program.

Program Design

The program has two fields to keep track of the dominoes:
  • hand : an array that can hold up to 6 Dominoes. It will contain null in any position that does not have a Domino.
  • table : an ArrayList of the Dominoes that have been placed on the table.

The hand should be displayed with a rectangular border and each domino drawn in its place. Empty spaces (containing null) should be displayed as empty.

The user can select a position on the hand using the mouse. The selected position (domino or empty space) should be highlighted in some way, eg by drawing a border around it, or by drawing some marker underneath it. The index of the selected position is stored in the selectedPos field.

The user can use the Left or Right button to move the contents of the selected position one place to the left or the right (by swapping with the contents of the adjacent position).

If there are any empty positions on the hand, the user can use the Pickup button to get a new (random) domino which will be added to the hand at the leftmost empty position. If there are no empty positions, Pickup should do nothing.

If the selected position contains a domino, the user can use the Place button to move the selected domino to the table.

The table is represented by an ArrayList of Domino objects, which are drawn in rows underneath the hand. At the beginning of the game the table should be empty. Dominoes should be added to the end of the table.

The following is a screenshot of the layout of the "hand" and the "table", showing a hand with five dominoes, and a "table" with ten dominoes.

domino-game-table.png

Core

  • Declare and initialise the hand and table fields

  • Complete the setupGUI method so that it sets up the buttons, and the mouselistener

  • Complete the restart() method so that it
    • sets the table field to be empty
    • sets the hand field to have no dominoes in it
    • calls the redraw() method to redraw the hand and the table

  • Complete the pickup() method so that it
    • looks for an empty position in the hand array, and if there is one, creates a new Domino object and puts it at that position in the hand array.

  • Complete the placeDomino() method so that it moves the domino at the selected position in the hand (if there is one) to the table.

  • Test your program carefully, to make sure that it works correctly in all cases:
    • when the hand is empty, when it is full, and when it is only part full.
    • when the selected position is at the ends and when it is not at the ends.

Completion

  • Complete the flipDomino() method so that it flips over the domino at the selected position in the hand (if there is one). (Hint: use the methods provided in the Domino class.)

  • Complete the moveLeft() and moveRight() methods so that they move the contents of the currently selected position on the hand to the left or the right, swapping the tile (or space) with the one next to it. (Note that they should not attempt to move something off either end of the hand.)

Challenge

  • Complete the suggestDomino() method, which finds a domino in the user's hand which can be placed into a position on the table. If there is such a domino, it should tell the user which domino it is; if there is no domino that matches, it should inform the user that there are no possible moves.
    The basic rule of Dominoes is that
    • If there are no dominoes on the table yet, only a double (a domino with the same number left and right) can be placed on the table. (Strictly, the double domino should be placed vertically, but you don't need to worry about that).
    • A domino can only be placed next to another domino if they have matching numbers (one of the numbers on the domino in the hand is the same as the right-hand number on the last domino on the table).

The following are two screenshots. In the first one, a domino was suggested. In the second one, there was no domino to be placed at the end of the table.

domino-game-table.png

domino-game-table.png

  • Let the user move Dominoes around on the hand by dragging them with the mouse. If a Domino is dragged to another Domino, it should be inserted by moving the other Dominoes along. If a Domino is dragged to a space, it can simply be put in the space.

Understanding Java

Answer the following questions in the UnderStandingJava.txt file, and make sure you submit it.

  1. Explain why "off-by-one" errors are so easy to make when programming with arrays.
  2. List three similarities and three differences between arrays and ArrayLists.
  3. Complete the following method that will reverse the order of items in an array of Card objects
     public void reverseItems(Card[] cardArray){


     }