Key Ideas
[Part 6 of 11]
There are three key ideas in this part:
- The first is that we can import code to work for us, either by making it into an object, or by extending (or implementing) it.
The code below both extends and instantiates imported code:
import javax.swing.*;
import java.awt.*;
public class HelloFrame extends JFrame {
public HelloFrame () {
super("HelloWorld!");
setSize(300,300);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Label hwLabel = new Label("Hello World");
add(hwLabel);
setVisible(true);
}
public static void main (String args[]) {
new HelloFrame();
}
}
- The second is that we need to know where to compile and run from:
- If we are just using
java.lang
code, likeSystem
orMath
, we don't need to import it, or compile from anywhere special. - If we're using other core packages, like
javax.swing
orjava.awt
in the above, we need to import them, but we don't need to compile from anywhere special. - If, on the other hand, we are using other non-JDK code, we need to compile and run from the base of the package hierarchy. Usually this will mean something like:
javac us/gov/emperors/*.java
java us.gov.emperors.Norton
Note that if your code is unpackaged this can become quite complex, as the classpath must include where your code is running from, and the root of the hierarchy. Assuming you are in the latter, you'll need this kind of thing:
javac unpackaged/lazyCode/*.java
java -classpath unpackaged/lazyCode/;. MyClass
What you CAN'T do is this:
java unpackaged/lazyCode/MyClass
The JVM will just assume you're trying to run from a package.
- If we are just using
- Thirdly, you should always document your code. At a minimum, this should include a comment block above the class, and one above every method.
Here's some code which uses these ideas. For reference, to get this links to the standard classes to work, the code was converted using this command:
javadoc -private -author -version -link http://docs.oracle.com/javase/8/docs/api/ HeightPoint.javaAnd generated this set of docs.
import java.awt.Point;
/**
* Class to extend the standard java.awt.Point with a z variable for height.
*
* The standard Point class built into java is suitable for 2D work only.
* This class extends it to provide extra functionality in the way of height.
*
* @author Dr Andy Evans
* @version 1.0
* @see java.awt.Point java.awt.Point
*
**/
public class HeightPoint extends Point {
/**
* The new variable value.
**/
private int z = 0;
/**
* Default constructor.
**/
public HeightPoint () {
super();
}
/**
* Extra constructor to deal with height.
* @see java.awt.Point#Point(int,int) java.awt.Point#Point(int,int)
* @param x : x coordinate value
* @param y : y coordinate value
* @param z : height value
**/
public HeightPoint(int x, int y, int z) {
super(x, y);
this.z = z;
}
/**
* Mutator method for height.
* @param z : height value
**/
public void setZ(int z) {
this.z = z;
}
/**
* Accessor method for height.
* @return z : height value
**/
public int getZ() {
return z;
}
/**
* Gives a String representation of the object.
* Outputs "x = " + x + " y = " + y + " z = " + z.
**/
public String toString() {
return "x = " + x + " y = " + y + " z = " + z;
}
}