Find us on Google+ Kill the code: 2012

Friday 14 December 2012

String sorting in JAVA

ublic class string_sort {
        static String input[] = {"abcdefgh","abcdefg","abcdef","abcde","abcd","abcz","ab","bat", "basd", "ba", "bas","abcdefgh","abcdefg","abcdef","abcde","abcd","abcz","ab","bat", "basd", "ba", "bas"};
   
    static int max = input[0].length();
   
    public static void main(String args[]) {
        findMax();
        int[] all = {0,0};
        int k =0;
        int size = input.length;
            for(int i=0;i<size;i++) {
                for(int j=0;j<size;j++) {
                    if(input[i].compareTo(input[j]) < 0) {
                        swap(i,j);
                    }      
                }
            }
        for(int i=0;i<size;i++) {
            System.out.println("" + input[i]);
        }       
    }
static void swap(int i,int j) {
        String temp = "";
        temp = input[i];
        input[i] = input[j];
        input[j] = temp;
    }  
}

Sunday 18 November 2012

Store Credit in JAVA

This program help customer to buy the most expensive products from all the available in the shop. This program takes from user the available amount with him and cost of all the available products in the shop. Than it finally gives the index number of the products that are most suitable.



public class store_credit {
    public static void main(String[] args) {
        int credit = 100; /*This is the available credit to the user.*/
    int total = 3; /*This is the total number of the products in shop.*/
    int price[] = {5,75,25}; /*This is the price array of all the items.*/
    int size = total;
    int temp = 0;
    int i;
    int pos1=0,pos2=0;
    for(i=0;i<size;i++) {
            for(int j=0;j<size;j++) {
                int t = price[i]+price[j];
        if( t > temp && t <= credit && i != j) {
                    temp = t;
                    pos1 = i + 1;
                    pos2 = j + 1;
        }
        }
    }
        System.out.println("Case#1: "+pos1 + " "+pos2);
    }
}

Reverse String Dramatically in JAVA

Input    : my name is nisarg mehta
Output : mehta nisarg is name my


public class reverse_string {
    public static void main(String args[]) {
        String s = "my name is nisarg mehta";
        String output = "";
        int space = 4;
        String temp = "";
        int i = s.length();
        int j = i;
        while(space >= 0) {
            while(i>=1 && s.charAt(--i) != ' ') {
            }
            temp = s.substring(i,j);
            j = i;
            output += " " + temp;
            temp = "";
            space--;
        }
        System.out.println("Case#1 : "+output);
    }
}

Sunday 28 October 2012

Change Background Color using Jslider in Java

This program changes the background color using the three Jslider Red, Green and Blue. it also directly change to three basic color Red, Green, Blue using JpopupMenu using right click.

Here I attach one screenshot of it.




Monday 8 October 2012

Rail Fence Cipher with Decryption

RAIL FENCE Cipher with Decryption



#include<conio.h>
#include<stdio.h>
void main()
{
    int i,j,k=0,l=0,m=0;
    char s[20],a[10],b[10],s1[10],s2[10];
    char decry[20];
    clrscr();
    printf("enter a string:");
    scanf("%s",s);
    for(i=0;i<strlen(s);i++)
    {
        if(i%2==0)
        {
            a[k]=s[i];
            k++;
        }
        else
        {
            b[l]=s[i];
            l++;
        }
    }
    for(i=0;i<k;i++)
    {
        printf("%c ",a[i]);
        s[m]=a[i];
        m++;
    }
    for(i=0;i<l;i++)
    {
        printf(" %c",b[i]);
        s[m]=b[i];
        m++;
    }
    printf("\n");
    k=0;

    printf("\n\ncipher text is %s",s);

    //DEVCRYPTION CODE...

    printf("\n\nTaking above string as input to this code...\n\n");
    l=0;
    //s1[];
    //s2[];
    for(i=0;i<strlen(s)/2;i++) //FOR s1
    {
        s1[k++]=s[i];
    }
    k=0;
    for(;i<strlen(s);i++) s2[k++]=s[i];
    k=0;
    l=0;
    for(i=0;i<strlen(s1)+strlen(s2);i++)
    {
        if(i%2==0)
        {
            s[i]=s1[k];
            k++;
        }
        else
        {
            s[i]=s2[l];
            l++;
        }
    }
    printf("\nFINAL OUTPUT : %s",s);
    getch();
}


Saturday 1 September 2012

Loops in PYTHON

>>> x=0
>>> i=0
>>> a=['a','b','c','d']
>>> for x in a:
    print(x)

   
a
b
c
d






>>> while i < 10:
    print(i)
    i+=1

   
0
1
2
3
4
5
6
7
8
9

