Data++
[Framework practical 2 of 7]


This practical we're going to take our basic application from last practical, and enhance it so it copes with a more significant dataset, ready for ultimately coping with real geographical datasets.


 

This practical we're going to make our data array larger and use some loops to fill it with fake randomised data. In a future practical we'll read data into this array, but for now we'll generate a random dataset.

Our sequence this practical will be:


 

Start, as last practical, by copying your Analyst and Storage files to a new directory: GEOG5XXXM\src\unpackaged\framework2. Keep a copy of the old files in last practical's directory: don't delete them, but work on the new copies.


 

First, let's increase the size of the array. In your Storage class, alter the array so it is 300 by 300.


 

Secondly, remove the code in the Analyst class that currently fills the array and replace it with code to fill all the array spaces with a single fixed number, say '42.23'.

To do this you'll need a nested loop structure in Analyst.java that fills all the spaces in the array with this number. Once you've written that, write a separate, second set of loops to run through it again and test it has been filled by printing the numbers in the array to the screen (see 'tip' at bottom of this page).

Remember: each time you write a major chunk of code, it is good to think of how to test the code does what you expect. Here we should print the array to the screen to test it is as we expect and we're not doing anything odd with the array dimensions.

Note: we could merge all this together, so we have one loop that both sets a random value and prints the value. This would be more efficient. However, keep these loops separate here (and make extra looping structures for the work we do later). This is because next practical we're going to cut and paste these structures into diverse places, and so we need the functionalities to be within their own, separate, looping structures.


 

Once you've got that working, then replace the code to fill the array with 42.23's with the code to fill it with random numbers between 0 and just less than 1000.

These are gained using a built-in Java method:

Math.random()

which returns a random double between zero and just less than one, and is used with a normal double variable thus:

double var = Math.random();

As you want a number between 0 and ~1000 and this gives you a number between 0 and ~1, you'll need to multiple the result by 1000 before putting it in the array variable space. Again, test the code to make sure it works - it should give you different numbers each time you run it.


 

Once you've got that working, move on to Part Two, where we'll implement a small chunk of code to do some processing.

 


 

Tip: If you want to print a whole array out, sadly this doesn't work:

System.out.println(store.data);

With the exception of Strings, when you print an object out using its name, you'll get a gibberish-looking code which represents the object to the JVM. Instead we have to loop through the array and print each location in it. You could run through the array and use System.out.println(store.data[i][j]) to print each value on a new line, but it would be neater if the rows in the array were printed as lines on the screen, dropping a line at the end of each row.

If you want to print numbers on the same line, rather than dropping a line with each println statement, use print, instead:

System.out.print(store.data[i][j] + " ");

in your inner loop. This will then print all of a row on the same line, space separated. Then, in the outer loop, use the standard statement with no content:

System.out.println("");

To drop a line each time the outer loop moves to the next row.

Try and work out how you'd do this yourself, but if you need some hints, see the quizzes associated with this part's online lectures, and, if you want the answer, see the associated key ideas page.