Posts which you will also like.

Thursday, February 23, 2012

Creating projects in java in team:Setting project on google code and connecting it with eclipse


Follow the following simple steps to set up :

1.Creating a New Project: 

Go to following link:
http://code.google.com/hosting/createProject

Sign in using your gmail account

2.Fill the proper details of the project you want to create:

image1


3.Install Subclipse plugin in Eclipse:
Open eclipse:
  • Go to help
  • Install new software
  • Click on add
  • In the Add repository dialog
  • choose any name you want
  • In location enter the following:
URL: http://subclipse.tigris.org/update_1.6.x
plugin_03
Light bulbIt is showing the duplicate location since I have configured it already.


image5

Click Next and continue until done.Eclipse will ask you to restart so that changes can take effect.


image9


Next step:Using Subclipse:

To participate in the developement of a project you must have to be a member of that project.You can ask the Administrator of the project to join the project.

Now time to set up repository (only members of a project can do it)
select Show view as fast view button then form the pop up dialog choose Other(you can also use the shortcut Alt+shift+Q,Q)


bblo


Now scroll down to SVN when next dialog box appears and choose SVN Repositories.
Untitled-3_03

SVN Repositories window will be opened right click then select New and click on
Repository Location.


image12

In the next Dialog box Enter the url of your project which you can get from your google code project under source tab.

Untitled-4_02
Copy the underline link in the Url:


image13

Click Finish when prompted.

Now we are ready to check in our project in Goole Code.It’s time to share our project with our team.Proceed as shown

image16


In the next Dialog select SVN

image17

Now select Use existing Repository location and click next.

Repository you already set up will appear in this dialog box

mod


Now select use project name as folder name and click next.

A new dialog will appear next which ask for a Username and a password.
Username is generally your email address but password can be generated via following manner:

  • Goto your project on google code and select source tab
  • Click on googlecode.com password to generate the password 
Untitled-4_02
Copy the password and paste it in the place of password in the eclipse Share project dialog



ima

Eclipse will ask you to choose the perspective associated with team.


image23

Now a folder with your project name will be created under svn/trunk at google code page at Source tab.

The project files can be uploaded to this project folder.You can perform this operation by clicking on Project and select Team the click Commit.

lk_02


A new window dialog will appear which will ask you to select the files you want to upload.You will be asked again the username and password.After this your project files can be seen at Google code.

Every time a team member update a source file then he/she commits which gets reflected in the repository.



Read More ->>

Tuesday, February 7, 2012

A Tutorial on Java for Solution Of Producer-Consumer Problem


Consumer Producer is a problem related to sharing of data.In this problem producer and consumers operates simultaneously on the shared data.More than one producers produce and more than one consumers consume.So two prevent resource quench we must use thread safety rules and enforce sequential access rather than concurrent access.
DIAGRAM-producer
To illustrate this situation we will use following code snippets :

Money Producer Class:

package com.techy.rajeev;

public class Producer implements Runnable {
    private SyncronizedStack stack;
    private int producerNum;
    public Producer(SyncronizedStack stack,int producerNum) {
        this.stack=stack;
        this.producerNum=producerNum;
    }

    public void run(){
        char c;
        for(int i=0;i<30;i++){
            c=(char)(Math.random()*26+'A');
            stack.push(c);
            System.out.println("Producer"+producerNum+"produced :"+c);
            try{
                Thread.sleep((int)(Math.random()*300));
                
            }catch(InterruptedException e){
                System.out.println("Error");
            }
        }
    }
}





Plate Consumer Class:


package com.techy.rajeev;

public class Consumer implements Runnable{
    private SyncronizedStack stack;
    private int consumerNum;
    public Consumer(SyncronizedStack stack,int consumerNum) {
        this.stack=stack;
        this.consumerNum=consumerNum;
    }

    public void run(){
        char c;
        for(int i=0;i<30;i++){
            c=stack.pop();
            System.out.println("Consumer"+consumerNum+"consumed:"+c);
            try{
                Thread.sleep((int)(Math.random()*300));
            }catch(InterruptedException e){
                System.out.println("Error");
            }
        }
    }

}