Friday 31 August 2012

Start PYTHON Today...

Input & Output:


>>> name=input("Enter your name  ")
Enter your name  nisarg

>>> interest=input("What is your interest  ")
What is your interest  programming

>>> define=input("How do you define your self   ")
How do you define your self   codekiller

Value of the variable defined....

>>> print(name)
nisarg

>>> print(interest)
programming

>>> print(define)
codekiller

Thursday 30 August 2012

Guess the Number game in Python

Below is the program for Guess The Number Game.
Run the program and then choose the number,thereby it will guess it. 
 


 
import random

guesses_made = 0

name = raw_input('Hello! What is your name?\n')

number = random.randint(1, 20)
print 'Well, {0}, I am thinking of a number between 1 and 20.'.format(name)

while guesses_made < 6:

    guess = int(raw_input('Take a guess: '))

    guesses_made += 1

    if guess < number:
        print 'Your guess is too low.'

    if guess > number:
        print 'Your guess is too high.'

    if guess == number:
        break

if guess == number:
    print 'Good job, {0}! You guessed my number in {1} guesses!'.format(name, guesses_made)
else:
    print 'Nope. The number I was thinking of was {0}'.format(number)

Friday 3 August 2012

MULTIPLICATION TABLE IN SERVLET

JSP File :



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="mul.do" method="post">
        Select the number :
        <select name="num" >
            <optgroup label="Choose the number" >
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
               
            </optgroup>
        </select>
        <input type="submit" />
        </form>
    </body>
</html>


Servlet File :


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class mul extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
             int no = Integer.parseInt(request.getParameter("num"));
             int i;
             for(i=1;i<=10;i++)
             {
                 out.println(no + " * " + i + " = " + (no*i) + "<br>");
             }
             
        } finally {            
            out.close();
        }
    }

    /* <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

Thursday 2 August 2012

Program to Print Header Information in Servlet


JSP File : 


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <form action="header_info.do">
            <input type="submit" value="Go" />
        </form>
    </body>
</html>

Servlet File : 



import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class header_info extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
             Enumeration one = request.getHeaderNames();
           
             while (one.hasMoreElements())
             {
                 String one1 = one.nextElement().toString();
                 out.println(one1 + "="+request.getHeader(one1)  +"<br>");
             }
           
           
        } finally {          
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}


Wednesday 1 August 2012

A simple VB .net Program using class and subroutine



Public Class area
Dim a As Integer
Dim b As Integer
Dim area As Integer

Public Sub getdata()

Console.WriteLine("Enter The Length a = ")
a=Convert.ToInt32(Console.ReadLine())

Console.WriteLine("Enter The width b = ")
b=Convert.ToInt32(Console.ReadLine())
End Sub

Public Function getarea() As Integer
area = (a * b)
return area
End Function


End Class

Module Module1
Sub Main()
Dim s1 As New area
s1.getdata()
Console.WriteLine("Area is {0}",s1.getarea() )

End Sub
End Module

Java simple Checkbox Program


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JLabel;


public  class ckbox extends JApplet implements ItemListener {

JLabel msg;
JCheckBox cb,cb1,cb2,cb3;

public void init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
msg = new JLabel("you have selected ..");
cb = new JCheckBox("Java");
cb.addItemListener(this);
cb1 = new JCheckBox("Dot Net");
cb1.addItemListener(this);
cb2 = new JCheckBox("XYZ");
cb2.addItemListener(this);
cb3 = new JCheckBox("All");
cb3.addItemListener(this);

add(cb3);
add(cb);
add(cb1);
add(cb2);
add(msg);


}

@Override
public void itemStateChanged(ItemEvent ie)
{

JCheckBox temp = (JCheckBox)ie.getItem();

if(temp.isSelected() == true )
{
msg.setText("you have selected"+temp.getText());
}
else
{
msg.setText("you have deselected"+temp.getText());
}


if(temp.getText().equals("All"))
{
if(temp.isSelected()==true)
{

cb1.setSelected(true);
cb2.setSelected(true);
cb.setSelected(true);
}
else
{

cb1.setSelected(false);
cb2.setSelected(false);
cb.setSelected(false);
}
}



}
}

Database Connectivity in Java


This program is for database connectivity in java
follow the steps
1) first create the database in MS ACCESS (DATABASE + TABLE)
2) write a program and execute program
3) In program SagarDB is database name and DemoTable is table name in MS ACCESS.

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.*;

public class DemoDbconnect
{
public static void main (String[] args)
{
       try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("jdbc:odbc:SagarDB");
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("select * from DemoTable;");

while(rs.next())
{
System.out.println(rs.getString(1)+"   " + rs.getString(2)+"   " + rs.getString(3)+"   ");
}
cn.close();
       }
 

catch(Exception e)
{
e.printStackTrace();
}
     }
}

Friday 27 July 2012

How to create a python server ?

Initially,you must have Python installed.

After the installation,do following.

Open a notepad file and type the following code there:

from http.server import HTTPServer, CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer((”, port), CGIHTTPRequestHandler)
print(“Starting simple_httpd on port: ” + str(httpd.server_port))
httpd.serve_forever()

Save it and remember the location of it.

Thereby,

For Windows user

1.Go to Start –>All Programs–>Python 3.2–>IDLE
2.Press Ctrl+O to open the file saved above.
3.Press F5.



Above window with message “Staring simple_httpd on port 8080″ shows success of our work.

For Unix And Mac OS X Users

You need to do two things to prepare your CGI script for execution:

1. Set the executable bit for your CGI using the chmod +x command.
2. Add the following line of code to the very top of your program:
#! /usr/local/bin/python3

Thereby do another thing:
1. Open Terminal (or Shell).
2. Type python

That’s it,server is running on your *NIX OS.

That’s all how a simple “home-made” Python server is made and run,simply with above 4 steps.Send your queries below in the comments.

Thursday 12 July 2012

Windmill effect in Java Applet




Code:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.lang.Math;

/**
 *
 * @author zero
 */
