[Framework practical 4 of 7]
This practical we're going to continue to practice our method development. Later in the course, we'll be dealing with converting data into images for display. Here we'll build a couple of methods that will be useful for that; one to turn 2D arrays into 1D arrays, and another for stretching an array between some pre-determined minimum and maximum (we won't yet actually produce an image - we'll just work up some of the necessary methods). At the same time, we'll look at some useful elements of array manipulation.
Start, as last practical, by copying your Analyst and Storage files to a new directory: GEOG5XXXM\src\unpackaged\framework4\ -- keep a copy of the old files in last practical's directory: don't delete them, but work on the new copies.
First, the stretching method. This will be built in the Storage class.
Images are based on arrays containing values between 0 and 255. To convert our data into an image, we need to re-range it between these two values. We need a method that will take the data in our array, and stretch or compress the numbers across a given range, that is, the minimum value matches a new minimum, the maximum a new maximum, and the rest of the data is spread between them, proportional to the values in the original dataset.
First up, declare a method in Storage with the following characteristics:
Parameter variable 1:
Parameter variable 2:
When you declare the method, also put in its starting and ending curly brackets - that way you won't put method code outside the method, and you'll know they are done. Remember, methods go one-after-the-other, rather than nesting or overlapping.
Note the return type is a 2D array. This is our first example of returning an array. Note that you have to show the dimensions involved, but not the size.
Now we just need to fill in the method with code. Here's the algorithm:
// Set up double variables containing currentMaximum and currentMinimum (where could we get these from...?)
// Make a double array called tempArray, and size [data.length][data.length]
// Open loop with index i down data rows
// Open loop with index j across a row
// tempArray[i][j] = data[i][j]
// tempArray[i][j] = tempArray[i][j] - currentMinimum (so values between 0 and currentMaximum - currentMinimum)
// tempArray[i][j] = tempArray[i][j] / (currentMaximum - currentMinimum) (so values between 0 and 1)
// tempArray[i][j] = tempArray[i][j] * (newMaximum - newMinimum)
(so values between 0 and newMaximum - newMinimum)
// tempArray[i][j] = tempArray[i][j] + newMinimum (so values between newMinimum and newMaximum)
// End loop across row
// End loop down rows
// return tempArray
Why do you think we use
Given we need to copy the data from
tempArray, why don't we just do this at the start:
tempArray = data (hint: remember that arrays are objects, not primitives)
Why do you think we make tempArray size
Now work out a way of testing that the code works from within your Analyst class. In particular, make sure you can re-range an array between 0 and 255. Remember, the method returns a 2D array, so you'd need this kind of thing inside Analyst to get the results:
double resultsArray = store.getRerangedData(0.0,255.0);
Note that because you just attach a 2D array label to the results, you don't have to specify the size -- you're just attaching a label to something that already exists.
Once you've tested it works ok, go on to Part Two, where we'll work on our second method, which turns a 2D array into a 1D array.