Uipoka’s Weblog

Archive for the ‘Programming’ Category

Java Packages

with 2 comments

Java Packages
Java package is a collection of java classes, interfaces and subpackages. Its an encapsualtion mechanism that can be used to group related classes, interfaces and subpackages.


From the figure there is a package called rishan contains two other packages: maiden and heaven.The maiden has a class called Metallica that implements an interface called IronMaiden, also found in the same package.In addition, the package maiden has a class called DreamOfMirrors and a subpackage called virtualXI containing a class called ComoEstais. The package heaven has two classes called DreamOfMirrors and BloodBrothers. The class BloodBrothers is a subclass of the class ComoEstais which is found in the subpackage of virtualXI in the package maiden.

Ok the introduction of the package hierarchy is complete.Now lets see how can we define them in our codes.The dot(.) notation is used to uniquely identify package members in the package hierarchy. The class rishan.maiden.DreamOfMirrors are different from the class rishan.heaven.DreamOfMirrors. Most important thing is this figure tells the organization of packages in conceptually and java source code is of no consequences in this regard.The java source code can contain zero of more definitions of classes and interfaces, but the compiler produces a separate class file containig the Java Bytecode for each of them . In java code package declaration tells the compiler that the bytecode be placed in a particular package.

There are two keywords used for package control:
import : Statement to import the java packages or classes into the code.
package: Specifies to which package all classes in a source file belong.

Package declaration:
package [package name];
This declaration must be the first statement in the sourcecode file.
Code example:

package rishan.maiden;

Import declaration:
import [type name]
This declaration is for single class or interface.
import [package name].*;
This declaration import all the classes and interfaces from the specified package.
Import declaration must be the first statement after any package declaration.
Code example:

import rishan.maiden.DreamOfMirrors;
import rishan.maiden.*;

Our working directory is: D:\rishan’s treasure>_

For the DreamOfMirrors class in package rishan.maiden here goes the sample code:
DreamOfMirrors.java

package rishan.maiden;

public class DreamOfMirrors {

	public static void main(String [] ui){
		System.out.println("This is the main class for class Dream of mirrors");	
	}    
}

Compile it using the command: D:\rishan’s treasure>javac DreamOfMirrors.java

Compiler create a bytecode file named DreamOfMirrors.class in the current directory.

Execute the command: D:\rishan’s treasure>java rishan.maiden.DreamOfMirrors

This error occurs:

Exception in thread “main” java.lang.NoClassDefFoundError: rishan/maiden/DreamOfMirrors

What happened here? Java command search the class file from this path ./rishan/maiden/DreamOfMirrors.class.
But there is no rishan folder in the current directory and so for maiden. First we would do that by hand to hand process.Make a directory named rishan in the current directory, then make a directory in rishan named maiden and then copy the class file DreamOfMirrors.class to maiden folder
and run the command:

D:\rishan’s treasure>java rishan.maiden.DreamOfMirrors

and get this output:

This is the main class for class Dream of mirrors

Ahh… at last we have done it successfully .

If you go to ./rishan/maiden and try to run the DreamOfMirrors then again you will get Exception.

There is a way to create the package folders in the time of compilation:

D:\rishan’s treasure>javac -d . DreamOfMirrors.java

In this command –d for destination and dot(.) is for current directory.This command will create ./rishan/maiden (and any other subdirectories if required) under the current directory and place the byte code for all the classes and interfaces in the directories corresponding to the package names.

Lets see the sample codes :
IronMaiden.java

package rishan.maiden;

public interface IronMaiden {
    void sing();
    void concert();
}

Metallica.java

package rishan.maiden;

public class Metallica implements IronMaiden{
    
    public void sing(){
    }
    
    public void concert(){	
    }   
}

ComoEstais.java

package rishan.maiden.virtualXI;

public class ComoEstais {

    public void hello(){
    	System.out.println("Hello method from Como Estais class");
    }
    
    public static void hi(){
    	System.out.println("Como Esatais Amigo :)");
    }
    
} 

BloodBrothers.java

package rishan.heaven;

import rishan.maiden.virtualXI.*;

public class BloodBrothers extends ComoEstais{
	
	    public static void main(String [] ui){
	    	rishan.maiden.virtualXI.ComoEstais.hi();			                  //Full class name is must
	    	rishan.heaven.BloodBrothers obj = new rishan.heaven.BloodBrothers(); //Full class name is must
	    	obj.hello();
	    }
    
}