public class NewApplet extends Applet implements Runnable {
    Dimension d = getSize();
    int b = 200;
    int c = 200;
    int a = 0;
    int x = (int)(100 * Math.cos(Math.toRadians(a)));
    int y = (int)(100 * Math.cos(Math.toRadians(a)));
    int i = 1;
    public void run() {
        while(true) {
         
            for(i = 90; i >= 0; i--) {
                try {
                Thread.sleep(5);
                }
                catch(Exception e) {
                }
                a = i;
                x = (int)(100 * Math.cos(Math.toRadians(a)));
                y = (int)(100 * Math.sin(Math.toRadians(a)));
                //showStatus(x + "," + y);
                repaint();
            }
        }
    }

    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    public void init()
        Thread t = new Thread(this);
        t.start();
    }
 
    public void paint(Graphics g) {
        g.drawLine(b, c, b, c + 200);
        g.drawLine(b, c, b + x, c + y);
        g.drawLine(b, c, b - x, c - y);
        g.drawLine(b, c, c + y, b - x);
        g.drawLine(b, c, c - y, b + x);
        //g.drawRect(0, 0, 550, 550);
    }
 
    // TODO overwrite start(), stop() and destroy() methods
}

Tare Zammen Par effect in Java Applet




Code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.lang.Math;


/*<applet code=NewApplet1' width='500' height='500'></applet>*/
/**
 *
 * @author zero
 */
public class NewApplet1 extends Applet implements Runnable {
    int a, b;
    Dimension d = getSize();
    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    public void init() {
        Thread t = new Thread(this);
        t.start();
    }
   
    public void run() {
        while(true) {
           
                d = getSize();
                try {
                Thread.sleep(1);
                }
                catch(Exception e) {
                }
           
                a = (int)(d.width * Math.random());
                b = (int)(d.height * Math.random());
                showStatus(d.width + "," + d.height);
                repaint();
           
        }
    }
   
    public void paint(Graphics g) {
        g.drawOval(a, b, 1, 1);
    }
   
    public void update(Graphics g) {
        paint(g);
    }
    // TODO overwrite start(), stop() and destroy() methods
}

Saturday 23 June 2012

"Hello World" program in Pyhton

Code :

#!usr/bin/python
#Filename: helloworld.py

print("Hello World !!!")


Output :


Thursday 24 May 2012

Surfing on a html page using iframe

Preview :








Code:


<html>
   <head>
      <script type="text/javascript">
         function surf() {
            document.getElementById('web').src=document.getElementById('url').value;
         }
      </script>


   </head>
   <table>
      <tr>
         <td>
            <input type="text" id="url" /><input type="button" id="sruf" value="surf" onclick="return surf()"/>
         </td> 
      </tr>
   </table>

   <iframe height="400px" width="400px" src="http://www.killthecode.blogspot.com" id="web"> </iframe>    



</html>

Monday 9 April 2012

Line Clipping Algorithm in C



#include<stdio.h>

#include<conio.h>

#include<graphics.h>



void main()