>Clock SynchronizedStack Class:

package com.techy.rajeev;

import java.util.Stack;

public class SyncronizedStack {
    private Stack<Character> buffer=new Stack<Character>();
    public synchronized char pop(){
        char c;
        while(buffer.size()==0){
            try{
                this.wait();
            }catch(InterruptedException e){
                
          }
        }
        c=buffer.remove(buffer.size()-1);
        return c;
    }
    public synchronized void push(char c){
        this.notify();
        buffer.add(c);
    }
}


Computer SyncTest Class:


package com.techy.rajeev;

public class SyncTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SyncronizedStack stack=new SyncronizedStack();
        Producer p1 =new Producer(stack,1);
        Thread t1=new Thread(p1);
        t1.start();
        Producer p2=new Producer(stack,2);
        Thread t2=new Thread(p2);
        t2.start();
        Consumer c1=new Consumer(stack,1);
        Thread ct1=new Thread(c1);
        ct1.start();
        Consumer c2=new Consumer(stack,2);
        Thread ct2=new Thread(c2);
        ct2.start();
    }

}

Thumbs up Output:

Producer1 produced :G
Consumer1 consumed:G
Consumer2 consumed:I
Producer2 produced :I
Producer1 produced :J
Consumer1 consumed:J
Producer2 produced :L
Consumer2 consumed:L
Producer1 produced :E
Consumer1 consumed:E
Consumer1 consumed:G
Producer2 produced :G
Producer1 produced :T
Consumer2 consumed:T
Producer2 produced :I
Consumer2 consumed:I
Producer1 produced :W
Consumer1 consumed:W
Producer2 produced :N
Consumer2 consumed:N
Producer1 produced :J
Consumer1 consumed:J
Producer1 produced :V
Consumer1 consumed:V
Producer2 produced :A
Consumer1 consumed:A
Producer1 produced :E
Consumer1 consumed:E
Producer2 produced :D
Consumer2 consumed:D
Producer2 produced :T
Consumer2 consumed:T
Producer2 produced :I
Consumer2 consumed:I
Producer2 produced :Z
Consumer1 consumed:Z
Producer1 produced :B
Consumer2 consumed:B
Consumer1 consumed:M
Producer2 produced :M
Producer1 produced :N
Consumer2 consumed:N
Consumer1 consumed:K
Producer2 produced :K
Producer1 produced :B
Consumer1 consumed:B
Consumer2 consumed:Y
Producer2 produced :Y
Consumer1 consumed:S
Producer2 produced :S
Consumer2 consumed:P
Producer2 produced :P
Producer1 produced :I
Producer2 produced :W
Producer2 produced :Z
Consumer1 consumed:Z
Producer2 produced :Y
Consumer2 consumed:Y
Producer1 produced :V
Producer1 produced :G
Consumer2 consumed:G
Consumer1 consumed:V
Consumer1 consumed:W
Producer2 produced :K
Consumer1 consumed:K
Consumer1 consumed:I
Producer1 produced :F
Consumer2 consumed:F
........
............
.......................
Read More ->>

Monday, February 6, 2012

A Tutorial on Java for Solution of The Josephus Problem


Light bulbDescription of the Problem: 
In this java tutorial we discuss about Josephus problem and its solution.
The Josephus Problem is a famous mathematical puzzle that goes back to ancient times. There are many stories to go with the puzzle. One is that Josephus was one of a group of Jews who were about to be captured by the Romans. Rather than be enslaved, they chose to commit suicide. They arranged themselves in a circle and, starting at a certain person, started counting off around the circle. Every nth person had to leave the circle and commit suicide.
Josephus decided he didn’t want to die, so he arranged the rules so he would be the last person left.
If there were (say) 20 people, and he was the seventh
person from the start of the circle, what number should he tell them to use for counting off?
The problem is made much more complicated because the circle
shrinks as the counting continues.
Thumbs up  Inputs are the number of people in the circle, the number used for counting off, and the number of the person where counting starts (usually 1). The output is the list of persons being eliminated.
Thumbs up When a person drops out of the circle, counting starts again from the person
who was on his left (assuming you go around clockwise).
Thumbs up Here’s an example.
There are seven people numbered 1 through 7, and you start at 1 and count off
by threes. People will be eliminated in the order 4, 1, 6, 5, 7, 3. Number 2 will remain at last
Solution of the Problem:
This problem can be solved by using a circular linked list. Complete listing of the java code is given below:
Pointing upCode for Circular linked list with some modified method(game())
package techy.rajeev;