Use this command:

D:\rishan’s treasure>javac -d . *.java

After successful exucution of the previous command use this command:

D:\rishan’s treasure>java rishan.heaven.BloodBrothers


Output:

Como Esatais Amigo 🙂
Hello method from Como Estais class

If you have any question then leave a comment, i will answer…………….

Written by uipoka

August 22, 2008 at 6:03 am

Posted in Java

Dealing with queue

with 3 comments

A queue is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure.Queue is a linear data structure.

Codes:

Interface

public interface InterQueue {
	boolean isEmpty();
    	void enqueue(Object x);
    	Object dequeue();
}

Basic class:

public class RishanQueue {

    public RishanQueue() {
    	theArray = new Object[DEFAULT_CAPACITY];
    	head = 0;
    	tail = 0;
    }
    
    public boolean isEmpty(){
    	return queueSize==0;
    }
    
    public void enqueue(Object x){
    	if(queueSize==theArray.length)throw new RishanException("Enqueue");
    	theArray[tail] = x;
    	queueSize++;
    	if(queueSize<theArray.length && tail+1 == theArray.length){
    		tail = 0;
    	}else tail++;
    }
    
    public Object dequeue(){
    	if(isEmpty())throw new RishanException("Dequeue");
    	Object x = theArray&#91;head&#93;;
    	queueSize--;
    	if(++head==theArray.length)
    		head = 0;
    	return x;
    }
      
    private final int DEFAULT_CAPACITY = 10;
    private int &#91;&#93; theArray;
    private int head=0;
    private int tail=0;
    private int queueSize=0;
    
}
&#91;/sourcecode&#93;

<strong>Exception class:</strong>


public class RishanException extends java.io.RuntimeException {
    public RishanException(String msg) {
        super(msg);
    }
}

In this implementation i used a fixed length of array but if anyone we want to increase out queue size
then we can use following method:

private void doubleQueue( )
{
Object [ ] dummy;
dummy = new Object[ theArray.length * 2 ];
for(int i=0;i

Written by uipoka

August 19, 2008 at 3:24 am

Posted in Data Structures, Java

Stack Implementation Using Java

with 2 comments

Stack
Stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used in the basic design of an operating system for interrupt handling and operating system function calls. Stacks are used to run a Java Virtual Machine. The stack is ubiquitous.
First i write an interface for implementing stack:

public interface Stacks {
	void push(Object x);
	void pop();
	Object top();
	Object topAndPop();
	boolean isEmpty();
	void makeEmpty();
}

Now look at the RishanStack class which implements the interface stacks


public class RishanStack implements Stacks{

	private static final int DEFAULT_CAPACITY = 10;
	private Object [] theArray; 
	private int topOfStack;
	
    public RishanStack() {
    	theArray = new Object[DEFAULT_CAPACITY];
    	topOfStack = -1;	
    }
    
    public boolean isEmpty(){
    	return topOfStack == -1;
    }
    
    public void makeEmpty(){
    	topOfStack = -1;	
    }
    
    public void push(Object x){
    	if(theArray.length==topOfStack+1)increaseSize();
    	theArray[++topOfStack]=x;
    }
    
    public Object top(){
    	if(isEmpty())throw new RishanStackException("Stack top");
    	return theArray[topOfStack];
    }
    
    public void pop(){
    	if(isEmpty())throw new RishanStackException("Stack pop");
    	topOfStack--;
    }
    
    public Object topAndPop(){
    	if(isEmpty())throw new RishanStackException("Stack top and pop");
    	return theArray[topOfStack--];
    }
    
    public void status(){
    	for(int i=0;i<theArray.length;i++){
    		System.out.println(theArray&#91;i&#93;);
    	}
    }
    
    public void increaseSize(){
    	Object &#91;&#93; dummy = new Object&#91;theArray.length*2&#93;;
    	for(int i=0;i<theArray.length;i++){
    		dummy&#91;i&#93;=theArray&#91;i&#93;;
    	}
    	theArray = dummy;
    }
}
&#91;/sourcecode&#93;

And lastly the RishanStackException class which extends <em>java.io.RuntimeException</em>
and overridden constructor:

public class RishanStackException extends RuntimeException{
    public RishanStackException(String msg) {
    	super(msg);
    }
}

Written by uipoka

August 13, 2008 at 7:18 pm