Find us on Google+ Kill the code: August 2012

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();
}
     }
}