public class CircularLinkedList {
    private Node start;
    private int count;
    public void append(int x){
        count++;
        Node temp=new Node(x);
        if(start==null){
            start=temp;
        }else{
            Node tp=start;
            while(tp.link!=start){
                tp=tp.link;
            }
            tp.link=temp;
        }
        temp.link=start;
    }
    public void addBeg(int x){
        count++;
        Node temp=new Node(x);
        if(start==null){
            temp.link=temp;
        }else{
            Node tp=start;
            while(tp.link!=start){
                tp=tp.link;
            }
            tp.link=temp;
            temp.link=start;
        }
        start=temp;
    }
    public void addAt(int pos,int x){
        Node temp,tp;
        temp=new Node(x);
        tp=start;
        for(int i=1;i<=pos;i++){
            if(tp.link==start)
                break;
            tp=tp.link;
        }
        temp.link=tp.link;
        tp.link=temp;
        count++;
    }
    public void displayList(){
        if(start==null)
            System.out.println("List is empty..");
        else{
        Node temp=start;
        System.out.print("->");
        while(temp.link!=start){
            System.out.println(" "+temp.data);
            temp=temp.link;
        }
        System.out.println(temp.data+" ->");
    }
}
    public void deleteAt(int position){
        Node current=start;
        Node previous=start;
        for(int i=0;i<position;i++){
            if(current.link==start)
                break;
            previous=current;
            current=current.link;
        }
        System.out.print(current.data);
        if(position==0)
            deleteFirst();
        else
            previous.link=current.link;
        count--;
    }
    public int deleteNode(Node node){
        Node current=start;
        Node previous=start;
        int data=node.data;
        while(current.data!=data){
            if(current.link==start)
                break; // Node does not exists in circular linked list
            previous=current;
            current=current.link;
        }
        previous.link=current.link;
        count--;
        return data;//returning the deleted data
    }
    public void deleteFirst() {
        Node temp=start;
        while(temp.link!=start){
            temp=temp.link;
        }
        temp.link=start.link;
        start=start.link;
        count--;
    }
    public int getCount(){
        return count;
    }
    /* Extra method for Josephus problem */
    public void game(int countToDeath,int persons){

        Node current=start;
        int caller=0;
        while(true){
            caller=current.data;
            System.out.print(caller+"Says->");
            for(int i=0;i<countToDeath;i++){
                current=current.link;
            }
            start=current.link;
            if(getCount()>1){
                // comparing caller with person going to be dead
              if(caller==current.data)
                    System.out.println("WTF!! I will have to kill myself ! :( "
                                  +deleteNode(current));
            else    
              System.out.println("I do not want to say but you must die Mr. no:"
                                   +deleteNode(current));
            }else{
                System.out.println("Thank God ! I saved myself :)");
                break;
                }
           
            current=start;
        }
    }
    private static class Node{
    int data;
    Node link;
    public Node(int data){
        this.data=data;
    }
    @SuppressWarnings("unused")
    public Node(int data,Node link){
        this.data=data;
        this.link=link;
    }
   }
}


Pointing upCode for main class:

package techy.rajeev;

import java.util.Scanner;

