Lab Exercise for Assignment 10

COMP102/112 2022 Tri 1: Lab Exercise 10

Goals

This assignment is intended to give you more experience in developing programs that use 2D arrays, including image data, and arrays of objects.

Preparation

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

Go over the lectures material on 2D arrays.

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

Summary

Overview

This assignment involves 2D arrays. There are two exercises. The exercises have model code that you may find useful when working on the main program. You should do both exercises.

RayBitsExercise

The RayBitsExercise program involves a square grid of colours. It has methods to make a new square, draw the square in the graphics pane, shift each color to the left, and check a column to see whether all the colors are the same.

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 RayBitsExercise demo.

The user interface and the makeNewPuzzle method are written for you; you are to complete:

  • drawPuzzle, which draws the puzzle as a grid of coloured squares with black borders.
  • shiftLeft, which shifts every value one step to the left, except for cells in the leftmost column, which are moved to the rightmost column. No colors should be lost!
  • checkCol(int col), which checks whether all the colours in the specified column are the same, returning true if they are, and false otherwise.

WordGridExercise

The WordGridExercise program lets the user load, and edit a 2D table of words. The table is represented by a 2D array of Strings, using null to represent empty cells.

If you are using a computer in a lab, you can run the WordGridExercise demo to see what your program should do (look in the assig10-demos folder). You can also watch the demo video.

The user interface allows the user to select a cell in the table with the mouse and has a textField where the user can enter a word. The Set Entry button will put the word into the selected cell; the Delete Entry button will remove the current value from the selected cell. There is also a Uppercase button to change all the words in the table to upper case and a Fill Right button to copy words into the empty spaces to their right.

The user interface methods are written for you. You are to complete four methods for loading, displaying, uppercasing, and filling the table:

  • redisplay() should draw the table in the graphics pane as a grid with words, and highlight the currently selected cell. The position of the table and the size of the individual cells is specified by the LEFT , TOP , WIDTH , and HEIGHT constants.
  • upperCase() should replace every word in the table by the word in upper case.
WordGrid-start.png arrow-east.png WordGrid-uppercase.png
  • fillRight() should find every cell that contains a word and has an empty cell to its right, and copy the word into the empty cell. (Note, if the cell that was empty but now has a word has an empty cell to its right, then the word could be copied to that empty cell.)
WordGrid-start.png arrow-east.png WordGrid-fillright.png
  • loadSparse() should load a table from a file (with the data stored as a "sparse array"). The first two numbers in the file will be the number of rows and columns, followed by lines specifying the non-null table values. Each line will have a row and column, followed by the word. The method should
    • Let the user choose a file
    • Read the size of the table and create the array
    • Read each value and put it at the specified row and column in the table
    • Close the file