{

 int x1, y1, x2, y2, a1, a2, a3, a4, b1, b2, b3, b4, xmin, ymin, xmax, ymax, qa, qb;

 int gd=DETECT, gm, c1, c2, c3, c4, xint1, yint1, xint2,  yint2;

 float m;

 clrscr();

 printf("enter end-points of line : ");

 scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

 printf("enter co-ord. of screen : ");

 scanf("%d %d %d %d", &xmin, &ymin, &xmax, &ymax);

 if(x1<xmin)

 {

  a3 = 0; a4 = 1;

 }

 if(x1>xmin && x1<xmax)

 {

  a3 = 0; a4 = 0;

 }

 if(x1>xmax)

 {

  a3 = 1; a4 = 0;

 }

 if(y1<ymin)

 {

  a1 = 0; a2 = 1;

 }

 if(y1>ymin && y1<ymax)

 {

  a1 = 0; a2 = 0;

 }

 if(y1>ymax)

 {

  a1 = 1; a2 = 0;

 }

 if(x2<xmin)

 {

  b3 = 0; b4 = 1;

 }

 if(x2>xmin && x2<xmax)

 {

  b3 = 0; b4 = 0;

 }

 if(x2>xmax)

 {

  b3 = 1; b4 = 0;

 }

 if(y2<ymin)

 {

  b1 = 0; b2 = 1;

 }

 if(y2>ymin && y2<ymax)

 {

  b1 = 0; b2 = 0;

 }

 if(y2>ymax)

 {

  b1 = 1; b2 = 0;

 }

 if(a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0)

 {

  qa = 5;

 }

 if(b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)

 {

  qb = 5;

 }

 initgraph(&gd, &gm, "c://tc//bgi");

 c1 = a1&b1;

 c2 = a2&b2;

 c3 = a3&b3;

 c4 = a4&b4;

 if(c1 == 0 && c2 == 0 && c3 == 0 && c4 == 0)

 {

  if(a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0 && b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)

   printf("line is completely inside");

  else

   printf("line is partially inside");

 }

 else

  printf("line is completely outside");







if(x2-x1 == 0 || y2-y1 == 0)

{

 if(x2 - x1 == 0)

 {

  if(qa == 5)

   line(x1, y1, x1, ymax);



  if(qb == 5)

   line(x1, ymin, x2, y2);



  if(y1<=ymin && y2>=ymax)

   line(x1,ymin, x2, ymax);



 }

 if(y2 - y1 == 0)

 {



  if(qa == 5)

   line(x1, y1, xmax, y2);



  if(qb == 5)

   line(xmin, y1, x2, y2);



  if(x1<=xmin && x2>=xmax)

   line(xmin, y1, xmax, y2);



 }

}

else

{

 xint1 = ((x2-x1)*(ymin-y1)/(y2-y1))+x1;

 xint2 = ((x2-x1)*(ymax-y1)/(y2-y1))+x1;

 yint1 = ((xmin-x1)*(y2-y1)/(x2-x1))+y1;

 yint2 = ((xmax-x1)*(y2-y1)/(x2-x1))+y1;



 if(qa == 5 || qb == 5)

 {

  if(qa == 5)

  {

   if(xint2<=xmax)

   line(x1, y1, xint2, ymax);

   if(yint2<=ymax)

   line(x1, y1, xmax, yint2);

  }

  if(qb == 5)

  {

   if(xint1>=xmin)

   line(xint1, ymin, x2, y2);

   if(yint1>=ymin)

   line(xmin, yint1, x2, y2);

  }

 }

 else

 {

  if(xint1>=xmin && xint1<=xmax)

  {

   if(xint2>=xmin && xint2<=xmax)

    line(xint1, ymin, xint2, ymax);



   if(yint2>=ymin && yint2<=ymax)

    line(xint1, ymin, xmax, yint2);



  }

  if(yint1>=ymin && yint1<=ymax)

  {

   if(xint2>=xmin && xint2<=xmax)

    line(xmin, yint1, xint2, ymax);



   if(yint2>=ymin && yint2<=ymax)

    line(xmin, yint1, xmax, yint2);



  }

 }

}



 rectangle(xmin, ymin, xmax, ymax);

 getch();

}

Monday 26 March 2012

Decimal to Hexadecimal convertor (10to16 convertor)using JAVA

 
class extra
{
 public static void main(String args[])
 {
  hexadecimal h=new hexadecimal();
  int a1,a2=0;
  char c;
  a1=Integer.parseInt(args[0]);
  //System.out.println(h.hax(a1,a2));
  h.hax(a1);
 }
}


class hexadecimal
{
 void hax(int a1)
 {
  int b;
  char c;
  b=a1%16;
  a1=a1/16;
  if(a1>16)
  {
   hax(a1); 
  }
  System.out.print(a1);
  if(b==0 && a1<16)
  {
   System.out.print(0);
  }
  if(b>9)
  {
   c=(char)(b+55);
   System.out.print(c);
  }
  else
  {
   System.out.print(b);
  }
  return;
 }
}

