Wednesday, October 26, 2011

Call by reference program in java

package org.learn.callbyreference;

public class CallByReferenceTest {

    String val = new String("Test");

    public static void main(String[] args) {
        CallByReferenceTest cTest = new CallByReferenceTest();
       
         String s= new String("test this string");
         cTest.changeString(s);
         System.out.println(s);
        

        System.out.println(cTest.val);
        cTest.chagngeVal("Test1111");
        System.out.println(cTest.val);

    }

    public void changeString(String s) {
        s = "test 89898989";
    }

    public void chagngeVal(String s) {
        val = "Test43323243242";
    }

}

Java program on Command design pattern


Save each class in a separate file with the class name and execute the Main.java program. Some classes are not used in the main execution. In later versions of this design I will include them.

----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

//This class(Main.java) is client part of command design pattern
public class Main {

    public static void main(String args[]) {
        CommandManager mgr = new CommandManager();
        // CommandManager is Invoker part of command design pattern
        Command tomcat = new TomcatStartCommand();
        // In TomcatStartCommand class I used Tomcat class. Here Tomcat class is
        // the receiver part of command design pattern
        Command serviceMix = new ServiceMixStartCommand();
        mgr.addCommand(tomcat);
        mgr.addCommand(serviceMix);
        mgr.runCommands();
    }

}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public interface Command {
   
    public void execute();

}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public class CommandException extends Exception {
   
    private String message;
   
    public CommandException(String message){
        super(message);
        this.message=message;
    }
   
    public CommandException(){
        super();
    }
   
    public void setMessage(String message){
        this.message = message;
    }
   
    public String getMessage(){
        return message;
    }
   
}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

import java.util.ArrayList;

public class CommandManager {

    private List<Command> commandList = new ArrayList();

    public void runCommands() {
        for (Command c : commandList) {
            c.execute();
        }
    }

    public void addCommand(Command command) {
        commandList.add(command);
    }
}

class TomcatStartCommand implements Command {

    Tomcat tomcat = new Tomcat();

    @Override
    public void execute() {
        tomcat.startTomcat();
    }
}

class TomcatStopCommand implements Command {

    Tomcat tomcat = new Tomcat();

    @Override
    public void execute() {
        tomcat.stopTomcat();
    }
}

class ServiceMixStopCommand implements Command {
    ServiceMix serviceMix = new ServiceMix();

    @Override
    public void execute() {
        serviceMix.stopServiceMix();
    }

}

class ServiceMixStartCommand implements Command {
    ServiceMix serviceMix = new ServiceMix();

    @Override
    public void execute() {
        serviceMix.startServiceMix();
    }

}
----------------------------------------------------------------------------------------------------------


package org.learn.designpatterns.command;

public class ServiceMix {
   
    public boolean stopServiceMix(){
        System.out.println("Service Mix is stopped");
        return true;
    }
   
    public boolean startServiceMix(){
        System.out.println("Service Mix is started");
        return true;
    }

}
------------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public class Status {
    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
   
   
}
------------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;

public class Tomcat {
   
    public boolean stopTomcat(){
        System.out.println("Tomcat Stopped");
        return true;
    }
   
    public boolean startTomcat(){
        System.out.println("Tomcat Started");
        return true;
    }

}
------------------------------------------------------------------------------------------------------------

Simple Binary Tree implementaion in java

package org.learn.datastructures.tree.binary;

public class BinaryTree {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        Node rootNode = new Node(10);
        tree.insert(rootNode, 20);
        tree.insert(rootNode, 30);
        tree.insert(rootNode, 5);
        tree.insert(rootNode, 6);
        tree.insert(rootNode, 16);
        tree.insert(rootNode, 67);
        tree.insert(rootNode, 116);
        tree.search(rootNode);
    }

    public void insert(Node node, int data) {
        if (node.data < data) {
            if (node.left != null)
                insert(node.left, data);
            else
                node.left = new Node(data);
        } else if (node.data > data) {
            if (node.right != null)
                insert(node.right, data);
            else
                node.right = new Node(data);
        }
    }

    public void search(Node node) {
        if (node == null)
            return;
        search(node.right);
        System.out.println(node.data);
        search(node.left);
    }

}

class Node {
    Node left;
    Node right;
    int data;

    public Node(int data) {
        this.data = data;
    }
}

A simple compression program in java

package org.learn.compression;

public class Compression {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String toBeCompressed = "aaaaaabbbbbcccce4uiiiioookkkkkkkkkkkkkkkkkkkkkkkk";
        String compressed = compress(toBeCompressed);
       
