CS 157 Lab Exercise

CS 157 Algorithmic Problem Solving - Labs 10 and 11



Purpose: Practice using 1- and 2-dimensional arrays

Visual Acuity

Generally speaking, visual actuity is the ability to discern small objects, typically measured using black objects on a white background (think the "big E" chart at your optometrist office).  According to the NDT Resource Center, "at 12 inches, the normal visual acuity of the human eye is 0.00349 inch. What this means is that if you had alternating black and white lines that were all 0.00349 inch wide, it would appear to most people as a mass of solid gray."

Imagine devising a test or game to measure other aspects of visual ability, like the ability to distinguish between closely-related colors or shades of gray.  One might display a sequence of boxes of similar shades, like this:

Color Chart

or even this:

Color Chart


Lab 10: Using 1-dimensional arrays

This exercise is based on Exercise 14.10.2 on page 403 of the text.

Create a class Concentration that draws a row of grayscale boxes (like the first image) that can be used to test the user's ability to match shades of gray.  Your program will display a row of 24 square boxes (scaled to fit no matter how large the canvas). Use a FramedRect to draw a thin black border around each box.  Generate 12 shades of gray and set the color of two randomly chosen boxes with each shade.

Use an array of Color objects to represent the grayscale values.  Use an array of FilledRect objects to represent the row of boxes.

When run, the program allows the user to select a box as the first member of a matching pair.  When this happens, change the color of the border to red.  If the next box clicked is exactly the same shade as the first, remove them both.  If the second box does not match the first, set the first border back to black.  The user can then try again.

Display a score below the row of boxes using the form "Score = 4 (out of 15 tries)" using either Text or JLabel objects.

You'll also need a constructor to call startController with a reasonable width and height for the window.

Demonstrate your solution to the instructor.

Warmup questions:


Lab 11 - Make it two dimensional

Start with a NEW PROJECT called Concentration2D which improves the original by displaying an 8 by 8 matrix of 64 total colored boxes as described in Exercise 14.10.2.

The basics (creating the board as a 2-dimensional array, for example) are already coded up in these two files: Concentration and ColorRect. Feel free to modify the way it works, change colors, etc.

More to come!


Stephen P. Carl