Tuesday, May 19, 2015

A guessing game in java

 import java.util.Random;
import java.util.Scanner;

public class gussingGame {
    int min=1;
    int max=16;
        Random rand=new Random();
        int num= rand.nextInt(max-min+1)+min;
        int gues;
    public int read()
    {
        Scanner s=new Scanner(System.in);
        System.out.println("Enter your guesing number between 1-16 : ");
        gues=s.nextInt();

       return gues;
    }
    public void check()
    {
        int i=0;
        while(i<16) {
            if (num==gues) {
                System.out.println("your gues is correct");
                break;
            } else if (num < gues) {
                System.out.println("The number is less than your gues ");
            } else {
                System.out.println("The number is greater than your gues ");
            }
            read();
            i++;
        }
    }
    public static  void main (String a [])
    {
        gussingGame gm=new gussingGame();
                gm.read();
        gm.check();
    }
}

Monday, May 18, 2015

Queue implementation in Java

public class queue {
    int num[] ;
    int i;
    int top;
    int maxSize;
    public queue(int length)
    {
        this.maxSize=length;
        top=-1;
        num=new int [maxSize];
    }
    public boolean isEmpty()
    {
        return  top==-1;
    }
    public boolean isFull()
    {
        return top==maxSize-1;
    }
    public void push(int newElm)
    {
        if(!isFull()) {
            num[++top] = newElm;
        }
        else        {
            System.out.println("array is full");
        }
    }
    public void pop()
    {
        for(i=0;i<top;i++) {
           num[i]=num[i+1];
        }
        top--;
    }
    public void print() {
        if(isEmpty())
        {
            System.out.println("array is Empty");
        }
        else {
            System.out.println("numbers are:");
            for (i = 0; i <= top; i++) {
                System.out.print(num[i] + " ");
            }
        }
    }
    public static void main(String a [])
    {
        queue q=new queue(5);
        q.push(51);
        q.push(56);
        q.push(56);
        q.pop();
        q.push(566);
        q.pop();
        q.print();
    }
}

Stack implementation in Java

import java.util.Scanner;

public class stack {
    int num[] ;
    int i;
    int top;
    int maxSize;
    public stack(int length)
    {
       this.maxSize=length;
        top=-1;
        num=new int [maxSize];
    }
    public boolean isEmpty()
    {
        return  top==-1;
    }
    public boolean isFull()
    {
        return top==maxSize-1;
    }
    public void push(int newElm)
    {
        if(!isFull()) {
            num[++top] = newElm;
        }
        else        {
            System.out.println("array is full");
        }
    }
    public int pop()
    {
        return num[top--];
    }
    public void peek()
    {
        if(!isEmpty())
        System.out.println("top element is "+num[top]);
        else            System.out.println("array is Empty");
    }
    public void print() {
        if(isEmpty())
        {
            System.out.println("array is Empty");
        }
        else {
            System.out.println("numbers are:");
            for (i = 0; i <= top; i++) {
                System.out.print(num[i] + " ");
                System.out.println();
            }
        }
    }
    public static void main(String a [])
    {
        stack st=new stack(5);
        st.push(50);
        st.push(45);
        st.push(65);
        st.push(85);
        st.pop();
        st.print();
        st.peek();
    }
}

Wednesday, May 6, 2015

Installing NodeJs on NetBeans

1. Install NodeJs first
2. Download the plugin nbm file Here
3. Go to NetBeans Tools > Plugins > Downloaded menu and click on ‘Add Plugins…’. Select the           downloaded nbm file and click install. This will install the nodejs plugin.

Installing NodeJs on ubuntu

1. apt-get update
2. apt-get install nodejs
3. apt-get install npm

XAMPP Installation on Ubuntu

1. Download the latest version of XAMPP Here
2. Go to the directory of downloaded XAMPP
    cd ~/Downloads
    sudo su
3. Makes the XAMPP package as executable
    sudo chmod +x xampp-linux-x64-5.6.8-0-installer.run
4. Install the XAMPP as shown bellow
   sudo ./xampp-linux-x64-5.6.8-0-installer.run
5. Then you get XAMPP installation wizard. It's now next..next installation.

For starting the XAMPP server
sudo /opt/lampp/lampp start
For stop the XAMPP server
sudo /opt/lampp/lampp stop

Thursday, March 26, 2015

Socket programming for multiple requests and respondses

