Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Sunday, March 22, 2009

Introduction to Object Oriented Programming (OOPS) in PHP

<!--phpoops.php--->

<html>
<title>PHP OOPS EXAMPLE</title>
<body>

<?php
unset($username);
unset($password);
?>
<form action="oopsformprocess.php" method=post>
<div align="center">
<p align="center">
<b>PHP OOPS EXAMPLE</b><br><br>
<table valign="center">
<tr>
<td>User Name</td>
<td><input type=text name=username></td>
</tr>
<tr>
<td>Password</td>
<td><input type=password name=password></td>
</tr>
<tr>
<td><input type=submit value="Submit"></td>
<td><input type=reset value="Reset"></td>
</tr>

</table>
</p>
</div>
</form>
</body>
</html



<!--oopsformprocess.php-->
<html>
<?php

$u=$_POST['username'];
$p=$_POST['password'];



new User($u,$p);

class User
{
var $username;
var $password;

function User($username,$password)
{
$this->username=$username;
$this->password=$password;

$valid=$this->check_login();

if($valid==true)
{
echo "Valid login";
}
else
{
echo "Invalid Login";
}

}

function check_login()
{
if($this->username=="anand" && $this->password=="anand")
{
return true;
}
else
{
return false;
}
}
}

?>

</html>

Arrays in PHP -- Part II

<html>
<?php
$vowels=array('a','e','i','o','u');

print "<br><br>Printing for the first time after using the current() and next() function<br>";
print "<br>Array value is ". current($vowels);
while($array_value=next($vowels))
{
print "<br>Array value is ". $array_value;
}

reset($vowels);

print "<br><br>Printing for the second time after using the reset() function<br>";
print "<br>Array value is ". current($vowels);
while($array_value=next($vowels))
{
print "<br>Array value is ". $array_value;
}

print "<br><br>Printing for the third time after using the end() and previous() function<br>";
print "<br>This is reverse iteration<br>";

print "<br>Array value is ". end($vowels);

while($array_value=prev($vowels))
{
print "<br>Array value is ". $array_value;
}

print "<br><br>Printing the keys of array<br>";

reset($vowels);
current($vowels);
print "<br> Key is ". key($vowels);

while(next($vowels))
{
print "<br> Key is ". key($vowels);
}


print "<br><br>Iteration using each() function<br>";

reset($vowels);
while($array_value=each($vowels))
{
$key=$array_value['key'];
$value=$array_value['value'];

print "<br> Key is : ". $key ." and Value is : ".$value;
}

?>
</html>

Introduction to Arrays in PHP -- Part I

<html>

<?php

echo "Creating array using array()<br>";

$vowels=array('a','e','i','o','u');

echo "<br>Output is<br>";
for($i=0;$i<5;$i++)
{
print "<br>".($i+1)." ".$vowels[$i];
}

echo "<br><br>Sequential adding of array elements<br>";

$vowels[0]="a";
$vowels[1]="e";
$vowels[2]="i";
$vowels[3]="o";
$vowels[4]="u";

echo "<br>Output is<br>";
for($i=0;$i<count($vowels);$i++)
{
print "<br>".($i+1)." ".$vowels[$i];
}

unset($vowels);
echo "<br><br>Sequential adding of array elements without mentioning indices<br><br>";

$vowels[]="a";
$vowels[]="e";
$vowels[]="i";
$vowels[]="o";
$vowels[]="u";

echo "<br>Output is<br>";
for($i=0;$i<count($vowels);$i++)
{
print "<br>".($i+1)." ".$vowels[$i];
}

echo "<br><br>Specifying non numeric indices in arrays<br>";

$vowels=array('first'=>'a',
'second'=>'b',
'third'=>'c',
'fourth'=>'d',
'fifth'=>'e');


echo "<br>Output is<br>";

print "<br>First => ".$vowels['first'];
print "<br>Second => ".$vowels['second'];
print "<br>Third => ".$vowels['third'];
print "<br>Fourth => ".$vowels['fourth'];
print "<br>Fifth => ".$vowels['fifth'];


echo "<br><br>Using range() function in arrays<br><br>";

$range_eg=range(2,4);

for($i=0;$i<count($range_eg);$i++)
{
print "Range of array at index " . $i. " is " . $range_eg[$i]."<br>";
}


echo "<br>Using list() function in arrays<br>";

$vowels=array('a','e','i','o','u');
list($first,$second,$third,$fourth,$fifth)=$vowels;

echo "<br>Output is<br>";

print "<br>First => ".$first;
print "<br>Second => ".$second;
print "<br>Third => ".$third;
print "<br>Fourth => ".$fourth;
print "<br>Fifth => ".$fifth;


echo "<br><br>Using in_array() function<br>";

if(in_array('a',$vowels)==true)
{
echo "<br>a is vowel<br>";
}
else
{
echo "<br>a is not a vowel<br>";
}

if(in_array('z',$vowels)==true)
{
echo "<br>z is a vowel<br>";
}
else
{
echo "<br>z is not a vowel<br>";
}



?>

</html>

Friday, March 6, 2009

Implementing Daemon Thread in Java

//DaemonThread.java


class Daemon implements Runnable
{
Global g;

public Daemon(Global g)
{
this.g=g;
}

public void run()
{
while(true)
{
if(g.getGlobalVar()==5)
{
System.out.println("Daemon Thread detected that the value of variable is now 5");
}
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}

public class DaemonThread extends Thread
{
public static void main(String args[])
{
Global g=new Global();

Daemon d=new Daemon(g);

Thread t=new Thread(d);
t.setDaemon(true);
t.start();

for(int i=0;i<10;i++)
{
g.setGlobalVar();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(g.getGlobalVar());
}
}
}

class Global
{
int globalVar;

public Global()
{
globalVar=0;
}

public void setGlobalVar()
{
globalVar++;
}

public int getGlobalVar()
{
return globalVar;
}


}

/*Output
C:\Program Files\Java\jdk1.6.0_10\bin>java DaemonThread
1
2
3
4
5
Daemon Thread detected that the value of variable is now 5
6
7
8
9
10

*/

Implementing Thread Exchanged in Java

//ThreadExchange.java


import java.util.concurrent.Exchanger;

public class ThreadExchange extends Thread
{

public static void main(String args[])
{
Exchanger<String> dataEx=new Exchanger<String>();

Send s=new Send("My Name is Anand",dataEx);

Receive r=new Receive(dataEx);



new Thread(s).start();
new Thread(r).start();
}

}

class Send implements Runnable
{
String data=null;
Exchanger<String> dataEx=new Exchanger<String>();

public Send(String data,Exchanger<String> dataEx)
{
this.data=data;
this.dataEx=dataEx;
}



public void run()
{
try
{
String dataStore=data;
System.out.println("Sending Data : " + data);
data=dataEx.exchange(data);
System.out.println("Sent Data : " + dataStore);

}
catch(InterruptedException e)
{
System.out.println(e);
}
}

}

class Receive implements Runnable
{
String data;
Exchanger<String> dataEx;

public Receive(Exchanger<String> dataEx)
{
this.dataEx=dataEx;
}

public void run()
{

try
{
data=dataEx.exchange(new String());
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("Recieved : " + data);
}
}

/*Output
C:\Program Files\Java\jdk1.6.0_10\bin>java ThreadExchange
Sending Data : My Name is Anand
Recieved : My Name is Anand
Sent Data : My Name is Anand

*/