public class SelectionSort { public static void main(String[] args) { int[] array = {6,7,3,8,1,2,3,4,5}; new SelectionSort().selectionSort(array); for(int i=0; i < array.length ; i++) System.out.println(array[i]); } public void selectionSort(int[] array ){ int min = 0; int length = array.length; for (int i = 0; i <= length - 2; i++){ min = i; for(int j = i+1 ; j <= length - 1 ; j++){ if(array[min] > array[j]){ min = j; } } if(min != i){ // This if condition is not there in original algorithm. It helps unnecessary swapping of same value in to same location again (min = i) int temp = array[i]; array[i] = array[min]; array[min] = temp; } } } }
Sunday, May 13, 2012
Selection Sort in Java
Bubble Sort in Java
public class BubbleSort { public static void main(String[] args){ int[] array = {1,2,3,4,5}; new BubbleSort().bubbleSort(array); for(int i=0; i < array.length ; i++) System.out.println(array[i]); } public void bubbleSort(int[] array){ int length = array.length; int count = 0; //counts if no swapping is done for(int i = 0 ; i <= length-2 ; i++){ count = 0; for(int j = 0 ; j <= length-i-2 ; j++){ if(array[j] > array[j+1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; count ++ ; } } if(count == 0){ // This if is not there in actual algorithm. If there are no swaps in the pass that means the array is sorted System.out.println("broke on i = " + i); break; } } } }
Insertion Sort in Java
public class InsertionSort { public static void main(String[] args){ int[] array = {6,7,3,8,1,2,3,4,5}; new InsertionSort().insertionSort(array); for(int i=0; i < array.length ; i++) System.out.println(array[i]); } public void insertionSort(int[] array){ for(int j=1; j <= array.length-1; j++){ insert(array, j); } } //insert array[index] into sorted sequence array[0]...array[index-1] private void insert(int[] array, int indexToInsert){ int key = array[indexToInsert]; int i = indexToInsert - 1; while(i >=0 && array[i] > key){ array[i+1] = array[i]; i--; } array[i + 1] = key; } }
Saturday, May 12, 2012
Doubly linked list implementation in java
I used generics here to write this.
public class DLLNode{ private E data = null; private DLLNode prevNode = null; private DLLNode nextNode = null; public E getData() { return data; } public void setData(E data) { this.data = data; } public DLLNode getPrevNode() { return prevNode; } public void setPrevNode(DLLNode prevNode) { this.prevNode = prevNode; } public DLLNode getNextNode() { return nextNode; } public void setNextNode(DLLNode nextNode) { this.nextNode = nextNode; } } public class DoublyLinkedList { private DLLNode headNode = null; private DLLNode tailNode = null; private int nodeCount = 0; public void addFirst(E data){ DLLNode newNode = new DLLNode (); newNode.setData(data); if(headNode == null && tailNode == null){ headNode = tailNode = newNode; //new node becomes both head and tail } else{ newNode.setNextNode(headNode); headNode.setPrevNode(newNode); headNode = newNode; } nodeCount++; } public void addLast(E data){ DLLNode newNode = new DLLNode (); newNode.setData(data); if(headNode == null && tailNode == null){ headNode = tailNode = newNode; //new node becomes both head and tail } else{ tailNode.setNextNode(newNode); newNode.setPrevNode(tailNode); tailNode = newNode; } nodeCount++; } public boolean delete(E data){ DLLNode curNode = headNode; DLLNode prevNode = headNode; while(curNode != null){ if(curNode.getData().equals(data)){ //find the node which has the data prevNode = curNode.getPrevNode(); if(nodeCount == 1){ // if there is only node in the list, and that one node contains the data to be deleted headNode = tailNode = null; // now both head and tail points nowhere } else if(curNode == headNode){ headNode = headNode.getNextNode(); // move the head node to next node headNode.setPrevNode(null); // make new head node's prev node value as null curNode.setNextNode(null); // set the next node of the previous head node (cur node) to null } else if(curNode == tailNode){ tailNode = prevNode; // make the tail node as previous node tailNode.setNextNode(null); //set the next node of tail node as null, as this is the tail node curNode.setPrevNode(null); //make cur node not point to prev node } else{ prevNode.setNextNode(curNode.getNextNode()); // make the prev node point to next node of cur node curNode.getNextNode().setPrevNode(prevNode); //make next node point to pren node curNode.setNextNode(null); // make cur node not point to next node curNode.setPrevNode(null); // make cur node not point to prev node } nodeCount--; return true; //return true to say that data is found and is deleted } curNode = curNode.getNextNode(); } return false; // //return false to say that data is not found and is not deleted } public void displayLinkedListFromHead(){ DLLNode node = headNode; System.out.println("Node Count: " + nodeCount); if(headNode != null){ System.out.println("Head Node: " + headNode.getData()); System.out.println("Tail Node: " + tailNode.getData()); } while(node != null){ System.out.println(node.getData().toString()); node = node.getNextNode(); } } public void displayLinkedListFromTail(){ DLLNode node = tailNode; System.out.println("Node Count: " + nodeCount); if(headNode != null){ System.out.println("Head Node: " + headNode.getData()); System.out.println("Tail Node: " + tailNode.getData()); } while(node != null){ System.out.println(node.getData().toString()); node = node.getPrevNode(); } } } public class TestDLL { public static void main(String[] args){ DoublyLinkedList list = new DoublyLinkedList (); list.addFirst(1); list.addFirst(2); list.addLast(3); list.addLast(4); //list.delete(2); //list.delete(3); //list.delete(2); list.delete(4); list.displayLinkedListFromHead(); list.displayLinkedListFromTail(); } }
Singly linked list implementation in java
I used generics here to write this.
public class Node{ private E data = null; private Node nextNode = null; public Node(E data){ this.data = data; this.nextNode = null; } public Node(){ } public E getData() { return data; } public void setData(E data) { this.data = data; } public Node getNextNode() { return nextNode; } public void setNextNode(Node nextNode) { this.nextNode = nextNode; } } public class SinglyLinkedList { private Node headNode = null; private Node tailNode = null; private int nodeCount = 0; public void addFirst(E data){ // add to the head Node newNode = new Node (data); if(headNode == null && tailNode == null){ // if list is empty, newNode is both the head and tail headNode = tailNode = newNode; } else{ // make newly added node as head node newNode.setNextNode(headNode); headNode = newNode; } nodeCount++; } public void addLast(E data){ // add to the tail Node newNode = new Node (data); if(headNode == null && tailNode == null){ // if list is empty, newNode is both the head and tail headNode = tailNode = newNode; } else{ // make newly added node as tail node tailNode.setNextNode(newNode); tailNode = newNode; } nodeCount++; } public boolean delete(E data){ Node curNode = headNode; Node prevNode = headNode; while(curNode != null){ if(curNode.getData().equals(data)){ //find the node which has the data if(nodeCount == 1){ // if there is only node in the list, and that one node contains the data to be deleted headNode = tailNode = null; // now both head and tail points nowhere } else if(curNode == headNode){ headNode = headNode.getNextNode(); // move the head node to next node curNode.setNextNode(null); // set the next node of the previous head node (cur node) to null null } else if(curNode == tailNode){ tailNode = prevNode; // make the tail node as previous node tailNode.setNextNode(null); //set the next node of tail node as null, as this is the tail node } else{ prevNode.setNextNode(curNode.getNextNode()); // make the prev node point to next node of cur node curNode.setNextNode(null); // make cur node not point to next node } nodeCount--; return true; //return true to say that data is found and is deleted } prevNode = curNode; curNode = curNode.getNextNode(); } return false; // //return false to say that data is not found and is not deleted } public void displayLinkedList(){ Node node = headNode; System.out.println("Node Count: " + nodeCount); if(headNode != null){ System.out.println("Head Node: " + headNode.getData()); System.out.println("Tail Node: " + tailNode.getData()); } while(node != null){ System.out.println(node.getData().toString()); node = node.getNextNode(); } } } public class TestLinkedList { public static void main(String [] args){ SinglyLinkedList list = new SinglyLinkedList (); list.addFirst(1); list.addFirst(2); list.addLast(3); list.addLast(4); list.addFirst(5); list.displayLinkedList(); list.delete(5); list.delete(4); list.displayLinkedList(); } }
Sunday, January 22, 2012
Writing an EJB 2.0 stateless session bean in RSA
1. Go to Window> Preferences> General> Capabilities, and in the dialog, choose Enable All, and Apply.
2. Go to Window> Preferences> Java >Compiler. Ensure that the JDK Compliance is 5.0.
3. Go to Window> Preferences> Java> Installed JREs and make sure Websphere v6.1 JRE is selected.
4. Go to Project, and check "Build Automatically".
Create Enterprise Application Project(EAR) and Enterprise Java Bean Project (EJB)
1. Switch to the J2EE perspective and create a new Enterprise Application Project using File >New>Other.
2. In the New dialog box, find the J2EE section, then select Enterprise Application Project, click Next.
3. Name your project "HeadFirstEJB-ear". Click the Finish button. Note: You may see an error on the EAR project, this is OK; this is because there is no EJB project in the EAR yet.
4. Now create the EJB project. File> New> Other.
5. Find the EJB section, select EJB Project, and click Next.
6. Name the project "HeadFirstEJB-ejb", make sure the "Add project to an EAR" checkbox is selected and the EAR Project is HeadFirstEJB-ear. Press Next.
2. Go to Window> Preferences> Java >Compiler. Ensure that the JDK Compliance is 5.0.
3. Go to Window> Preferences> Java> Installed JREs and make sure Websphere v6.1 JRE is selected.
4. Go to Project, and check "Build Automatically".
Create Enterprise Application Project(EAR) and Enterprise Java Bean Project (EJB)
1. Switch to the J2EE perspective and create a new Enterprise Application Project using File >New>Other.
2. In the New dialog box, find the J2EE section, then select Enterprise Application Project, click Next.
3. Name your project "HeadFirstEJB-ear". Click the Finish button. Note: You may see an error on the EAR project, this is OK; this is because there is no EJB project in the EAR yet.
4. Now create the EJB project. File> New> Other.
5. Find the EJB section, select EJB Project, and click Next.
6. Name the project "HeadFirstEJB-ejb", make sure the "Add project to an EAR" checkbox is selected and the EAR Project is HeadFirstEJB-ear. Press Next.
7.
Skip the Project Facets
screen by clicking the Next
button.
8. On the EJB module screen, make sure to uncheck the "Create an EJB Client
Jar module to hold the client interfaces and classes" checkbox, then click
Finish.
Note:
You may see an
error on the EJB project; this
is ok, as there
are no beans
in the project yet.
Go to
the Project Explorer window and
expand both the EAR and
EJB projects.
Create Session Bean:
In RSA, select the Project Explorer and
expand the EJB project, then expand the Deployment Descriptor section. It
should look similar to this:
we are creating a Session bean called
"Advice".
1. Right-click on the Session Beans entry, and select New>
Session Bean
2. In the Create an Enterprise Bean dialog, name the bean Advice,
and ensure that the package name is ejbs. Note: Do NOT NAME IT "ADVICE BEAN'', then press Next.
3. On the next
screen, you shouldn't have to change anything, but should note that the wizard
is going to create the three files (one class and two interfaces)
4. Press Next twice (skipping the EJB Java Class Details screen). On the Select Class Diagram for Visualization screen, uncheck the "Add bean to Class Diagram" checkbox and then press Finish. There will now be no errors on the projects.
5. Now if you expand the Advice bean section in the project explorer, you should be able to see the three files that have been created for you by the wizard. See the following:
Bean Class:
If you double-click the AdviceBean class in the project ,explorer, it will open up the bean class.
Note that the wizard has already created the
boilerplate code for the Session Bean interface as well as the creation method:
ejbCreate(), ejbActivate(), ejbPassivate(), ejbRemove() and
setSessionContext( SessionContext ). The
wizard also created a seriaiVersionUID that is recommended for any class that
is serializable. The Session Bean interface extends Serializable. There is also
a mySessionCtx variable to hold the session context that is set by the server.
The server will call setSessionContext() during the beans lifecycle.
Type in the getAdvice() method in thw AdviceBean.java file. Take a look at the Advice.java interface file. You will need to add the getAdvice() method signature to it. There's another way to have RSA add your method signatures to the interfaces, but for now, do it by hand.
In the AdviceHome.java interface, the wizard has taken
care of the create() method for
you.
Viewing the Deployment Descriptor:Select and expand the EJB project. In the
J2EE Perspective, you can open the Deployment Descriptor by double-clicking on
the entry (Deployment Descriptor) just below the HeadFirstEJB-ejb. Or you can open
the ejbModule folder, expand the META-INF folder, and then double-click on
the ejb-jar.xml file. This will open the Deployment
Descriptor editor.
At this point you can view the Deployment
Descriptor to see what RSA has created. It is worth noting that when using the
J2EE perspective, RSA displays in slightly different fashion than when in the
Java perspective. After opening the project, switch back and forth, and observe
the differences.
There are tabs along the bottom of the editor which present a user friendly view of the Descriptor.
(Do
not be concerned with
understanding everything that is being presented.) By default, the source tab is
not open
for view. Find the Source tab,
and click on it. You should
see the following.
Deploying
the Application:
At this point, you have an application that
can actually be deployed to the server. Before you can deploy the application
however, you need to generate the deployment code. This process generates the
code needed to facilitate the remote EJB call from the client.
1. In the Package Explorer, select the EJB project, right-click on it. In the selection list about 3/4 down choose Prepare For Deployment. Note: There will be no successful completion message; if you do not see an error, the process was successful. Examine the new contents of the ejbModule folder to see the new artifacts & packages that RSA created. The generated code will be distinctive and will have names prefaced with an underscore and/or EJS. See the following:
2. If
there is no server view pane open, open it now (Window> Show View>
Servers). When creating a new workspace, RSA should have created a server(s) by
default. If there are no servers present in the view, right-click in the server
pane and create a new server- you can use the
default RSA profile. Now start the server and ensure that it starts correctly
by viewing the console output- you should see no errors.
3. You
are now ready to deploy the application. In the server pane, select the server. Right-click and choose Add and Remove Projects from
the list. This will bring up the following dialog:
4.The EAR project will appear in the Available Projects section. Click the Add button to move the project to the Configured projects, and click Finish.
5. You will see activity in the console. Check the console for successful installation. Among other information, you should see a line of text containing the following: Application started:
HeadFirstEJB-ear. Right-click on the server and select Stop.
5. You will see activity in the console. Check the console for successful installation. Among other information, you should see a line of text containing the following: Application started:
HeadFirstEJB-ear. Right-click on the server and select Stop.
RSA- Universal Test Client
We will now examine a built in facility in RSA to test EJBs.
Excerpt from IBM Help Center:
Universal Test Client:
The server tools provide you with a Web-based Universal Test Client where you can test your enterprise beans and other Java objects running in a local or remote server. Using this test client, you can test the local or remote interface methods of your enterprise beans. By calling the methods and passing user-defined arguments you can test the methods to ensure that they work correctly.
Using the Universal Test Client, you can accomplish the following tasks:
We will now examine a built in facility in RSA to test EJBs.
Excerpt from IBM Help Center:
Universal Test Client:
The server tools provide you with a Web-based Universal Test Client where you can test your enterprise beans and other Java objects running in a local or remote server. Using this test client, you can test the local or remote interface methods of your enterprise beans. By calling the methods and passing user-defined arguments you can test the methods to ensure that they work correctly.
Using the Universal Test Client, you can accomplish the following tasks:
• Find an enterprise bean when you know or do not know the JNDI name
• Call a method
• Remove an enterprise bean
• View public fields of methods
• View fields
• View methods
• Filter the methods displayed in the Universal Test Client
Following, are some basic steps to use the Universal Test Client:
1. If your server is not still running from the previous exercise, please start it now.
2. Right-click on the 6.1 server in the server view, and select Run Universal Test Client (third
option from the bottom of list). The test client page should appear.
3. In the upper left of the pane, click on JNDI Explorer. This will display information in the right pane.
4. Now in the right-hand pane and expand the ejb folder. Expand the ejbs folder and click on the AdviceHome entry.
5. Now look in the upper left corner of the pane. Expand the AdviceHome entry and click Advice Create()
• Call a method
• Remove an enterprise bean
• View public fields of methods
• View fields
• View methods
• Filter the methods displayed in the Universal Test Client
Following, are some basic steps to use the Universal Test Client:
1. If your server is not still running from the previous exercise, please start it now.
2. Right-click on the 6.1 server in the server view, and select Run Universal Test Client (third
option from the bottom of list). The test client page should appear.
3. In the upper left of the pane, click on JNDI Explorer. This will display information in the right pane.
4. Now in the right-hand pane and expand the ejb folder. Expand the ejbs folder and click on the AdviceHome entry.
5. Now look in the upper left corner of the pane. Expand the AdviceHome entry and click Advice Create()
6. In the right-hand pane click on the Invoke button. This calls the create() on Home interface.
7. Now another button appears just below the Invoke button. Click the Work with Object button.
8. You will see another entry appear in upper left called Advice 1. Click on this entry to expand it.
9. The methods available in the Advice bean- getAdvice() is displayed.
7. Now another button appears just below the Invoke button. Click the Work with Object button.
8. You will see another entry appear in upper left called Advice 1. Click on this entry to expand it.
9. The methods available in the Advice bean- getAdvice() is displayed.
Subscribe to:
Posts (Atom)