Thursday 22 March 2012

Sticky Oval in JAVA

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class sticky_oval extends Applet implements MouseMotionListener {
 int x,y;
 public void init() {
  addMouseMotionListener(this);
 }
 public void mouseMoved(MouseEvent me) {
  x=me.getX();
  y=me.getY();
  repaint();
 }
 public void mouseDragged(MouseEvent me) {
 
 }
 public void paint(Graphics g) {
  try {
   g.drawOval(x,y,x,y);
   if(x==y) {
    g.setColor(Color.blue);
    g.fillOval(x,y,x,y);
    showStatus("THIS IS PERFECT CIRCLE.");
    Thread.sleep(4000);
   } else showStatus("");
  } catch(InterruptedException e) {
  
  }
 }
} 

Saturday 17 March 2012

Calculator using Buttons in JavaScript

Untitled Document


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
.box {
 width:15px;
 height:15px;
}
</style>
</head>

<body>
<script type="text/javascript">
 var no1="",no2="";
 var flag=0;
 var flag2=0;
 var already=0;
 function calc(id,txt)
 {
  
  if(flag==0)
  {
   no1=no1+document.getElementById(id).value;
   document.getElementById(txt).value=no1;
  }
  else if(flag==1)
  {
   no2=no2+document.getElementById(id).value;
   document.getElementById(txt).value=no2;
  }
 }
 function change(id)
 {
  if(already==0)
  {
  flag=1;
  already=1;
  var sign=document.getElementById(id).value;
  
  if(sign=='+')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"+";
   flag2=1;
  }
  if(sign=='-')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"-";
   flag2=1;
  }
  if(sign=='*')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"*";
   flag2=1;
  }
  if(sign=='/')
  {
   if(flag2==0) document.getElementById('one').value=document.getElementById('two').value;
   document.getElementById('one').value=document.getElementById('one').value+"/";
   flag2=1;
  }
  document.getElementById('two').value="";
  }
  
  
 }
 function ans(id)
 {
  var txt1=document.getElementById('one').value;
  var no1=txt1.substr(0,txt1.length-1);
  var no2= document.getElementById('two').value;
  flag=3;
  
  <!--var no2=document.getElementById('two').value;-->
  
  <!--var txt1=document.getElementById('one').value;-->
  <!--var no2=no1.substr(0,txt1.length-2);-->
  no1=parseInt(no1);
  no2=parseInt(no2);
  
  if(txt1.charAt(txt1.length-1)=='+') document.getElementById('ans').value=(no1+no2);
  if(txt1.charAt(txt1.length-1)=="-") document.getElementById('ans').value=(no1-no2);
  if(txt1.charAt(txt1.length-1)=="*") document.getElementById('ans').value=(no1*no2);
  if(txt1.charAt(txt1.length-1)=="/") 
  {
   try 
   {
    if(no2!=0) document.getElementById('ans').value=(no1/no2);
    else  document.getElementById('ans').value="Error while dividing";
   }
   catch(err)
   {
    document.getElementById('ans').value="by Zero is not possible";
   }
  }
  
 }
 function clear1()
 {
  document.getElementById('one').value="no1";
  document.getElementById('two').value="no2"
  document.getElementById('ans').value="answer";
  no1="";
  no2="";
  flag=0;
  flag2=0;
  already=0;
 }
 
</script>

<input type="text" id="one" readonly="readonly" value="no1"/><br />
<input type="text" id="two" readonly="readonly" value="no2"/><br />
<input type="text" id="ans" value="answer" readonly="readonly" />

<table width="30%" cellpadding="0" cellpadding="0">
<tr>
  <td><input type="button" id="01" value="1" onclick="return calc(this.id,'two')"/>
  <input type="button" id="2" value="2" onclick="return calc(this.id,'two')"/>
  <input type="button" id="3" value="3" onclick="return calc(this.id,'two')"/>
  <input type="button" id="add" value="+" onclick="return change(this.id)" />
 </td>
</tr>
<tr>
<td>
  <input type="button" id="4" value="4" onclick="return calc(this.id,'two')"/>
  <input type="button" id="5" value="5" onclick="return calc(this.id,'two')"/>
  <input type="button" id="6" value="6" onclick="return calc(this.id,'two')"/>
  <input type="button" id="sub" value="-" onclick="return change(this.id)" />
</td>
</tr>
<tr>
<td>
  <input type="button" id="7" value="7" onclick="return calc(this.id,'two')"/>
  <input type="button" id="8" value="8" onclick="return calc(this.id,'two')"/>
  <input type="button" id="9" value="9" onclick="return calc(this.id,'two')"/>
  <input type="button" id="mul" value="*" onclick="return change(this.id)" />
