Key Ideas
[Part 4 of 11]
There are three key ideas in this part:
- The first is that we can make methods that are passed information, and/or return information:
public class Point {
private double x = 0.0;
public void incrementX() {
x++;
}
public void setX (double xIn) {
x = xIn;
}
public double getX() {
return x;
}
public double addToX(double addendum) {
x = x + addendum;
return x;
}
}
public class Test {
public static void main (String args[]) {
Point pt1 = new Point();
pt1.incrementX();
pt1.setX(10);
System.out.println(pt1.getX());
double ans = pt1.addToX(20.0);
}
}
- The second is that both methods and variables can be static. This most notably
causes issues with the main class, the simplest solution to which is to pull the following
slight of hand (actually against yourself, more than the JVM!):
public class MainTemplate {
// Note that we pass the args array on, just incase you want it.
public MainTemplate(String args[]) {
IntStorage store = new IntStorage();
store.setData(10);
System.out.println(store.getData());
}
public static void main (String args[]) {
// Note that we don't label the object, as we'll never need to
// refer to it again.
new MainTemplate(args);
}
}
- Finally, it is worth noting that one way in which static is used is to set
up toolkits of static methods and static final variables. These often have their
constructors set to private so that you have to use the methods directly from the class, you
can't turn the classes into objects.
// Code to fill a 2D array with random ints between 0 and 99.
int[][] array1 = new int[300][200];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
array1[i][j] = (int) (Math.random() * 100.0);
}
}
Here's some code which uses these ideas to create a better version of the code from the first quiz. It makes 100 random points in an sub-area of a larger map. Can you work out how it works by following the code execution through? If you understand all this, you're doing very well indeed!
public class GIS {
public static void main (String args[]) {
Point[] points = new Point[100];
for (int i = 0; i < points.length; i++) {
do {
System.out.println("Trying...");
points[i] = PointMaker.makePointWithin(100,100,300,300);
} while (points[i] == null);
System.out.println("Point made, x = " + points[i].getX() + " y = " + points[i].getY());
}
}
}
public class PointMaker {
// Why do you think these variables need to be static?
private static double minX = 0.0;
private static double maxX = 1000.0;
private static double minY = 0.0;
private static double maxY = 1000.0;
private PointMaker() {}
public static Point makePointWithin(double left, double bottom, double right, double top) {
double x = (Math.random() * (maxX - minX)) + minX;
double y = (Math.random() * (maxY - minY)) + minY;
if (((x > left) && (x < right)) &&
((y > bottom) && (y < top))) {
return new Point(x, y);
} else {
return null;
}
}
}
public class Point {
private double x = 0.0;
private double y = 0.0;
public Point(double xIn, double yIn) {
x = xIn;
y = yIn;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
NB: The variables in PointMaker need to be static
, because they are used in the static method makePointWithin
.