Tuesday 30 September 2014

How to create a user defined exception in Java?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program. When an error occurs within a method (member function), the method creates an object of the error/exception and hands it off to the runtime system.

Java provides better exception handling mechanism for its programmers. It simplifies the exception handling process and gives a try .... catch ... finally stucture to handle the exception.

Sunday 28 September 2014

Content Management System (CMS)

A Content Management System (CMS) is a web application that makes creation and management of websites very easy by making creation, modification, organization of content and publishing the website on a public or private web server.  CMSs provide a central interface to manage the website. Such systems of content management make easy handling of workflow in a collaborative environment.

Some popularly used CMSs are:

    WordPress
    Joomla
    Drupal
    Plone

Saturday 27 September 2014

Assebmly language program to display a message

Assembly Language Programming is an indigenous programming method used for PCs. An assembly language programmer has to understand the internal architecture of the Processor and he has to program accordingly.

The program given here is a simple example for printing a message on the console.

Before going for assembly language programming make sure that you have installed TASM or MASM assembler in your system.

Then type your program using any editor and save it with an extension .asm.
(see program hello.asm.)

Thursday 25 September 2014

C Program to check Leap Year

A leap year is a year containing one additional day in order to keep the calendar year synchronized with the astronomical or seasonal year.

For example, in the Gregorian calendar, each leap year lasts 366 days instead of the usual 365, by extending February to 29 days rather than the common 28 days.

For example:
1991 is not  a leap year.
2000  is a leap year.
1900  is not a leap year.
2004 is  a leap year.

C Program for generating Fibonacci Series

Fibonacci series is a series of integer numbers where the first two numbers in the series are 1 and 1, or 0 and 1, depending on the chosen starting point of the series, and each subsequent number is the sum of the previous two numbers  ie. Fn = Fn-1  +  Fn-2.


For example:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ….

Or

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ….

Thursday 18 September 2014

How to create a class in Java?

This example shows how to define and use a class in Java. Let us consider a rectangle class which has two properties and four methods.

Saturday 13 September 2014

Samsung has launched its new Phablet Galexy Note 4



Samsung has introduced its new Phablet Galexy Note 4 in a Consumer Electronics Fare, I. F. A, in Berlin, Germany. It comes with Quad HD Display -  2560 x 1440 resolution -  with AMOLED  screen. The newborn gadget is designed to compete with  Apple iPhone 6.

SanDisk Premieres World’s Highest Capacity SD Card for High Performance Video and Photo Capture

SanDisk in a press release announces the launch of the world’s highest capacity SD card (SanDisk Extreme PRO SDXC UHS-I memory card) for the video and photographic professionals.

 SanDisk Extreme Pro UHS-I SDHC/SDXC memory cards are available in various capacities like 128GB, 256GB, and 512GB worldwide. The company announced that the 512GB Extreme Pro SDXC UHS-I card will be available at $799.99 (Rs. 48,000 approximately).

Friday 12 September 2014

C program to check if a given matrix is a sparse matrix or not

A sparse matrix is a matrix in which most of the elements are zero.

For example let us consider two matrices  A and B

Matrix A
1 0 0
1 1 0
0 1 0

Matrix B
1 1 1
0 1 0
1 0 1

Wednesday 10 September 2014

Java Program to implement stack using array

A stack is a data structure which is a  logical representation of  a real physical stack or pile of objects, where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of the data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. In a stack last added item is deleted first and so it is called a LIFO (Last In First Out) data structure.


C Program to implement a Queue using array

Queue 
 A queue is a a linear collection of objects, where objects are inserted and removed according to the first-in first-out (FIFO) principle. An example of a queue is a line of people in a ticket counter. New additions to the line is made to the back of the queue, while removal (or serving) happens in the front. In a queue only two operations are allowed enqueue/insert and dequeue/delete. Enqueue means to insert an item into the back of the queue, dequeue means deleting the front item. 


Tuesday 9 September 2014

How to upload a file using PHP script



