Popular Posts

Monday, February 23, 2009

INTERFACES EXAMPLES

package interfaceexercise;

// Define an interface with three abstract methods.
// Any class that implements this interface has to
// implement all three methods.
public interface Relation {
public boolean isGreater( Object a, Object b);
public boolean isLess( Object a, Object b);
public boolean isEqual( Object a, Object b);
}
Code-11.5.a: Relation.java

package interfaceexercise;

public class Main {
public static void main( String[] args ) {
Relation x = new Relation();
}
}
Code-11.5.b: Main.java

package interfaceexercise;

// The Line class is a concrete class that implements the
// Relation interface.
public class Line implements Relation {
private double x1;
private double x2;
private double y1;
private double y2;

// Constructor methoe of the Line class.
public Line(double x1,double x2,double y1,double y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}

// A new method definition of the Line class
public double getLength(){
double length = Math.sqrt( (x2-x1)*(x2-x1) +
(y2-y1)*(y2-y1));
return length;
}

// Implement isGreater(..) method defined in the Relation interface
public boolean isGreater( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen > bLen);
}

// Implement isLess(..) method defined in the Relation interface
public boolean isLess( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen < bLen);
}

// Implement isEqual(..) method defined in the Relation interface
public boolean isEqual( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen == bLen);
}
}
Code-11.5.c: Line.java

package interfaceexercise;

public class Main {
public static void main( String[] args ) {

// Create two Line object instances.
Line line1 = new Line(1.0, 2.0, 1.0, 2.0);
Line line2 = new Line(2.0, 3.0, 2.0, 3.0);

boolean b1 = line1.isGreater(line1, line2);
System.out.println("line1 is greater than line2: " + b1);
boolean b2 = line1.isEqual(line1, line2);
System.out.println("line1 is equal with line2: " + b2);

// Note that the line3 is object instance of Line type.
// Because the Line type is also a type of Relation,
// the line3 variable can be declared as Relation type.
// This is a very very important concept you need to understand.
Relation line3 = new Line(1.0, 5.0, 1.0, 5.0);
boolean b3 = line3.isEqual(line1, line3);
System.out.println("line1 is equal with line3: " + b3);

System.out.println("Length of line1 is " + line1.getLength());
System.out.println("Length of line2 is " + line2.getLength());

// The following line of code will generate a compile error since line3
// is declared as Relation interface type not Line type
// and the getLength() method is not one of the methods defined
// in the Relation interface. It is commented out for now,
// System.out.println("Length of line3 is " + line3.getLength());

}
}
Code-11.5.d: Main.java

Exercise 11.5: Interfaces

Steps to follow:

1. Write Relation.java as shown in Code-11.5.a below. Please note that the Relation is an interface. (You are welcome to do this work using either command line tools or NetBeans. The instruction here is given using command line tools. In general, using NetBeans is highly recommended.)

* cd \myjavaprograms
* mkdir interfaceexercise
* jedit interfaceexercise\Relation.java
2. Write Main.java as shown in Code-11.5.b below.

* cd \myjavaprograms (if you are not in this directory already)
* jedit interfaceexercise\Main.java
3. Compile Relation.java and Main.java. (You will experience a compile error as described in the subsequent step.)

* cd \myjavaprograms (if you are not in this directory already)
* javac interfaceexercise\Relation.java interfaceexercise\Main.java

4. Note that you will experience a compile error as shown below. This is because you cannot create an object instance from an Interface.

* C:\myjavaprograms>javac interfaceexercise\Relation.java interfaceexercise\Main.java
interfaceexercise\Main.java:5: interfaceexercise.Relation is abstract; cannot be instantiated
Relation x = new Relation();
^
1 error

5. Write Line.java as shown in Code-11.5.c below. It contains a concrete implementation class that implements Relation interface. Note that the all the methods defined in the Relation interface is now implemented.

* cd \myjavaprograms (if you are not in this directory already)
* jedit interfaceexercise\Line.java
6. Rewrite Main.java as shown in Code-11.5.d below.

* cd \myjavaprograms (if you are not in this directory already)
* jedit interfaceexercise\Main.java
7. Compile and run the code using a directory structure.

* cd \myjavaprograms (if you are not in this directory already)
* javac interfaceexercise\*.java
* java -classpath . interfaceexercise.Main

8. Verify the result as following:

* C:\myjavaprograms>java -classpath . interfaceexercise.Main
line1 is greater than line2: false
line1 is equal with line2: true
line1 is equal with line3: false
Length of line1 is 1.4142135623730951
Length of line2 is 1.4142135623730951

Exercise 12.1: Exception Handling

Introduction:

If you are new to Exception handling, please read "Exception Handling Statements" section of the Java Progamming Tutorial.

Steps to follow:

1. Write ExceptionExample.java as shown in Code-12.1 below. (You are welcome to do this work using either command line tools or NetBeans. The instruction here is given using command line tools. In general, using NetBeans is highly recommended.)

* cd \myjavaprograms
* mkdir exceptionexercise (to match the packaging structure)
* jedit exceptionexercise\ExceptionExample.java
EXAMPLES
package exceptionexercise;

public class ExceptionExample {
public static void main( String[] args ){

try{
System.out.println( args[1] );
} catch( ArrayIndexOutOfBoundsException exp ){
System.out.println("Exception caught! The exception is " + exp);
}
}
}
2. Compile and run the code using a directory structure.

* cd \myjavaprograms (if you are not in this directory already)
* javac exceptionexercise\*.java
* java -classpath . exceptionexercise.ExceptionExample

3. Verify the result

* C:\myjavaprograms>java -classpath . exceptionexercise.ExceptionExample
Exception caught!