</td>
</tr>
<tr>
<td>
 <input type="button" id="c" value="C" onclick="return clear1()"/>
 <input type="button" id="0" value="0" onclick="return calc(this.id,'two')"/>
 <input type="button" id="equal" value="=" onclick="return ans(this.id)"/>
 <input type="button" id="div" value="/" onclick="return change(this.id)" />
</td>
</tr>
</table>


</body>
</html>


Tuesday 13 March 2012

Dynamic Finite Automata for String Validation in C

#include<conio.h>

#include<stdio.h>

void main()

{

 int tab[10][10], i, j, k, t=65, b, r, c, l=0;

 char s[20], t1[50];

 clrscr();

 printf("enter rows and cols : ");

 scanf("%d",&r);

 scanf("%d",&c);

 printf("enter your state transition table : ");

 scanf("%s",t1);

 for(i=0; i<r; i++)

 {

  for(j=0; j<c; j++)

  {

   tab[i][j]=t1[l];

   l++;

  }

 }

 for(i=0; i<r; i++)

 {

  for(j=0; j<c; j++)

  {

   printf("%c", tab[i][j]);

  }

  printf("\n");

 }

 printf("enter a string : ");

 scanf("%s",s);

 for(i=0; i<strlen(s); i++)

 {

  for(k=0; k<r; k++)

  {

   if(t==tab[k][0]) b=k;



  }

  if(s[i]=='0')

  {

   t=tab[b][1];

  }

  if(s[i]=='1')

  {

   t=tab[b][2];

  }

 }

 if(t==tab[r-1][0]) printf("valid");

 else printf("invalid");

 getch();

}

Monday 12 March 2012

Count the number of Checkboxes selected

Untitled Document
Subject one two three four five

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<script type="text/javascript">
var i=0;
function isset(id) {
var n=document.getElementById(id);
if(n.checked==true) {
i++;
}
else {
i--;
}
}
function validate() {
document.getElementById('lbl').innerHTML=i+" Checkbox Selected";
}
</script>


<table width="100%"><br />
<tr>
<td width="20%">Subject </td>
<td> <input type="checkbox" id="1" onclick="return isset(this.id)" />one
<input type="checkbox" id="2" onclick="return isset(this.id)" />two
<input type="checkbox" id="3" onclick="return isset(this.id)" />three
<input type="checkbox" id="4" onclick="return isset(this.id)" />four
<input type="checkbox" id="5" onclick="return isset(this.id)" />five
</td>
<td> <input type="button" onclick="return validate()" value="Check"/> </td>
<td> <label id="lbl"> </label> </td>
</tr>
</table>

</body>
</html>


Sunday 11 March 2012

Palindrome validation in Javascript

Preview :


New Web Project Enter a string :


Code :

<html>

    <head>

        <title>New Web Project</title>

    </head>

    <body>

     

     <script type="text/javascript">

      function pal() {

       var b;

       var a = document.getElementById('str');

       var c1 = a.value.length;

       var flag=0;

       for(b=0; b<c1/2;b++) {

        if(a.value.charAt(b)==a.value.charAt(--c1)) {

         flag=0;

        }

        else {

          flag=1;
          break;

        }

       }

       if(flag!=1) {

        document.getElementById("lbl").innerHTML = "The string is Palindrome";

        document.getElementById("fnt").style.color = "green";

       }

       else {

        document.getElementById("lbl").innerHTML = "The string is not Palindrome";

        document.getElementById("fnt").style.color = "red";

       }

       return;

      }

      

      function foc() {

       var a = document.getElementById("str");

       if(a.value == "enter string...") a.value = "";

       return;

      }

      function foc_lost() {

       var b = document.getElementById("str");

       if(b.value == "") b.value = "enter string...";

       return;

      }

     </script>

     

     

     

     

        Enter a string : <input type="text" name="string" id="str" value="enter string..." onfocus="return foc()" onblur="return foc_lost()"/>

        <input type="button" onclick="return pal()" value="Check"/><br />

        <font id="fnt"><label id="lbl"></label></font>

    </body>

</html>

Thursday 8 March 2012

Draw Table with required rows and cols in JAVA



import java.awt.*;
import java.applet.*;
import java.awt.event.*;


/*
<applet code="mousemotion" width=400 height=400>
</applet>
*/