In this article let us discuss how to upload a file to a web server using PHP script. It is implemented as a function in this example in upload.php file. To upload a file, you have to create an HTML form with a file input control by using the input tag like this: "<input type=file>". 

While creating the HTML form, enctype attribute of the form should have the value "multipart/form-data". So the form tag will be like this:

<form action="upload.php" method="post"  enctype="multipart/form-data">


HTML code to crate the form


<form action="upload.php" method="post" autocomplete="off" enctype="multipart/form-data">
        <table align="center" width="100%">
           
            <tr bgcolor="#ababee">
                <td colspan="2" align="center"><h2>Upload your file</h2></td>
            </tr>
            <tr>
                <td >Select file to upload</td>
                <td ><input type="file" name="file" id="file" style="left:0;">               
                </td>
            </tr>   
                          
           
            <tr>
                <td colspan="2">               
                <input type="submit" name="upload" value="Upload">
                </td>
            </tr>                
           
        </table>       
   </form>


Database Connection using VB.Net class

This Class, implemented in VB .Net, is  used to connect and execute queries on Microsoft SQL Server database. It uses Data.SqlClient library to perform database operations.

Methods implemented in this class

C Program to implement stack using array


A stack is a data structure which is a  logical representation of  a real physical stack or pile of objects, where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of the data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. In a stack last added item is deleted first and so it is called a LIFO (Last In First Out) data structure.


Wednesday 3 September 2014

C Program to check a number is prime or composite

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime because 1 and 5 are its only positive integer factors, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6.

Examples of prime numbers are:  2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,.....

Let us see a function to check whether a number is prime or not.

// Function to check prime returns 0 or 1
int isprime(int value) {
    int i;
    for (i=2;i<=value/2;i++)
        if (value%i==0)
            return (0);
    return 1;
}

Here value is the number to be checked given as parameter to the function. This function returns 1 if the value prime and returns 0 if it is not.

C Program to check whether an array is sorted or not

To sort an array there are different methods. And you may be familiar with them. However, what to do if you want to simply check whether an array is sorted or not.

Method 1
Start from the beginning of the array and compare whether the 0th element is less than 1st element and so on up to the end of the array (for Ascending order). If you can move up to the end of the array without any failure in the condition, then you can assure that the array is sorted.

Method 2
You can apply the reverse logic of method 1. Initially check whether the 0th element is greater than 1st element. If this condition is true, then you can say it is not sorted without checking further.

Let us see a function using the second method to check whether an array is sorted or not.

int issorted(int array[], int len){
    int i;
    for(i=0;i<len-1;i++)
        if (a[i]>a[i+1])
            return (0); //Unsorted

    return (1); //Sorted

}

Here array[] is the actual array and len is the length of the array. This function returns 1 if the array is sorted and returns 0 if it is not sorted.

C Program to Insert an element in an array

How to insert an element in a given position in an array. It is very simple, if you want insert an element at nth position, you want to move or shift the elements right to the nth location by one place and make the nth location free. Then you can just assign the new value to the nth location.

Let us see a function implemented in C Language for inserting an element in an array.

void insert(int array[],int value,int position,int len)
{
    int i;
    for (i=len + 1 ; i>=position;i--)
        a[i]=a[i-1];
    a[position-1]=value;
    return;
}

Here array[] is the actual array where insertion is to be made. value is the new value that has to be inserted and position is the location where the value is to be inserted. len is the existing length of the array.

C Program to check Amicable Numbers

Amicable numbers are two different numbers so related that the sum of the proper divisors of first number is equal to the other number and vice-versa. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.).

For example, the smallest pair of amicable numbers is (220, 284); for the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.

The first few amicable pairs are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368).

Following is a C language program to check the given two numbers are amicable or not.


#include <stdio.h>
#include <conio.h>

