Java is a
Java: Simple [243]
Java: Object Oriented [244]
Java: Distributed [245]
Java: Interpreted [246]
Java: Robust [247]
Java: Secure [248]
Java: Architecture Neutral [249]
Java: Portable [249]
Java: High Performance [249]
Java: Multithreaded [250]
Java: Dynamic [250]
Java: State Transistions [251]
Java: Inheritance Example [252]
import java.awt.Color; // package
public abstract class Primitive { // abstract=>cannot make an object instance
public static final int X=100; // class constant
public static final int Y=100;
private int x; // instance variable
private int y;
private String name; // String is class
protected Color color; // let subclasses have access
public Primitive(String name, int x, int y, Color color) { // constructor
this.name = name; // refer to self
this.x = x;
this.y = y;
this.color = color;
}
public void setx(int x) {this.x = x; } // instance method
public void sety(int y) { this.y = y; }
public int getx() { return x; }
public int gety() { return y; }
public String getName() { return name; }
public Color getColor() { return color; }
public void chgAppearance() { color = color.brighter(); }
}
Java: Inheritance Example [253]
import java.awt.Color;
public class Conic extends Primitive { // inheritance
private int a; // ax^2 + bxy + cy^2 + dx + ey + f = 0
private int b;
private int c;
private int d;
private int e;
private int f;
public Conic(String name, int x, int y, Color color,
int a, int b, int c, int d, int e, int f) {
super(name,x,y,color); // call constructor of parent
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
}
Java: Inheritance Example [254]
public class Circle extends Conic { // inheritance
public static final double PI=3.14159; // class constant
public static int num_circles=0; // class variable
public int r; // instance variable
public Circle(String name, int x, int y, Color color, int r) {
super(name,x,y,color,1,0,1,0,0,-r*r); // call constructor of parent
num_circles++; // count number of objects
this.r = r;
}
public Circle(String name, int r) { // multiple constructors
this(name,Primitive.X,Primitive.Y,Color.red,r);
}
public Circle(int r) { this("",r); }
public double circumference() { return 2*PI*r; }
public double area() { return PI*r*r; }
public Circle bigger(Circle c) { // instance method
if (c.r > this.r) return c; else return this; // return myself
}
public static Circle bigger(Circle a, Circle b) { // class method
if (a.r > b.r) return a; else return b;
}
public void chgAppearance() { // overriding method
color = color.darker(); // access protected variable
}
}
Java: Inheritance Example [255]
class Geo {
public static void main(String args[]) { // entry point
Circle a = new Circle("a",10); // object instance of class
Circle b = new Circle("b",20); // another object
Color color;
// access class variable
System.out.println("Number of circles: "+Circle.num_circles);
// access instance method
System.out.println("Circ of a: "+a.circumference()+" Area of b: "+b.area());
// access public instance variable
System.out.println("Radius of a: "+a.r);
// access instance method of grandparent
System.out.println("Location of a: "+a.getx()+","+a.gety());
// access instance method
System.out.println("Biggest area: "+a.bigger(b).area());
// access class method
System.out.println("Biggest area: "+Circle.bigger(a,b).area());
// access instance method of grandparent
System.out.println("Circle with biggest area: "+a.bigger(b).getName());
color = a.getColor();
System.out.println("Color of circle a: "+color.toString());
a.chgAppearance(); // make it darker
color = a.getColor();
System.out.println("Color of circle a: "+color.toString());
} }
Java: Philosopher Demo [256]
class PhilDemo {
public static void main(String args[]) {
String s = new String("A");
Integer fork1 = new Integer(1); Integer fork2 = new Integer(2);
if (args.length > 0) s = args[0];
if (s.equals("A")) {
new PhilA("Philosopher One",4).start();
new PhilA("Philosopher Two",4).start();
} else
if (s.equals("B")) {
new PhilB("Philosopher One",4,fork1,fork2).start();
new PhilB("Philosopher Two",4,fork1,fork2).start();
} else
if (s.equals("C")) {
PhilC phil1 = new PhilC("Philosopher One",4);
PhilC phil2 = new PhilC("Philosopher Two",4);
phil1.setOther(phil2); phil2.setOther(phil1);
phil1.start(); phil2.start();
Thread me = Thread.currentThread(); me.yield();
synchronized(phil1)phil1.notify();
} else
if (s.equals("D")) {
new PhilD("Philosopher One",4,6117,6118).start();
new PhilD("Philosopher Two",4,6118,6117).start();
} } }
Java: Philosopher Sync Method [257]
class PhilA extends Thread { // inheritance
private int steps;
public PhilA (String name, int steps) { // constructor
super(name); // let Thread class save the name
this.steps = steps;
}
public void run() { // start executes the run method
while (steps > 0) {
eat();
steps--;
}
}
static synchronized void eat() { // entire method is critical section
print();
}
public static void print() { // static=>can use without an object
Thread me = Thread.currentThread();
System.out.println(me.getName()+" start eating");
me.yield(); // try to give up the processor
System.out.println(me.getName()+" stop eating");
me.yield();
}
}
Java: Philosopher Sync Block [258]
class PhilB extends Thread {
private int steps;
private Integer fork1; // any object will do
private Integer fork2;
public PhilB (String name, int steps, Integer fork1, Integer fork2) {
super(name);
this.steps = steps;
this.fork1 = fork1;
this.fork2 = fork2;
}
public void run() {
while (steps > 0) {
eat();
steps--;
}
}
void eat() {
synchronized(fork1) { // critical section based on object
synchronized(fork2) {
PhilA.print();
}
}
}
}
Java: Philosopher Wait/Notify [259]
class PhilC extends Thread {
private int steps;
Thread other;
public PhilC (String name, int steps) {
super(name);
this.steps = steps;
}
public void setOther(Thread other) { // remember the other philosopher
this.other = other;
}
public void run() {
while (steps > 0) {
if (getName().equals("Philosopher One"))
eat1();
else
eat2(); // make 2 versions, both synchronized
steps--;
}
}
synchronized void eat1() { // must be synchronized to use wait
try this.wait(); catch(InterruptedException e) System.err.println(e);
PhilA.print();
synchronized(other)other.notify();
}
}
Java: Philosopher Send/Receive [260]
import java.net.*;
class PhilD extends Thread {
int steps;
byte[] inbuf = new byte[1]; byte[] outbuf = new byte[1];
DatagramPacket inpacket; DatagramPacket outpacket;
DatagramSocket mysocket; DatagramSocket yoursocket;
public PhilD(String name, int steps, int myport, int yourport) {
super(name); this.steps = steps;
try { mysocket=new DatagramSocket(myport); yoursocket=new DatagramSocket();
inpacket=new DatagramPacket(inbuf,inbuf.length);
outpacket=new DatagramPacket(outbuf,outbuf.length,
InetAddress.getLocalHost(),yourport);
} catch (Exception e) System.err.println(e);
}
public void run() {
if (getName().equals("Philosopher Two"))
try yoursocket.send(outpacket); catch (Exception e);
while (steps > 0) { eat(); steps--; }
}
void eat() {
try { mysocket.receive(inpacket);
PhilA.print();
yoursocket.send(outpacket);
} catch (Exception e) System.err.println(e);
} }
Java: Exceptions [261]
class MyException extends Exception {
public MyException() { super(); }
public MyException(String s) { super(s); }
}
class MyOtherException extends Exception {
public MyOtherException() { super(); }
public MyOtherException(String s) { super(s); }
}
class MySubException extends MyException {
public MySubException() { super(); }
public MySubException(String s) { super(s); }
}
public class throwtest {
public static void main(String argv[]) { int i;
try { i = Integer.parseInt(argv[0]); }
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Must specify an argument");
return;
}
catch (NumberFormatException e) {
System.out.println("Must specify an integer argument");
return;
}
a(i);
} }
Java: Exceptions [262]
public static void a(int i){
try b(i);
catch (MyException e) {
if (e instanceof MySubException)
System.out.print("MySubException: ");
else
System.out.print("MyException: ");
System.out.println(e.getMessage());
System.out.println("Handled at point 1");
}
}
public static void b(int i) throws MyException {
int result;
try {
System.out.print("i="+i+" ");
result = c(i);
System.out.println("c(i)="+result);
}
catch (MyOtherException e) {
System.out.println("MyOtherException: "+e.getMessage());
System.out.println("Handled at point 2");
}
}
Java: Exceptions [263]
public static int c(int i) throws MyException, MyOtherException {
switch (i) {
case 0 : // processing resumes at point 1 above
throw new MyException("input too low");
case 1 : // processing resumes at point 1 above
throw new MySubException("input still too low");
case 99: // input resumes at point 2 above
throw new MyOtherException("input too high");
default: return i*i;
}
}