public class mousemotion extends Applet implements MouseMotionListener,MouseListener {
String name="NISARG";
//String dim=" ";
int mx,my,mx2,my2,mx1,my1,mx3,my3;
int reles=0,dragged=0,drg_reles=0,rls_aftr_drg=0,clr_screen=0;
int ROW=12;
int COLOUM=12;



public void init() {
addMouseMotionListener(this);
addMouseListener(this);
}
public void mouseClicked(MouseEvent me) {
showStatus("MOUSE CLICKED.");
clr_screen=1;
repaint();
}
public void mousePressed(MouseEvent me) {
mx1=me.getX();
my1=me.getY();
}
public void mouseEntered(MouseEvent me) {
showStatus("SHOW STATUS.");
}
public void mouseReleased(MouseEvent me) {
if(dragged==1) {
drg_reles=1;
}
mx2=me.getX();
my2=me.getY();
dragged=0;
reles=1;
repaint();
}
public void mouseExited(MouseEvent me) {
showStatus("MOUSE EXITED.");
}
public void mouseDragged(MouseEvent me) {
showStatus("Mouse is at "+me.getX()+" "+me.getY());
mx3=me.getX();
my3=me.getY();
rls_aftr_drg=1;
//drg_reles=1;
dragged=1;
repaint();
}
public void mouseMoved(MouseEvent me) {
showStatus("Mouse is at "+me.getX()+" "+me.getY());
}
public void paint(Graphics g) {
if(clr_screen==1) {
clr_screen=0;
}
else if(reles==1 && dragged==1) { //MOUSE RELEASED.
g.drawLine(mx1,my1,mx2,my1);
g.drawLine(mx1,my1,mx1,my2);
g.drawLine(mx1,my2,mx2,my2);
g.drawLine(mx2,my1,mx2,my2);
int ROW11=(my3-my1)/ROW;
int ROW111=ROW11;
ROW11+=my1;
int COLOUM11=(mx3-mx1)/COLOUM;
int COLOUM111=COLOUM11;
COLOUM11+=mx1;
for(int i=0;i<ROW;i++) {
g.drawLine(mx1,ROW11,mx3,ROW11);
ROW11+=ROW111;

}
//g.drawLine(mx1,ROW11,mx3,ROW11);
for(int j=0;j<COLOUM;j++) {
g.drawLine(COLOUM11,my1,COLOUM11,my3);
COLOUM11+=COLOUM111;
}
reles=0;
}
else if(rls_aftr_drg==1) { //MOUSE DRAGGED.
g.drawLine(mx1,my1,mx3,my1);
g.drawLine(mx1,my1,mx1,my3);
g.drawLine(mx1,my3,mx3,my3);
g.drawLine(mx3,my1,mx3,my3);
int ROW11=(my3-my1)/ROW;
int ROW111=ROW11;
ROW11+=my1;
int COLOUM11=(mx3-mx1)/COLOUM;
int COLOUM111=COLOUM11;
COLOUM11+=mx1;
for(int i=0;i<ROW;i++) {
g.drawLine(mx1,ROW11,mx3,ROW11);
ROW11+=ROW111;

}
for(int j=0;j<COLOUM;j++) {
g.drawLine(COLOUM11,my1,COLOUM11,my3);
COLOUM11+=COLOUM111;
}
dragged=0;
//rls_aftr_drg=0;
}
if(drg_reles==1) { //MOUSE RELEASED.
g.drawLine(mx1,my1,mx2,my1);
g.drawLine(mx1,my1,mx1,my2);
g.drawLine(mx1,my2,mx2,my2);
g.drawLine(mx2,my1,mx2,my2);
int ROW11=(my3-my1)/ROW;
int ROW111=ROW11;
ROW11+=my1;
int COLOUM11=(mx3-mx1)/COLOUM;
int COLOUM111=COLOUM11;
COLOUM11+=mx1;
for(int i=0;i<ROW;i++) {
g.drawLine(mx1,ROW11,mx3,ROW11);
ROW11+=ROW111;

}
for(int j=0;j<COLOUM;j++) {
g.drawLine(COLOUM11,my1,COLOUM11,my3);
COLOUM11+=COLOUM111;
}
drg_reles=0;
}
}
}

Wednesday 7 March 2012

Ball rolling in the TUNNEL


/* Make the ball float in tunnel and apply password mechanism...*/


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="rolling_fun12" width=1000 height=300>
</applet>
*/