TCPServer.py
from socket import *
serverPort = 12029
print 'The server is ready to receive'
c=1
while c==1:
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(('', serverPort))
    serverSocket.listen(1)
    connectionSocket, addr = serverSocket.accept()
    sentence = connectionSocket.recv(1024)
    capitalizedSentence = sentence.upper()
    connectionSocket.send(capitalizedSentence)
 
connectionSocket.close()

TCPClient.py
from socket import *
serverName = 'localhost'
serverPort = 12029
c=1
while c==1:
    clientSocket = socket(AF_INET, SOCK_STREAM)
    clientSocket.connect((serverName, serverPort))
    sentence = raw_input('Input lowercase sentence:')
    clientSocket.send(sentence)
    modifiedSentence = clientSocket.recv(1024)
    print 'From Server:', modifiedSentence
    
clientSocket.close()

Socket programming for 2 clients to talk each other through the server

TCPServer.py
from socket import *
serverPort = 12095
print 'The server is ready to receive'
c=1
while c==1:
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(('', serverPort))
    serverSocket.listen(2)
    connectionSocket1, addr1 = serverSocket.accept()
    connectionSocket2, addr2 = serverSocket.accept()
    sentence1 = connectionSocket1.recv(1024)
    sentence2 = connectionSocket2.recv(1024)
    capitalizedSentence1 = sentence1.upper()
    capitalizedSentence2 = sentence2.upper()
    connectionSocket2.send(capitalizedSentence1)
    connectionSocket1.send(capitalizedSentence2)

connectionSocket.close()

TCPClient1.py
from socket import *
serverName = 'localhost'
serverPort = 12095
c=1
while c==1:
    clientSocket = socket(AF_INET, SOCK_STREAM)
    clientSocket.connect((serverName, serverPort))
    sentence = raw_input('Input lowercase sentence:')
    clientSocket.send(sentence)
    modifiedSentence = clientSocket.recv(1024)
    print 'From Server:', modifiedSentence
    
clientSocket.close()

TCPClient2.py
from socket import *
serverName = 'localhost'
serverPort = 12095
c=1
while c==1:
    clientSocket = socket(AF_INET, SOCK_STREAM)
    clientSocket.connect((serverName, serverPort))
    sentence = raw_input('Input lowercase sentence:')
    clientSocket.send(sentence)
    modifiedSentence = clientSocket.recv(1024)
    print 'From Server:', modifiedSentence
    
clientSocket.close()


Saturday, March 14, 2015

Simple UDP socket program in python

UDPServer.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print "The server is ready to receive"
while True:
    message, clientAddress = serverSocket.recvfrom(2048)
    modifiedMessage = message.upper()
    serverSocket.sendto(modifiedMessage, clientAddress)

UDPClient.py
from socket import *
serverName = 'localhost'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = raw_input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()

Simple TCP socket program in python

TCPServer.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print 'The server is ready to receive'
while 1:
    connectionSocket, addr = serverSocket.accept()
    sentence = connectionSocket.recv(1024)
    capitalizedSentence = sentence.upper()
    connectionSocket.send(capitalizedSentence)
    connectionSocket.close()


TCPClient.py
from socket import *
serverName = 'localhost'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
sentence = raw_input('Input lowercase sentence:')
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print 'From Server:', modifiedSentence
clientSocket.close()

Thursday, March 12, 2015

OOP principles in Java

Encapsulation

Encapsulation is the mechanism that binds together code and data and keeps both safe from the outside interference and misuse.
The technique is, making the fields in a class as private. if we declare a variable as private than anyone can't accessed the fields outside the class.
The advantage of encapsulation is, the users can't break the code. so It gives maintainability and flexibility of code.

Inheritance

In OOP concepts, we define everything as objects(eg: dog,cat,human) . Each objects have behaviours and attributes. There is some relationships among objects with their behaviours and attributes.
Inheritance is a process which one object acquires the properties of another object. sub classes acquire some attributes of  the super class. For a example animal is a super class of mammals and mammal is a super class of dog.

In inheritance, we use the keywords extends and implements.

extends : when we inherit a class with a class this keyword can be used.
For example:
public class mammal{}
public class dog extends mammal{}
In this case, all the properties of super class will be able to inherit with the super class except the private properties of the super class.

implements : This keyword is used when a class inherit with a interface.
For a example:
public interface mammal{}
public class dog implements mammal{}