int  main() {
    int i, sf1=0, sf2=0,  m, n;

    clrscr();
    printf("Enter the first number :");
    scanf("%d", &m);
    printf("Enter the second number :");
    scanf("%d",&n);

    if (m==n) {
       printf("Given numbers are not amicable");
       getch();
       return 0;
    }

   / * Finds sum of proper divisors of the first number (m) */
    for (i=1;i<=m/2;i++){
        if (m%i==0)
            sf1=sf1+i;
        }

 / * Finds sum of proper divisors of the second number (n) only if sum of proper divisors of first number is equal to the second number */

    if (sf1==n)
        for (i=1; i<=n/2;i++){
            if (n%i==0)
                sf2=sf2+i;
        }

     if (sf2==m)
        printf ("Given numbers are amicable");
     else
        printf("Given numbers are not amicable");

     
     getch();
     return 0;
}

Monday 1 September 2014

Sample code for implementing "check availability option in registration form" using Ajax, PHP and MySQL



This example shows how you can include a ‘check availability’ option in a registration form using Ajax, PHP and MySQL. Using HTML input element, user enters the email id and submits to the server side PHP script ‘isavailable.php’ using Ajax. ‘isavailable.php’ script checks whether the email id is already available in the database table or not and returns the status as responseText.


checkavailability.php

This PHP file contains the form and the Ajax script to submit the email id to the server side PHP script (isavailable.php) and display the response on the page.


<!DOCTYPE html>
<html>
    <head>
        <title>Check Availability - Ajax, PHP and MySQL </title>
        <script>
        function checkavailability(){

        var xmlhttp;

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari

            xmlhttp=new XMLHttpRequest();

        }

        else {// code for IE6, IE5

            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

        }



        xmlhttp.onreadystatechange=function() {

            if (xmlhttp.readyState==4 && xmlhttp.status==200) {

                document.getElementById("divInfo").innerHTML=xmlhttp.responseText;

            }

        }



        url = "isavailable.php";

        param = document.getElementById('txtemail').value;                       

        url = url + "?txtemail=" + param;

       

               

        xmlhttp.open("GET",url,true);

        xmlhttp.send();

    }
</script>

</head>
   
<body>


<form>
    <table align="center">
        <tr>
            <td colspan="2"><h2>Check Availability</h2></td>
        </tr>
        <tr>
        <td>Email</td>
        <td><input type="text" size="40" id="txtemail" name="txtemail"></td>
        </tr>
       
        <tr>
        <td></td>
        <td><input type="button" id="cmdcheck" name="cmdcheck" value="Check Availability" onclick="checkavailability()"></td>
        </tr>
       
        <tr>
            <td colspan="2"><div id="divInfo"></div></td>       
        </tr>
       
    </table>
</form>


</body>
</html>




isavailable.php

 This PHP file extracts the email id submitted to the server from $_REQUEST array and check whether this id is available in the table by using PHP mysql functions. The availability status is sends back to the requested page.


<?php
    $email = $_REQUEST['txtemail'];

    $dblink=mysql_connect("localhost","root","");
    mysql_select_db("dbcheckavailability", $dblink);

    $sql="SELECT * FROM tblmember WHERE email='" . $email . "';";

    $result=mysql_query($sql, $dblink);

    $responseText ="";
   
   
    if (mysql_num_rows($result)) {
        $responseText = "Sorry, <b>" . $email . "</b> is already in use!";
    }
    else {
        $responseText = "Congratulation, <b>" .  $email . "</b> is available!";
    }
  
    echo $responseText;
    mysql_close($dblink);  
?>



Table Structure

Thsis is the structure of the table that has to be created in MySQL for this example.

--
-- Database: `dbcheckavailability`
--

-- --------------------------------------------------------

--
-- Table structure for table `tblmember`
--

CREATE TABLE IF NOT EXISTS `tblmember` (
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `email` varchar(40) NOT NULL,
  UNIQUE KEY `email` (`email`)
);


INSERT INTO `tblmember` (`firstname`, `lastname`, `email`) VALUES
('anand', 'kumar', 'csnotes32@gmail.com');



Output