public class Josephus {
    private CircularLinkedList cl;
    private int noOfPersons;
    private int countToDeath;
    public void setInts(){
        System.out.print("Enter the no. of people:");
        Scanner in=new Scanner(System.in);
        noOfPersons=in.nextInt();
        System.out.print("Enter the count to death:");
        countToDeath=in.nextInt();
    }
    public void buildCircularLinkedList(){
        cl=new CircularLinkedList();
        for(int i=1;i<=noOfPersons;i++)
            cl.append(i);
        cl.displayList();
    }
    public void startTheGame(){
        cl.game(countToDeath, noOfPersons);
        System.out.print("Lucky fellow was:");cl.displayList();
    }
    public static void main(String args[]){
        Josephus js=new Josephus();
        js.setInts();
        js.buildCircularLinkedList();
        js.startTheGame();
        
    }
    
}
Pointing upOutput for a test case:

Enter the no. of people:7

Enter the count to death:3

-> 1
2
3
4
5
6
7 ->
1Says->I do not want to say but you must die Mr. no:4

5Says->I do not want to say but you must die Mr. no:1

2Says->I do not want to say but you must die Mr. no:6

7Says->I do not want to say but you must die Mr. no:5

7Says->WTF!! I will have to kill myself ! :( 7

2Says->I do not want to say but you must die Mr. no:3

2Says->Thank God ! I saved myself :)

Lucky fellow was:->2 ->

More on this at techcresendo.com

Read More ->>

Saturday, February 4, 2012

Circular Linked list implementation in Java


public class CircularLinkedList {
    private Node start;
    private int count;
    public void append(int x){
        count++;
        Node temp=new Node(x);
        if(start==null){
            start=temp;
        }else{
            Node tp=start;
            while(tp.link!=start){
                tp=tp.link;
            }
            tp.link=temp;
        }
        temp.link=start;
    }
    public void addBeg(int x){
        count++;
        Node temp=new Node(x);
        if(start==null){
            temp.link=temp;
        }else{
            Node tp=start;
            while(tp.link!=start){
                tp=tp.link;
            }
            tp.link=temp;
            temp.link=start;
        }
        start=temp;
    }
    public void addAt(int pos,int x){
        Node temp,tp;
        temp=new Node(x);
        tp=start;
        for(int i=0;i<pos;i++){
            if(tp.link==start)
                break;
            tp=tp.link;
        }
        temp.link=tp.link;
        tp.link=temp;
        count++;
    }
    public void displayList(){
        if(start==null)
            System.out.println("List is empty..");
        else{
        Node temp=start;
        System.out.print("->");
        while(temp.link!=start){
            System.out.println(" "+temp.data);
            temp=temp.link;
        }
        System.out.println(temp.data+" ->");
    }
}
    public void deleteAt(int position){
        Node current=start;
        Node previous=start;
        for(int i=0;i<position;i++){
            if(current.link==start)
                break;
            previous=current;
            current=current.link;
        }
        System.out.print(current.data);
        if(position==0)
            deleteFirst();
        else
            previous.link=current.link;
        count--;
    }

    public void deleteFirst() {
        Node temp=start;
        while(temp.link!=start){
            temp=temp.link;
        }
        temp.link=start.link;
        start=start.link;
        count--;
    }
    public int getCount(){
        return count;
    }
    private static class Node{
    int data;
    Node link;
    public Node(int data){
        this.data=data;
    }
    @SuppressWarnings("unused")
    public Node(int data,Node link){
        this.data=data;
        this.link=link;
    }
    }
}


Class which uses this Circular linked list.


public class CLLUserClass {
public static void main(String args[]){
    CircularLinkedList ccl=new CircularLinkedList();
    ccl.addBeg(1);
    ccl.append(2);
    ccl.append(3);
    ccl.append(4);
    ccl.addAt(1, 0);
    ccl.append(5);
    ccl.append(12);
    ccl.displayList();
    ccl.deleteAt(1); //index starts from zero
    System.out.println("After deletion....");
    ccl.displayList();
}
}

More on this at techcresendo.com
Read More ->>
DMCA.com Protected by Copyscape Online Plagiarism Tool