Practice pieces


Data Storage

Ok, so this is about the difference between vector and raster data. As we build out our project, you'll see that we start holding the data in larger 2D data arrays, each value representing a raster data point. However, as we know, the alternative way of holding data is as a series of points: vector data. This practice piece looks at how to set this up. We'll get the the position where we can store multiple lines of vector points, each having an x and y coordinate and an attribute String.


Hints:

Start with two fresh blank files: "Analyst.java" and "Storage.java", in the same directory.

In Analyst, put in a main method, and in this method, make yourself a Storage class object called "store".


 

Now, in Storage, let's start with a 1D array. Make a data array of type "int" for the moment, and make it large enough to hold three things. Back in the Analyst class, after you make the "store" object, use the object and the dot operator to set the value of the three spaces in store.data.


 

This is fine, but we don't yet have something that will store x and y coordinates, let alone the attribute data we might also want to store for a set of vector points. For this, rather than storing ints we need to store objects made from a class that represents a single vector point and which has variables inside it to represent all the data that might be associated with a single point. If we build a 1D array of such objects, we'll then have a single line of vector points stored.

First, make a new java file in the same directory as the others. Call it "Point.java". Add in integer instance variables to store a single x and a single y value.


 

Now, back in your Storage class, change the type of the array from "int" to "Point". Provided the Point.java is in the same directory as Storage.java, the JVM will find it.


 

Finally, in Analyst, set up new Point objects in the array instead of ints, and give their x and ys values. At the moment, you'll be doing something like this:

store.data[0] = 10;

But remembering that you need to make the objects first when an array holds objects, you'll need to change this to something like:

store.data[0] = new Point();
store.data[0].x = 10;

If you need more help, here's the answers (or one option, anyhow). There's also a brief list of things to add to finish the example off (attributes and multiple lines).