        System.out.println("String to Compressed="+toBeCompressed + ", length="+toBeCompressed.length());
        System.out.println("Compressed String ="+compressed+ ", length="+compressed.length());
    }
   
    public static String compress(String str){
        String compressedStr="";
        String countChar="";
        int index=0;
        int strLength = str.length();
        while(true){
            char c = str.charAt(index);
            int count=1;
            int temp=index;
            while(true){
                countChar="";
                ++temp;
                if( temp<strLength && c == str.charAt(temp)){
                    count++;
                }else{
                    countChar = c + ""+count;
                    break;
                }
            }
            compressedStr = compressedStr + countChar;
            index+=count;
            if(index>=strLength)
                break;
        }
        return compressedStr;
    }

}

Stack implementation in java

package org.learn.datastructures.stack;

/*
 * A items is an array in which elements can be retrieved and stored in Last in First Out Order
 */
public class Stack {

    private String[] items = null;
    private int top = -1;

    public Stack(int size) {
        items = new String[size];
    }

    public void pop() {
        items[top] = null;
        top--;
    }

    public void push(String element) {
        items[++top] = element;
    }

    public void printItems() {
        if (top == -1) {
            System.out.println("\nStack is empty");
        } else {
            for (int x = 0; x <= top; x++) {
                System.out.print("items[" + x + "]=" + items[x]);
                System.out.print(",");
            }
        }

    }

    public void size() {
        System.out.println("items Size=" + (top + 1));
    }

    public void empty() {
        top = -1;
    }

    public static void main(String args[]) {
        Stack s = new Stack(10);
        s.push("A");
        s.push("B");
        s.push("C");
        s.size();
        System.out.println("Before the pop the items elements are");
        s.printItems();
        s.pop();
        System.out.println("\nAfter the pop the items elements are");
        s.size();
        s.printItems();
        s.empty();
        s.printItems();
    }
}

Simple LinkedList implementation in java without iterator

package org.learn.collections.list.linkedlist;

/* a linked list is a data structure consisting of a group of nodes which together represent a sequence.
 * Under the simplest form, each node is composed of a datum and a reference (in other words, a link)
 * to the next node in the sequence; more complex variants add additional links.
 * This structure allows for efficient insertion or removal of elements from any position in the sequence.
 * But reading a particular node in a list is time consuming action because it has to travel all previous
 * nodes of the target node.
 *
*/

/*
 * This code is working perfectly without any issues. Need to handle the null pointer exceptions only.
 */
public class LinkedList {

    Node header;

    public LinkedList(int data) {
        header = new Node(data);
    }

    public void insert(Node n) {

        if (header == null) {
            header = n;
        } else {
            Node node = header;
            while (node.next != null) {
                node = node.next;
            }

            node.next = n;

        }
    }

    public void insertAt(int pos, Node newNode) {

        Node temp = null;
        if (pos == 1) {
            newNode.next = header;
            header = newNode;
        } else if (pos > 1) {
            Node node = header;
            for (int x = 2; x < pos; x++) {
                if (node != null)
                    node = node.next;
            }
            temp = node.next;
            node.next = newNode;
            newNode.next = temp;
        }
    }

    public void printLinkedList() {
        Node node1 = header;
        while (node1 != null) {
            System.out.print(node1.data);
            node1 = node1.next;
            if (node1 != null)
                System.out.print("->");
        }
    }

    public static void main(String[] args) {
        LinkedList ll = new LinkedList(10);
        ll.insert(new Node(12));
        ll.insert(new Node(13));
        ll.insert(new Node(14));
        ll.insertAt(2, new Node(11));
        ll.insertAt(1, new Node(5));
        ll.insertAt(7, new Node(59));
        ll.insertAt(5, new Node(59));
        ll.printLinkedList();
    }

}

class Node {
    int data;
    Node next;
   
    public Node(int data,Node node){
        this.data=data;
        this.next= node;
    }
    public Node(int data){
        this(data,null);
    }
}

Tuesday, October 11, 2011

Installing war support feature in apache-karaf 2.2.2

login into apache-karaf

karaf@root>features:install -v war

This will install war 2.2.2, http 2.2.2 and jetty 7.4.2.v20110526 features.

How to connect to a Dell PowerConnect 6248 Switch

To connect a switch there are two ways.
1. Type the IP address in the browser and it will displays a login page and use the credentials to login.
2. Use putty or windows hyper terminal  use the telnet to connect. In putty select telnet and type the IP of the switch this will asks you the user name and password to login into the switch.

After login into the switch use enable command to get the privilege to execute admin commands.
For example to execute a traceroute command you need privilege so use enable command.

console#script ?
This command gives the sub commands of the script command

console#script list

This command displays the scipt files which are in the switch

console#script show abc.scr

This command shows the contents of abc.scr script file

console#script delete abc.scr
deletes abc.scr file.