Sample JAVA Programs
Sample JAVA Programs
Sample JAVA Programs
edu/~xwu/Java/SamplePr
ograms.shtml
Java Sample Programs
Fall 2002
A Java Class
class Point {
public double x, y;
public static Point origin = new Point(0,0);
// This always refers to an object at (0,0)
Point(double x_value, double y_value) {
x = x_value;
y = y_value;
}
public void clear() {
this.x = 0;
this.y = 0;
}
public double distance(Point that) {
double xDiff = x - that.x;
double yDiff = y - that.y;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
}
Interfaces
interface Lookup {
/** Return the value associated with the name, or
* null if there is no such value */
Object find(String name);
}
https://www2.cs.uic.edu/~sloan/CLASSES/java/
To create a Java program, you must ensure that the name of the class in the
file is the same as the name of the file (and that the file has the extension
.java).
To compile the program use the command javac as in:
javac HelloWorld.java
This will take the source code in the file HelloWorld.java (which better contain
the class HelloWorld) and create the Java ByteCode in a file HelloWorld.class.
Note that you do not use the any file extension in this command.
HelloWorld.java
HelloDate.java
FunctionCall.java
Point2d.java
Test jig for Point2d class (PointTester.java)
Point3d.java
KeyboardIntegerReader.java
KeyboardReader.java
MyFileReader.java
MyFileReader.txt
MyFileWriter.java
MyFileWriter.txt
HelloWorldException.java
KeyboardReaderError.java
DivBy0.java
Contact the UIC Student Chapter of the ACM for additional help. The ACM Office is in 1309
SEO and you can set up some tutoring by sending email to [email protected] or by
going to the ACM Tutoring Web Page