Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Thursday, April 28, 2016

Java Chat Program Using TCP Socket | Java Networking

Java Chat Program

When we are developing a client/server model based application, first we need to make a decisions that whether the application is to run over TCP or over UDP.

TCP is connection-oriented and provides a reliable byte stream channel through which data flows between two end systems. UDP is connection-less and sends independent packets of data from one end system to the other, without any guarantees about delivery.


Client Side Code
import java.util.*;
import java.io.*;
import java.net.*;

public class ClientTCP 
{
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("localhost", 30000);  
        DataInputStream dis = new DataInputStream(s.getInputStream());
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        String msg = "";
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.print("Client: ");
            msg = sc.nextLine();
            dos.writeUTF(msg);
            if (msg.equals("stop")) {
                break; 
            }
            msg = dis.readUTF();
            System.out.println("Server: " + msg);           
        }
        dis.close();
        dos.close();
        s.close();
    }
}
Server Side Code
import java.util.*;
import java.net.*;
import java.io.*;

public class ServerTCP
{
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(30000);
        Socket s = ss.accept(); 
        System.out.println("connection established...");
        DataInputStream dis = new DataInputStream(s.getInputStream());
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        String msg = "";
        Scanner sc = new Scanner(System.in);
        while(true) {
            msg = dis.readUTF();
            System.out.println("Client: " + msg);
            if (msg.equals("stop")) {
                break; 
            }
            System.out.print("Server: ");
            msg = sc.nextLine();
            dos.writeUTF(msg);         
        }
        dis.close();
        dos.close();
        s.close();
        ss.close();
    }
}

Output

Make sure to run the server program first and then the client program.

Tuesday, April 5, 2016

Prime Factors Using Unix Shell Script

Prime Factors Using Bash Script

To do solve this problem we use the factor command available in the shell. The factor command prints the print the prime factors of the given number. The command executes as follows.

$ factor 6
6: 3 2
$ factor 5
5: 5

From the above example we can clearly see that factor n return the all prime factors of n. If the given number ‘n’ is a prime number the number of factor will be one i.e. the number itself. If the given number ‘n’ is a non prime number then the number of factor is greater than 1.

Now, we need to evaluate the factor command for each input in a to b where a is the lower range and b is the upper range given by the user. If factor i return exactly 2 arguments we will conclude that I is a prime number otherwise I is a non prime number.

For this we need to set the output of factor command to command line using set command then using the $# variable we will determine the number of values in the command line.

Code

# Unix Shell Script To Print Prime Numbers In A Given Range [a, b]

if [ $# -ne 2 ]
then
    echo "Wrong Number Of Arguments"
    exit
fi

if [ $2 -lt 0 -o $1 -lt 0 ]
then
    echo "Argument One Or Two Is Negative."
    exit
fi

a=$1
b=$2

if [ $a -gt $b ]
then
    t=$a
    a=$b
    b=$t
fi

echo "Prime Numbers In Range [$a, $b] Are:"
echo -ne "["

for i in `seq $a 1 $b`
do
    set -- `factor $i`
    if [ $# -eq 2 ]
    then
        echo -ne "$i,"
    fi
done

echo -e "\b]"

Output

$sh primerange.sh 1 10
Prime Numbers In Range [1, 10] Are:
[2,3,5,7]

$sh primerange.sh 10 50
Prime Numbers In Range [10, 50] Are:
[11,13,17,19,23,29,31,37,41,43,47]

$sh primerange.sh
Wrong Number Of Arguments

$sh primerange.sh -5 10
Argument One Or Two Is Negative.


Saturday, April 2, 2016

Boundary Fill C Program Using graphics.h

Boundary Fill Algorithm

Unlike Flood Fill Algorithm, Boundary Fill Algorithm starts at a point inside a region and paint the interior outward toward the boundary. If the boundary is specified in a single color, the fill algorithm proceeds outward pixel by pixel until the boundary color is encountered. This method, called the boundary-fill algorithm.

Follow The Instructions To Successfully Run The Program In Dev-Cpp/CodeBlock:

Whenever you #include <graphics.h> in a program, you must instruct the linker to 
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the 
Parameters tab from the pop-up window and type the following into the Linker area:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

Code

#include <stdio.h>
#include <graphics.h>

/* fc = fill color and bc = boundary color */
void boundaryfill(int x, int y, int fc, int bc)
{
    int c = getpixel(x, y);
    if (c != fc && c != bc) {
        putpixel(x, y, fc);
        boundaryfill(x, y + 1, fc, bc);
        boundaryfill(x, y - 1, fc, bc);
        boundaryfill(x - 1, y, fc, bc);
        boundaryfill(x + 1, y, fc, bc);
    }
}

int main()
{
    int arr[] = {10, 10, 100,10, 10, 100, 10, 10};
    initwindow(300,300,"BoundaryFill");
    drawpoly(4, arr);
    
    delay(3000);
    
    boundaryfill(40, 40, 4, 15);
    
    while(!kbhit());
    
    return 0;
}

Output


Friday, April 1, 2016

Find Prime Numbers Using Bash Script

This script will determine all prime numbers between two positive integers a and b. a and b are supplied as command line arguments.

if [ $# -ne 2 ]
then
    echo  "Wrong Number of Arguments in the Command Line"
    exit
fi

if [ $1 -le 0 -o $2 -le 0 ]
then
    echo "Either 1st or 2nd Argument is NEGATIVE or ZERO "
    exit
fi
    
a=$1;b=$2
if [ $a -gt $b ]
then 
    t=$a
    a=$b
    b=$t
fi
i=$a
while [ $i -le $b ]
do
    set -- `factor $i | cut -f 2 -d":"`
    if [ $# -eq 1 ]
    then
        echo "$i is a Prime Number"
    fi
    i=`expr $i + 1 `
done
$ sh x.sh 2 50
2 is a Prime Number 
3 is a Prime Number 
5 is a Prime Number 
7 is a Prime Number 
11 is a Prime Number 
13 is a Prime Number 
17 is a Prime Number 
19 is a Prime Number 
23 is a Prime Number 
29 is a Prime Number 
31 is a Prime Number 
37 is a Prime Number 
41 is a Prime Number 
43 is a Prime Number 
47 is a Prime Number 

Flood Fill C Program Using graphics.h

Flood Fill Algorithm

Sometimes we want to fill in (or recolor) an area that is not defined within a single color boundary. Here is a figure describing the situation.

We can paint such areas by replacing a specified interior color instead of searching for a boundary color value. This approach is called a flood-fill algorithm. We can use either a 4-connected or 8-connected approach,

Follow The Instructions To Successfully Run The Program In Dev-Cpp/CodeBlock:

Whenever you #include <graphics.h> in a program, you must instruct the linker to 
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the 
Parameters tab from the pop-up window and type the following into the Linker area:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

Code

#include <stdio.h>
#include <graphics.h>


/* oc = old color and fc = fill color */
void floodfill(int x, int y, int fc, int oc)
{
    int c = getpixel(x, y);
    if (c == oc) {
        putpixel(x, y, fc);
        floodfill(x, y + 1, fc, oc);
        floodfill(x, y - 1, fc, oc);
        floodfill(x - 1, y, fc, oc);
        floodfill(x + 1, y, fc, oc);
    }
}

int main()
{
    int arr[] = {10, 10, 100,10, 10, 100, 10, 10};
    initwindow(300,300,"FloodFill");
    drawpoly(4, arr);
    
    delay(3000);
    
    floodfill(40, 40, 4, 0);
    
    while(!kbhit());
    
    return 0;
}

Output