public class rolling_fun12 extends Applet implements Runnable,KeyListener {
String msg="";
String password="nisarg"; /*THIS IS YOUR PASSWORD STRING...*/
char c;
boolean correct=false;
int i=0;
int finish=0;
public void init() {
addKeyListener(this);
requestFocus();
t=new Thread(this);
}
public void keyPressed(KeyEvent ke) {
showStatus("key Pressed.");
/*c=ke.getKeyChar();
check=true;
if(c==password.charAt(i++)) {
msg=msg+c;
}
else {
msg="";
i=0;
}*/
//if(msg.equals(password)) showStatus("VALID PASSWORD.");
//showStatus("key Pressed.");
}
public void keyReleased(KeyEvent ke) {
if(finish==1) showStatus("THE OUTPUT IS ON THE SCREEN.");
else if(msg.equals(password)) {
showStatus("VALID PASSWORD.");
repaint();
}
}
public void keyTyped(KeyEvent ke) {
if(finish==1) showStatus("THE OUTPUT IS ON THE SCREEN.");
else {
showStatus("Key Typed.");
//showStatus("Key Typed.");
c=ke.getKeyChar();
if(c==password.charAt(i++)) {
msg=msg+c;
}
else {
msg="";
i=0;
}
if(msg.equals(password)) {
correct=true;
repaint();
showStatus("VALID PASSWORD.");
}
repaint();
}
}
Thread t;
int start=180;
int end=350;
int x=300;
int y=225;
int end1=0,end2=0,end3=0;
int flag=0,flag2=0;
int slide1=0,drop=0,slide2=0,remain=0,enter=0,once=0;
Color c1=new Color(255,0,255);
/*public void init() {
t=new Thread(this,"first");

}*/

public void start() {
t.start();
}

public void run() {
for(;;) {
if(end3<1460) {
slide2=1;
repaint();
}
else if(end2<1650) {
drop=1;
slide2=0;
repaint();
}
else if(end1<1350) {
drop=0;
slide1=1;
repaint();
}
else {
remain=1;
slide1=0;
slide2=0;
drop=0;
repaint();
}
}
}
public void paint(Graphics g) {
if(correct) {
g.drawLine(15,100,150,100);
g.drawLine(150,100,150,260);
g.drawLine(151,260,325,260);
g.drawLine(200,58,200,218);
g.drawLine(15,58,200,58);
g.drawLine(326,260,326,219);
g.drawLine(326,218,200,218);
if(slide1==1)
{
try {

start=start+15;
end=end+15;
//g.drawArc(x,59,40,40,start,360);
g.setColor(c1);
if(enter==0) {
g.fillArc(x,y,40,40,/*start*/0,360);
enter=1;
}
else {
g.fillArc(x,y,40,40,/*start*/start,353);
enter=0;
}
x=x-5;

if(start>350) {
start=0;
end=350;
}
//if(x>230) x=10;
//if(end1<1350) 
//{
end1+=50;
Thread.sleep(60);
slide1=0;
repaint();
//showStatus("end = "+end);

//}
//flag2=1;
} catch(InterruptedException e) {
showStatus("ERROR");
}
}
if(drop==1) {
try {
start=start+15;
end=end+15;
//x=155;
//y=y+40;
//g.drawArc(x,y,40,40,start,360);
g.setColor(c1);
if(enter==0) {
g.fillArc(x+2,y,40,40,/*start*/0,360);
enter=1;
}
else {
g.fillArc(x+2,y,40,40,/*start*/start,353);
enter=0;
}
y=y-5;

if(start>350) {
start=0;
end=350;
}
//if(y>295) y=150;
//if(end2<700) 
//{
end2+=50;
drop=0;
repaint();
//showStatus("end = "+end);
Thread.sleep(100);
//}
} catch(InterruptedException e) {
showStatus("ERROR");
}
}
if(slide2==1)
{
try {

start=start-15;
end=end+15;
//g.drawArc(x,y-5,40,40,start,360);
g.setColor(c1);
if(enter==1) {
g.fillArc(x,y-5,40,40,/*start*/0,360);
enter=0;
}
else {
g.fillArc(x,y-5,40,40,/*start*/start,353);
x=x-5;
}
if(start>350) {
start=0;
end=350;
}
//if(x>230) x=10;
//if(end1<1350) 
//{
end3+=50;
Thread.sleep(60);
slide1=0;
slide2=0;
repaint();
//showStatus("end = "+end);

//}
//flag2=1;
} catch(InterruptedException e) {
showStatus("ERROR");
}
}
if(remain==1) {
//g.drawArc(x-5,y-5,40,40,0,360);
g.setColor(c1);
finish=1;
if(once==0) {
showStatus("PROGRAM ENDED.");
once=1;
}
g.fillArc(x+5,y,40,40,0,360);
try {
Thread.sleep(500);
} catch(InterruptedException e) {

}
}
}
else {
g.drawString("ENTER VALID PASSWORD.",40,40);
try {
Thread.sleep(3000);
} catch(InterruptedException e) {

}
}
}
}