|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help |
I'll use a few short examples to introduce and illustrate some Java features.
class Program1 { public static void main(String[] args) { System.out.println("99 euros = " + 99. *(61./100.) + " pounds"); } }It has a root class which prints out 2 strings and the result of a calculation by calling the println method (in Java, functions are called methods). Note that the method has a long name which shows where it comes from. println belongs to out which belongs to System (which in turn is part of the java.lang package, which is implicitly imported - more on this later). Java has many routines but because they're organised in a hierarchy, sorting through the documentation isn't too hard - the name of the method shows you where to look.
To execute this program it first needs to be compiled into a byte-code file. The Java compiler, javac, takes your source file and translates its text into the native language of the Java Virtual
Machine.
javac Program1.java
This produces a file called Program1.class which can be fed to the Java interpreter by typing
java Program1
Note the addition of throws Exception. Without this the program
won't compile. Exceptions in Java and C++ are a way of dealing with
errors. Java knows that the file-handling commands might come up against
problems (it might be impossible to create files, for example) and
insists that the program recognises this possibility.
import java.io.*;
class Program2 {
public static void main(String[] args) throws Exception {
PrintStream pstream;
FileOutputStream fstream;
File myfile;
myfile= new File("abcde");
fstream = new FileOutputStream(myfile);
pstream = new PrintStream(fstream);
pstream.println("hello");
}
}
import java.io.*; class Program3 { public static void main(String[] args) throws Exception { PrintStream pstream; FileOutputStream filestream; File myfile; try { myfile= new File("/impossible"); filestream = new FileOutputStream(myfile); pstream = new PrintStream(filestream); pstream.println("hello"); } catch(Exception e) { System.err.println("An error has happened"); } } }
import java.applet.Applet; import java.awt.Graphics; public class Program4 extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }Note that we use extends to mean that Program4 is going to be derived from Applet (whose definition we've imported). We're also importing the Abstract Window Toolkit (awt) to do graphics. For now, we'll gloss over the details. To make this work you need to compile it, put it somewhere on the WWW then write a page including HTML code similar to what's below (with the codebase appropriately set). Here's how to run my (tpl's) copy
<applet codebase="http://www2.eng.cam.ac.uk/~tpl/javateaching" code="Program2.class" width=400 height=75 align=center > <param name="text" value="This is a demo."> <blockquote> <hr> If you were using a Java-enabled browser, you wouldn't see this paragraph. <hr> </blockquote> </applet>
import java.util.Properties; class Program5 { public static void main(String[] args) { Properties properties; properties=System.getProperties(); properties.list(System.out); } }
import java.util.*; public class SupervisionAppointments extends Observable { private String message; public String getMessage() { return message; } public void changeMessage(String message) { this.message = message; setChanged(); notifyObservers(message); } public static void main (String [] args ) { SupervisionAppointments appointments = new SupervisionAppointments(); Student janet = new Student(); Student john = new Student(); appointments.addObserver(janet); appointments.addObserver(john); appointments.changeMessage("Friday meeting cancelled"); } } // an observer is expected to have an appropriate "update" routine. // "Observer" is an "interface" class Student implements Observer { public void update(Observable o, Object arg) { System.out.println("appointment changed! " + arg); } }
class Program7 { public static void main(String[] args) { int i=1, j=3; System.out.println("i+j="+i+j); System.out.println("i+j="+(i+j)); } }
| computing help | |