Javascript  
 
  ScriptJava  
 
  Perl  
 
  PHP  
 
  ROR  
 
  ask us  
 

 

Applications
  1. Getting started
  2. Definitions
  3. First program
  4. Characters and Strings
  5. Object and methods
  6. Arrays and Circles
  7. Exceptions
  8. The main method
  9. Reading
  10. Writing file
  11. Vectors
  12. Stacks
  13. Map The Dictionary
  14. Lists
  15. Linked lists
  16. Collection
  17. Interfaces
  18. Scanner
  19. StringTokenizer
  20. Generics
  21. JDBC
  22. DataBase Queries
  23. JSP, The main step

Graphics

Applets
  1. Regards
  2. One Picture
  3. Calculator
  4. Random pictures
  5. Bouncing picture

Swings
  1. Buttons listeners
  2. TextFields
  3. Swing Example

JavaBeans
  1. The first step
  2. Example

Search a word:
   



© The scientific sentence. 2010

Interfaces classes
Comparable & Enumeration Interfaces

Interface Comparable

1. Introduction
In C++ language, an inheritence can come from many super classes. In 
java, an inheritence comes from only one class. The other classes needed 
to use are just implemented. A class in java extends from a supper class 
(mother class) and implements from an interface. The methods of an interface 
are always identified as abstract.
In the following example related to file classes, RandomAccessFile extends 
from java.io class and implements DataOutput and DataInput.

public interface java.io.DataInput {
	public abstract char readChar();
	public abstract Double readDouble();
	public abstract String readLine();
	public abstract  ....
	}

public interface java.io.DataOutput{
	public abstract void writeInt (int AnInteger);
	public abstract void writeChar (int AnInteger);
	public abstract void writeDouble (double Number);
	public abstract void writeChars (String str);
	public abstract  ....
	}

public class java.io.RandomAccessFile extends java.lang.Object
implements java.io.DataOutput, java.io.DataInput{
	public final char readChar();
	public final double readDouble();
	public final String readLine();
	public final void writeChar(int AnInteger);
	public final void writeDouble (double Nomber);
	public final void writeChars (String str);
	public final ....
	}
	
The following figure represents an example of interfaces 
related to the access of files:
	

In the following two examples, we will use Comparable interface 
that implement a classe rectangle in order to compare two rectangles 
(two objets); and Enumeration interface to broke sentences by the 
StringTokenizer.

2. The related program

class Rectangle implements Comparable { 
private int width;
private int height ;
	
public Rectangle(int width, int height) { constructor
this.width = width; 
this.height = height;
}

public Rectangle() {//just another constructor
this (0,0) ; // by default, width = height = 0
}

public Rectangle (int side) {//just another constructor
this (side, side);
}
	
public Rectangle( Rectangle TheOther) {//another constructor
this (TheOther.width, TheOther.height);
}

public int Perimeter (){//method to retur perimeter values
return 2 * (width + height);
}

// Define again toString() of the object
public String toString() {
return "\n\tRectangle : width = " + width + ", height = " + 
height + ", Perimeter = " + Perimeter();
}

// Here is the the method of the interface Comparable
public boolean isTheSmallest (Object TheObject){
return (this.Perimeter() <( (Rectangle)TheObject).Perimeter());
}

// Method to sort by swapping
public static void toSort  (Comparable  [] Table){
Comparable tempoElement;
	for(int j = 0; j< Table.length -1; j++){
	int IndexMin = j;
	for (int k = j+1; k< Table.length ; k++)
	if(Table[k].isTheSmallest(Table [IndexMin]))
	IndexMin  = k;
	if(IndexMin  !=j){
		tempoElement = Table[j];
		Table[j] = Table[IndexMin];
		Table[IndexMin] = tempoElement;
		}
	}
}

}//end class Rectangle


//----------------------------------------

interface Comparable{
boolean isTheSmallest (Object TheObject);
}
//----------------------------------------


public class RectComparable {

static void Display (Object [] TheObject, String info){
System.out.println(info);
for (int i = 0; i< TheObject.length; i++)
System.out.println(TheObject[i].toString());
System.out.println();
}

public static void main (String [] args){
Rectangle Rect [] = {new Rectangle (1,2), new Rectangle (7,10), 
new Rectangle (3,4), new Rectangle (12,13)};
Display (Rect, "\n\tUnsorted rectangles according to their primeters: ");
Rectangle.toSort(Rect);
Display (Rect, "\n\tSorted rectangles according to their primeters: ");
}
}//end of RectComparable class

//----------------------------------------

II. Interface Enumeration

/**
1. Introduction
The StringTokenizer class allows an application to break a string into tokens.
It implements from the inteface Enumeration of java.util. We have the following 
methods:
public interface java.util.Enumeration{
public abstract boolean hasMoreElements();
public abstract Object nextElement();
}
and:
public class java.util.StringKokenizer 
extends java.lang.Object implements java.util.Enumeration, 
with the constructors: 
public StringKokenizer (String str);
public StringKokenizer (String str, String delimiter);

and the methods:
public int countTokens();
public boolean hasMoreElements();
public boolean hasMoreTokens();
public Object nexElement();
public String nextToken();
public String nextToken(String delimiter);

The Object method toString(); is defined again in the class String 
as well as in the class Integr, but not in the class StringKokenizer.

StringKokenizer  TheString = new StringKokenizer ("All the best of luck"); 
gives with System.out.println (TheString); a wrong result, but:

while (TheString.hasMoreElements()){System.out.printl( TheString.nextElement())
}
gives the right output.

2. Tests:
*/

import java.util.*;
abstract class TokenTest implements Enumeration{
public static void main (String [] args){
StringTokenizer  TheString = new StringTokenizer ("All the best of luck"); 
System.out.println ("\n\t" + TheString);
System.out.println ();
while (TheString.hasMoreElements()){
	System.out.print("\n\t" + TheString.nextElement() + " ");
	}
		System.out.println ();
}
}

/*
The output:

C:\Java>javac TokenTest.java
C:\Java>java TokenTest

        java.util.StringTokenizer@3e25a5

        All
        the
        best
        of
        luck

C:\Java>
*/


  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.