tag:blogger.com,1999:blog-30907279386919463512023-11-16T05:23:17.716-08:00Vickys Progr@mming environmentvivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-3090727938691946351.post-62519698349584216962016-01-25T14:45:00.003-08:002016-01-25T14:46:30.969-08:00Python Files Demo<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="color: #660000;"># Python files demo</span></b><br />
<b><span style="color: #660000;"><br /></span></b>
<b><span style="color: #660000;"># This function writes data into file</span></b><br />
def fileWrite():<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span># fopen=open("sample.txt","w+")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>with open("sample.txt","w+") as fopen:<span class="Apple-tab-span" style="white-space: pre;"> </span># Trying to open a file using 'with' operator. This automatically closes the file.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>msg=raw_input('\nEnter the string you want to add to file:\t')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fopen.write(msg)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span># fopen.close()<br />
<br />
<b><span style="color: #660000;"># This function reads data from a file</span></b><br />
def fileRead():<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fopen=open("sample.txt","r+")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>msg=fopen.read()<span class="Apple-tab-span" style="white-space: pre;"> </span># You can also specify the read(arg) to some numeric value to read till specific line size.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\nCharacters read from the file: ',msg<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fopen.close()<br />
<br />
<b><span style="color: #660000;"># This function will append data to existing file</span></b><br />
def fileAppend():<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fopen=open("sample.txt","a+")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>msg=raw_input('\n Enter the string you want to append: ')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fopen.write(msg)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span># Reading characters from file after appending<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>str=fopen.read()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print str<br />
<br />
<b><span style="color: #660000;"># This function splits words in a file & also counts # of words</span></b><br />
def fileSplit():<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>with open("sample.txt","r+") as fobj:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>data=fobj.read()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>count=0<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for i in data:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>word=data.split()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print word<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print 'Word count is:',data.count(' ')+1<span class="Apple-tab-span" style="white-space: pre;"> </span><b><span style="color: #660000;"># This function counts number of words in file</span></b><br />
<br />
print '\n *** This program demonstrates various operations on Files****'<br />
print '\n 1. Write to a file \n 2. Read from file \n 3. Append data to file \n 4. Word count in file'<br />
option=input('Enter the option you want to perform:')<br />
<br />
if option==1:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '***Write() will append the data to mentioned file (or) will create a new file ***'<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fileWrite()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
elif option==2:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\n *** Read() function will try to read data from existing file***'<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fileRead()<br />
<br />
elif option==3:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\n *** APpend() function will append data to mentioned file (or) create a new file if file doesn\'t exist'<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fileAppend()<br />
<br />
elif option==4:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\n ****Counting Number of words in file ****'<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fileSplit()<br />
<br />
else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print 'Your option was not right'<br />
<br />
<b><span style="color: #660000;"># Output:</span></b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrx1fodSRL14uvv3PljQO_Ij99pE0hEyCz-SBJ2o2X0LwevWtwlotKB3UbiONQHDOOvUSNvDGdMtFnAVc74idByijT75VvW7PY70WfVwaE0FK1n4zrUSbO5Xka3FU-vTuL8UMEA3AeLkv3/s1600/filesdemo.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrx1fodSRL14uvv3PljQO_Ij99pE0hEyCz-SBJ2o2X0LwevWtwlotKB3UbiONQHDOOvUSNvDGdMtFnAVc74idByijT75VvW7PY70WfVwaE0FK1n4zrUSbO5Xka3FU-vTuL8UMEA3AeLkv3/s320/filesdemo.JPG" width="320" /></a></div>
<b><span style="color: #660000;"><br /></span></b>
<br />
<div>
<br /></div>
</div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-10420738230518787902016-01-12T10:03:00.003-08:002016-01-12T10:05:25.382-08:00Python "Class & module" Example<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="color: #660000;">#*****geometricClass.py*****</span></b><br />
<br />
# Python Geometric class<br />
<br />
class geometryClass:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def __init__(self,length,breath,radius,height,base1,base2):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>self.length=length<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>self.breath=breath<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>self.radius=radius<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>self.height=height<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>self.base1=base1<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>self.base2=base2<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def square(self):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_sqr=self.length ** 2<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return area_sqr;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def rectangle(self):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_rect=self.length*self.breath<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return area_rect;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def circle(self):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_circle=3.14*(self.radius ** 2)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return area_circle;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def triangle(self):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_tri=0.5*self.base1*self.height<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return area_tri;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def parallelogram(self):<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_para=self.base2*self.height<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return area_para;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>def trapezoid(self):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_trap=0.5*self.base1*self.base2*self.height;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return area_trap;<br />
<br />
shape=geometryClass(5,3,2,4,6,3);<br />
print shape.square();<br />
<div>
<br /></div>
<div>
<b><span style="color: #660000;"># *****Shape.py******</span></b></div>
<div>
<div>
# this page imports the Shape class and implements those functions</div>
<div>
<br /></div>
<div>
<b>from geometryClass import *</b></div>
<div>
<br /></div>
<div>
print '\n *** Area of various Geometric shapes***'</div>
<div>
print ' 1. Square \n 2. Rectangle \n 3. Circle \n 4. Triangle \n 5. Parallelogram \n 6. Trapezoid'</div>
<div>
option=input('\n Select the geometric shape to find the area:')</div>
<div>
<br /></div>
<div>
if option==1:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>length=input('Enter the length of square side:')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_sqr=geometryClass(length,0,0,0,0,0);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print ('Area of square:',area_sqr.square());</div>
<div>
elif option==2:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>length=input('Enter the length of rect: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>breath=input('Enter the breadth of rect: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_rect=geometryClass(length,breath,0,0,0,0)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print ('Area of Rectangle:',area_rect.rectangle());</div>
<div>
elif option==3:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>radius=input('Enter the radius of circle: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_circle=geometryClass(0,0,radius,0,0,0)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print ('Area of Circle:',area_circle.circle());</div>
<div>
elif option==4:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>base=input('Enter the base of triangle: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>height=input('Enter the height of triangle: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_tri=geometryClass(0,0,0,height,base,0)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print ('Area of Triangle:',area_tri.triangle());</div>
<div>
elif option==5:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>base=input('Enter the base of parallelogram: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>height=input('Enter the height of parallelogram: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_par=geometryClass(0,0,0,height,0,base)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print (' Area of parallelogram: ',area_par.parallelogram());</div>
<div>
elif option==6:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>base1=input('Enter the base1 of trapezoid: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>base2=input('Enter the base2 of trapezoid: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>height=input('Enter the height of trapezoid: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>area_par=geometryClass(0,0,0,height,base1,base2)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print (' Area of trapezoid: ',area_par.trapezoid());</div>
<div>
else:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print 'No Geometric shape existing with this selection. please try back.'</div>
</div>
<div>
<br /></div>
<div>
<b><span style="color: #660000;">Output</span></b>:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8GU7hqq9SzlXU3H53V9gWMObX0tQj6KxSaGaYbYtxCAIa2EiJwlkTAkQN43-SPELt5rcm95sanMdKhSIgdzE37RkZ6O6cg-QMZmuSKHzATAWSAxcu4U7IBLZsaJNJ-80VOdECSajBpAYU/s1600/classexam_1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="268" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8GU7hqq9SzlXU3H53V9gWMObX0tQj6KxSaGaYbYtxCAIa2EiJwlkTAkQN43-SPELt5rcm95sanMdKhSIgdzE37RkZ6O6cg-QMZmuSKHzATAWSAxcu4U7IBLZsaJNJ-80VOdECSajBpAYU/s320/classexam_1.JPG" width="320" /></a></div>
</div>
</div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-66243562651041106442016-01-11T08:37:00.003-08:002016-01-11T08:38:16.704-08:00Python Functions example<div dir="ltr" style="text-align: left;" trbidi="on">
# Python Function demo<br />
<br />
print '*** Select the operation you would like to perform *****'<br />
print '\n 1. Prime Number between given interval \n 2. Armstrong Number \n 3. Even or Odd \n 4. Factorial Of number \n'<br />
option=input('Option:')<br />
message='********* Welcome to python programming *****'<br />
def test(message):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print message<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
test(message);<br />
<br />
def prime_number(prime_range):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for num in range(2,prime_range+1):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for i in range(2, num):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (num%i==0):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>break<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print num<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
<br />
def armstrongNumber(num):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>temp=num;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>sum=0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>while temp >1:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>rem=temp%10;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>sum+=rem**3<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>temp<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if sum==num:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print (num,'is an Armstrong Number')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print (num,'is not an Armstrong Number')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
<br />
def evenodd(num):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(num%2==0):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print (num,'is Even Number')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print (num,'is Odd Number')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
<br />
def factorial(num):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fact=1<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(num < 1):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\n Factorial doesnt exist for negative numbers'<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>elif num==0:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\n Factorial of 0 is 1'<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for i in range(1, num+1):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fact=fact*i;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print ('\n Factorial of ',num,' is: ',fact)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
<br />
if option==1:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>prime_range=input('\n Enter the range till you want to find prime numbers:')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>prime_number(prime_range);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
elif option==2:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>num=input('\n Enter the number to check if it is armstrong or not: ')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>armstrongNumber(num);<br />
<br />
elif option==3:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>num=input('\n Enter the number to find even or odd: ')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>evenodd(num);<br />
<br />
elif option==4:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>num=input('\n Enter the number to find factorial: ')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>factorial(num);<br />
else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '\n Invalid option'<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0vnW7QzaxytIrTZBr9ab-NZWDAwAfMkIl-2byGXE1ngA5yaoDhQ3tczoBMY1ppDQT9A3GfnZoPQg4-6Ke7V3f9nc-PjZ9miuMnC900ifjAAzkGZ6zXwYD0FtyH0irB5hzSXgqHJaRodUg/s1600/function_1.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0vnW7QzaxytIrTZBr9ab-NZWDAwAfMkIl-2byGXE1ngA5yaoDhQ3tczoBMY1ppDQT9A3GfnZoPQg4-6Ke7V3f9nc-PjZ9miuMnC900ifjAAzkGZ6zXwYD0FtyH0irB5hzSXgqHJaRodUg/s320/function_1.JPG" width="320" /></a></div>
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtsyP-p-xDw_q6o_nGT60gJCytjGSdhulCqcPY7EicxNGZgsT3GaKDHB8xf7MpypDGfhgM7Mw43X6cpkgWLK8jiZj6rLb6lsfKdd1jkWOGvKj2xO4MlG4x2Hli1BpWM4ML33YwQ0C-zBYB/s1600/function_2.JPG" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="202" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtsyP-p-xDw_q6o_nGT60gJCytjGSdhulCqcPY7EicxNGZgsT3GaKDHB8xf7MpypDGfhgM7Mw43X6cpkgWLK8jiZj6rLb6lsfKdd1jkWOGvKj2xO4MlG4x2Hli1BpWM4ML33YwQ0C-zBYB/s320/function_2.JPG" width="320" /></a></div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-17783987140497464762016-01-11T08:30:00.000-08:002016-01-11T08:30:20.464-08:00Python Dictionary example<div dir="ltr" style="text-align: left;" trbidi="on">
# Python dictionary basics operations<br />
<br />
data={'Name':'Viv','id':'yom439','Major':'CS','Degree':'MS'}<br />
<br />
print (data['Name'])<br />
print (data['Major'])<br />
<br />
print ('\n Before adding new key to dictionary');<br />
print("\n",data)<br />
<br />
data['Sem']=3<span class="Apple-tab-span" style="white-space: pre;"> </span># Adding new key to dictionary<br />
<br />
print ('\n After adding new key to dictionary');<br />
print("\n",data)<br />
<br />
#Notes: data.clear() clears all entries in dictionary<br />
#Notes: del data; deletes dictionary<br />
<br />
# Converting lists into Dictionaries<br />
print '\n Converting lists into Dictionaries'<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
states={}<br />
numofstates=int(raw_input('\n Enter number of states'))<br />
for i in range(0, numofstates):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>statename=raw_input('\n Enter the state:')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>states[statename]={}<br />
<br />
capitals={}<br />
for j in range(0,numofstates):<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>capitalname=raw_input('\n Enter the capital name:')<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>capitals[capitalname]={}<br />
<br />
# Step 1: Convert the lists which you want to make dictionary into tuple format using zip()<br />
<br />
state_capital=zip(states,capitals)<br />
print '\n'<br />
print(state_capital)<br />
<br />
# Step 2: Now convert this tuple into dictionary using dict()<br />
<br />
states_and_capitals=dict(state_capital)<br />
print '\n'<br />
print(states_and_capitals)<br />
<div>
<br /></div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOblIo44vOZxgVE9TdDiDp8dQhQkHXyeu8C-Q6fxfC5FKbVjmgqOKJuADL3cUeQN6_401YxXPkjaggYffa0ff5XllVuRe-A6Ka7K_PMYb4JIKJ1pl8n73sSryfNwU-4gVDxs6Lw09KdYLQ/s1600/Dictionary+operations.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="174" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOblIo44vOZxgVE9TdDiDp8dQhQkHXyeu8C-Q6fxfC5FKbVjmgqOKJuADL3cUeQN6_401YxXPkjaggYffa0ff5XllVuRe-A6Ka7K_PMYb4JIKJ1pl8n73sSryfNwU-4gVDxs6Lw09KdYLQ/s320/Dictionary+operations.JPG" width="320" /></a></div>
</div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-55398866078513700792016-01-11T08:21:00.003-08:002016-01-11T08:22:00.049-08:00Python List operations<div dir="ltr" style="text-align: left;" trbidi="on">
<h4 style="text-align: left;">
# <b><span style="color: #660000;">This program holds basic operations performed by list such as 'Adding new entry', 'Inserting list element', 'Extending list'</span></b></h4>
<div>
<div>
# List operations</div>
<div>
stud_names=[]</div>
<div>
stud_id=[]</div>
<div>
cousins=['Vivek','Veerain','Priyanka','Shirisha','Shravya']</div>
<div>
friends=['Abhi','pri','Sammmmmy','Sai','Shaaaa','hemu','adii','sowji','murali','venky','Sanndy']</div>
<div>
<br /></div>
<div>
names=[]</div>
<div>
<br /></div>
<div>
print '***** What action you would like to do ***** \n'</div>
<div>
print '1. Add New Student name & id\n'</div>
<div>
print '2. Insert student details \n'</div>
<div>
print '3. Extend lists \n'</div>
<div>
<br /></div>
<div>
operation=input('Enter the operation you would like to perform: ')</div>
<div>
<br /></div>
<div>
if operation==1:</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span># Use of 'append' keyword</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>sname=raw_input('Enter the student name: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>stud_names.append(sname)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>cousins.append(sname)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>sid=input('Enter student id: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>stud_id.append(sid)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print('Students:',stud_names)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print('Student Id:',stud_id)</div>
<div>
<br /></div>
<div>
elif operation==2:</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span># Use of 'insert' keyword</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>sindex=input('Enter the index position you want to insert the name: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>sname=raw_input('Enter the student name: ')</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>stud_names.insert(sindex,sname);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>cousins.insert(sindex,sname)<span class="Apple-tab-span" style="white-space: pre;"> </span># This is just another example</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print(cousins)</div>
<div>
<br /></div>
<div>
elif operation==3:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print '**************** Full list of cousins and friends would be ************** \n'<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span># Use of 'extend' keyword</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>names.extend(cousins)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>names.extend(friends)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print(names)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print('\n sorted names list: ',names.sort())</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print('\n Length of names list is :',len(names))<span class="Apple-tab-span" style="white-space: pre;"> </span># Use of 'len' function</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>search=raw_input('Enter the name you would like to Search: ')</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span># Use of 'in'& 'not in' keyword</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if search in names:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print 'Search Successful'</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>elif search not in names:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print 'Search unsuccessful'</div>
<div>
else:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>print 'Invalid operation'<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2sJwX_ofOGvyTVaRXuk9XqC_L-6CGe0jVPZyC733NczvxILut1hMNm3toYCvc8IDAdEv40boHorTS2wH5iifPOPB5j6yQ40GE79Q6Sr-SMCH0mxxiZJdBVANIlFYSKwDNoboLbcr5KMGP/s1600/List_exam.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="214" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2sJwX_ofOGvyTVaRXuk9XqC_L-6CGe0jVPZyC733NczvxILut1hMNm3toYCvc8IDAdEv40boHorTS2wH5iifPOPB5j6yQ40GE79Q6Sr-SMCH0mxxiZJdBVANIlFYSKwDNoboLbcr5KMGP/s320/List_exam.JPG" width="320" /></a></div>
</div>
<div style="color: #660000; font-weight: bold;">
<br /></div>
<div style="color: #660000; font-weight: bold;">
<br /></div>
</div>
</div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-3833311092529835132016-01-11T08:08:00.002-08:002016-01-11T08:10:47.329-08:00Sample List example using iterations<div dir="ltr" style="text-align: left;" trbidi="on">
<h4 style="text-align: left;">
# <span style="color: #660000;">Programming illustrating how we can use 'list' with iterations using for loop</span></h4>
<br />
num1=int(raw_input("Enter first number"));<br />
num2=int(raw_input("Enter second number"));<br />
num3=int(raw_input("Enter third number"));<br />
num4=int(raw_input("Enter fourth number"));<br />
num5=int(raw_input("Enter fifth number"));<br />
num6=int(raw_input("Enter sixth number"));<br />
<br />
number=[num1, num2, num3, num4, num5, num6];<span class="Apple-tab-span" style="white-space: pre;"> </span>#this list(number) holds values provided by users and form an input to for loop.<br />
<br />
# these empty lists will hold even and odd number provided by user in their lists<br />
even_num=[]<br />
odd_num=[]<br />
<br />
print ("The provided numbers are:",number);<br />
<br />
for value in number:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if value%2==0:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>even_num.append(value);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>elif value%2!=0:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>odd_num.append(value);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Value is neither even nor odd");<br />
<br />
print('\n List of Even numbers are:',even_num);<br />
<br />
print('\n List of odd number are:',odd_num);<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj01bDxt5uGkkwtI9Iy4C8DPapPgZSeq_9dg845MX2mFwrwPMWPalkwlg1faFtq67SLR9dcgNNeEUx8Mb6dNuDAQaEI8jXpYHcIV7muxY3PBOHAppE8steI_eY6dxdazrWk1alT3q-gdeB4/s1600/odd_eve.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="80" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj01bDxt5uGkkwtI9Iy4C8DPapPgZSeq_9dg845MX2mFwrwPMWPalkwlg1faFtq67SLR9dcgNNeEUx8Mb6dNuDAQaEI8jXpYHcIV7muxY3PBOHAppE8steI_eY6dxdazrWk1alT3q-gdeB4/s320/odd_eve.JPG" width="320" /></a></div>
<br /></div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-26183697361819279492016-01-11T07:57:00.000-08:002016-01-11T08:14:47.303-08:00Decision Making in python<div dir="ltr" style="text-align: left;" trbidi="on">
<h4 style="text-align: left;">
# <b><span style="color: #660000;">A python program that uses 'Conditional statements / Decision Making' and operates on user specified input</span></b></h4>
<br />
num1=input("enter first number:");<span class="Apple-tab-span" style="white-space: pre;"> </span># This will read the input and tries to understand.<br />
num2=int(raw_input("enter second number:"));<span class="Apple-tab-span" style="white-space: pre;"> </span>#raw_input() will convert the provided input to string by default. Ex: try removing typecasting int() and run and provide the operation 1 or 6 and see the difference.<br />
<br />
<br />
print("*****Possible Arithmetic operations*****\n");<br />
print("1. Addition");<br />
print("2. Subtraction");<br />
print("3. Multiplication");<br />
print("4. Division");<br />
print("5. Modulo");<br />
print("6. Exponent");<br />
<br />
operation=input("******Enter the Arithmetic operation you would like to perform******* \n");<br />
<br />
if operation==1:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>addRes=num1+num2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Addition of {0} and {1} is {2} \n".format(num1,num2,addRes));<br />
elif operation==2:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>subRes=num1-num2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Subtraction of {0} and {1} is {2} \n".format(num1,num2,subRes));<br />
elif operation==3:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mulRes=num1*num2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Multiplication of {0} and {1} is {2}\n".format(num1,num2,mulRes));<br />
elif operation==4:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>divRes=float(num1)/float(num2);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Division of {0} and {1} is {2}\n".format(num1,num2,divRes));<br />
elif operation==5:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>modRes=float(num1)/float(num2);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Modulous of {0} and {1} is {2}\n".format(num1,num2,modRes));<br />
elif operation==6:<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>expRes=num1**num2;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>print("Exponent of {0} and {1} is {2}\n".format(num1,num2,expRes));<br />
else:<br />
print("No such option available");<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7PIZvbdvRSgjyfBAeFUPLzsfxov4T7BuFq6_mELvP_JPFpP7TUeJxW588qbwO36WSEn0l22ZCPHue8Gvuhn2f6WEGlShfLKgAtXzcWe_HK56Aero4G1Eg9ed2Ghf1FuculIQtWhsK6tA4/s1600/ALU.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="238" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7PIZvbdvRSgjyfBAeFUPLzsfxov4T7BuFq6_mELvP_JPFpP7TUeJxW588qbwO36WSEn0l22ZCPHue8Gvuhn2f6WEGlShfLKgAtXzcWe_HK56Aero4G1Eg9ed2Ghf1FuculIQtWhsK6tA4/s320/ALU.JPG" width="320" /></a></div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<div>
<br /></div>
</div>
vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-23785872422940738942012-06-22T20:32:00.002-07:002012-06-22T20:32:58.092-07:00Useful RUN Commands<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;">
<span class="hasCaption">Accessibility Controls = access.cpl<br /> Accessibility Wizard = accwiz<br /> Add Hardware Wizard = hdwwiz.cpl<br /> Add/Remove Programs = appwiz.cpl<br /> Administrative Tools = control admintools<br /> Adobe Acrobat ( if installed ) = acrobat<br /> Adobe Distiller ( if installed ) = acrodist<br /> Adobe ImageReady ( if installed ) = imageready<br /> Adobe Photoshop ( if installed ) = photoshop<br /> Automatic Updates = wuaucpl.cpl<br /> Basic Media Player = mplay32<br /> Bluetooth Transfer Wizard = fsquirt<br /> <br /> Calculator = calc<br /> Ccleaner ( if installed ) = ccleaner<br /> C: Drive = c:<br /> Certificate Manager = cdrtmgr.msc<br /> Character Map = charmap<br /> Check Disk Utility = chkdsk<br /> Clipboard Viewer = clipbrd<br /> Command Prompt = cmd<br /> Command Prompt = command<br /> Component Services = dcomcnfg<br /> Computer Management = compmgmt.msc<br /> Compare Files = comp<br /> Control Panel = control<br /> Create a shared folder Wizard = shrpubw<br /> <br /> Date and Time Properties = timedate.cpl<br /> DDE Shares = ddeshare<br /> Device Manager = devmgmt.msc<br /> Direct X Control Panel ( if installed ) = directx.cpl<br /> Direct X Troubleshooter = dxdiag<br /> Disk Cleanup Utility = cleanmgr<br /> Disk Defragment = dfrg.msc<br /> Disk Partition Manager = diskmgmt.msc<br /> Display Properties = control desktop<br /> Display Properties = desk.cpl<br /> Display Properties (w/Appearance Tab Preselected ) = control color<br /> Dr. Watson System Troubleshooting Utility = drwtsn32<br /> Driver Verifier Utility = verifier<br /> <br /> Ethereal ( if installed ) = ethereal<br /> Event Viewer = eventvwr.msc <br /> Files and Settings Transfer Tool = migwiz<br /> File Signature Verification Tool = sigverif<br /> Findfast = findfast.cpl<br /> Firefox = firefox<br /> Folders Properties = control folders<br /> Fonts = fonts<br /> Fonts Folder = fonts<br /> Free Cell Card Game = freecell<br /> <br /> Game Controllers = joy.cpl<br /> Group Policy Editor ( xp pro ) = gpedit.msc <br /> Hearts Card Game = mshearts<br /> Help and Support = helpctr<br /> Hyperterminal = hypertrm<br /> Hotline Client = hotlineclient<br /> <br /> Iexpress Wizard = iexpress<br /> Indexing Service = ciadv.msc<br /> Internet Connection Wizard = icwonn1<br /> Internet Properties = inetcpl.cpl<br /> Internet Setup Wizard = inetwiz<br /> IP Configuration (Display Connection Configuration) = ipconfig /all<br /> IP Configuration (Display DNS Cache Contents) = ipconfig /displaydns<br /> IP Configuration (Delete DNS Cache Contents) = ipconfig /flushdns<br /> IP Configuration (Release All Connections) = ipconfig /release<br /> IP Configuration (Renew All Connections) = ipconfig /renew<br /> IP Configuration (Refreshes DHCP & Re-Registers DNS) = ipconfig /registerdns<br /> IP Configuration (Display DHCP Class ID) = ipconfig /showclassid<br /> IP Configuration (Modifies DHCP Class ID) = ipconfig /setclassid<br /> <br /> Java Control Panel ( if installed ) = jpicpl32.cpl<br /> Java Control Panel ( if installed ) = javaws <br /> Keyboard Properties = control keyboard<br /> <br /> Local Security Settings = secpol.msc<br /> Local Users and Groups = lusrmgr.msc<br /> Logs You Out of Windows = logoff<br /> <br /> Malicious Software Removal Tool = mrt<br /> Microsoft Access ( if installed ) = access.cpl<br /> Microsoft Chat = winchat<br /> Microsoft Excel ( if installed ) = excel<br /> Microsoft Diskpart = diskpart<br /> Microsoft Frontpage ( if installed ) = frontpg<br /> Microsoft Movie Maker = moviemk<br /> Microsoft Management Console = mmc<br /> Microsoft Narrator = narrator<br /> Microsoft Paint = mspaint<br /> Microsoft Powerpoint = powerpnt<br /> Microsoft Word ( if installed ) = winword<br /> Microsoft Syncronization Tool = mobsync<br /> Minesweeper Game = winmine<br /> Mouse Properties = control mouse<br /> Mouse Properties = main.cpl<br /> MS-Dos Editor = edit<br /> MS-Dos FTP = ftp<br /> <br /> Nero ( if installed ) = nero<br /> Netmeeting = conf<br /> Network Connections = control netconnections<br /> Network Connections = ncpa.cpl<br /> Network Setup Wizard = netsetup.cpl<br /> Notepad = notepad<br /> Nview Desktop Manager ( if installed ) = nvtuicpl.cpl<br /> <br /> Object Packager = packager<br /> ODBC Data Source Administrator = odbccp32<br /> ODBC Data Source Administrator = odbccp32.cpl<br /> On Screen Keyboard = osk<br /> Opens AC3 Filter ( if installed ) = ac3filter.cpl<br /> Outlook Express = msimn<br /> <br /> Paint = pbrush<br /> Password Properties = password.cpl<br /> Performance Monitor = perfmon.msc<br /> Performance Monitor = perfmon<br /> Phone and Modem Options = telephon.cpl<br /> Phone Dialer = dialer<br /> Pinball Game = pinball<br /> Power Configuration = powercfg.cpl<br /> Printers and Faxes = control printers<br /> Printers Folder = printers<br /> Private Characters Editor = eudcedit<br /> <br /> Quicktime ( if installed ) = quicktime.cpl<br /> Quicktime Player ( if installed ) = quicktimeplayer<br /> <br /> Real Player ( if installed ) = realplay<br /> Regional Settings = intl.cpl<br /> Registry Editor = regedit<br /> Registry Editor = regedit32<br /> Remote Access Phonebook = rasphone<br /> Remote Desktop = mstsc<br /> Removable Storage = ntmsmgr.msc<br /> Removable Storage Operator Requests = ntmsoprq.msc<br /> Resultant Set of Policy ( xp pro ) = rsop.msc<br /> <br /> Scanners and Cameras = sticpl.cpl<br /> Scheduled Tasks = control schedtasks<br /> Security Center = wscui.cpl<br /> Services = services.msc<br /> Shared Folders = fsmgmt.msc<br /> Sharing Session = rtcshare<br /> Shuts Down Windows = shutdown<br /> Sounds Recorder = sndrec32<br /> Sounds and Audio = mmsys.cpl<br /> Spider Solitare Card Game = spider<br /> SQL Client Configuration = clicongf<br /> System Configuration Editor = sysedit<br /> System Configuration Utility = msconfig<br /> System File Checker Utility ( Scan Immediately ) = sfc /scannow<br /> System File Checker Utility ( Scan Once At Next Boot ) = sfc /scanonce<br /> System File Checker Utility ( Scan On Every Boot ) = sfc /scanboot<br /> System File Checker Utility ( Return to Default Settings) = sfc /revert<br /> System File Checker Utility ( Purge File Cache ) = sfc /purgecache<br /> System File Checker Utility ( Set Cache Size to Size x ) = sfc /cachesize=x<br /> System Information = msinfo32<br /> System Properties = sysdm.cpl<br /> <br /> Task Manager = taskmgr<br /> TCP Tester = tcptest<br /> Telnet Client = telnet<br /> Tweak UI ( if installed ) = tweakui <br /> User Account Management = nusrmgr.cpl<br /> Utility Manager = utilman<br /> <br /> Volume Serial Number for C: = label<br /> Volume Control = sndvol32 <br /> Windows Address Book = wab<br /> Windows Address Book Import Utility = wabmig<br /> Windows Backup Utility ( if installed ) = ntbackup<br /> Windows Explorer = explorer<br /> Windows Firewall = firewall.cpl<br /> Windows Installer Details = msiexec<br /> Windows Magnifier = magnify<br /> <br /> Windows Management Infrastructure = wmimgmt.msc<br /> Windows Media Player = wmplayer<br /> Windows Messenger = msnsgs<br /> Windows Picture Import Wizard (Need camera connected) = wiaacmgr<br /> Windows System Security Tool = syskey<br /> Windows Script host settings = wscript<br /> Widnows Update Launches = wupdmgr<br /> Windows Version ( shows your windows version ) = winver<br /> Windows XP Tour Wizard = tourstart<br /> Wordpad = write <br /> Zoom Utility = igfxzoom</span><br />
<br />
<span class="hasCaption">Temp files deletion in Windows for system fast running: %temp% </span></div>
</div>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-72864727392918455792009-10-23T09:55:00.000-07:002009-12-06T00:14:44.794-08:00PolymorphismAs the same name suggests 'Poly' (many) 'morphos' (forms) an object existing in different forms. In our Programming environment we call it as an "Entity existing in many forms". Resilence to change is the best example of polymorphism. In OOPs we express polymorphism as <span style="font-style: italic; color: rgb(51, 204, 0);">" One Interface, Multiple Functions". </span><br /><br />Talking about kinds of Polymorphism, they are classified into two basic forms<br /><br />1. Static Polymorphism or compile time polymorphism<br />2. Dynamic Polymorphism or run time polymorphism.<br /><br />In our programming environment we can see the importance of polymorphism by creating more than one function in a class with the same name but differing them with the parameter declaration, i.e method overloading is one of the feature where we see the implementation of polymorphism.<br /><br /><blockquote>example: a woman who can be a wife, a daughter,a sister,a mother etc.... is an example of polymorphism.<br /><br /></blockquote><span style="font-weight: bold;">Static Polymorphism</span>:<br /><blockquote><br />"The Mechanism of linking a function to a class object at compile time is called static polymorphism or early binding. "<br /><br /></blockquote><blockquote></blockquote>C# uses two approaches to implement static polymorphism.....<br /><br />1. Function overloading.<br />2. Operator Overloading.<br /><br /><span style="font-weight: bold;">1. Function Overloading: </span><br /><br />Process of defining a function multiple times with same name inside a single class by changing the type of parameters, sequence of parameters, no of parameters.<br /><br /><blockquote>additionFunction(int n1,int n2);<br />additionFunction(float n1, float n2);<br /><br />Here the return type of the functions can be same or they can differ.<br /></blockquote><span style="font-weight: bold;">2. Operator Overloading: </span><br /><br /><blockquote>/*<br /><br />*/<br /><br /></blockquote><span style="font-weight: bold;">Dynamic Polymorphism</span>:<br /><br />In Dynamic polymorphism the decision about function execution is made at runtime. Dynamic polymorphism is more useful as compared to static polymorphism because dynamic polymorphism gives much flexibility for manipulating objects.<br /><br /><blockquote>"The mechanism of linking of a function with an object at runtime is called Dynamic Polymorphism of late binding. "<br /></blockquote>C# uses two approaches to implement dynamic polymorphism,<br /><br />1. Abstract classes .<br />2. Virtual Functions.<br /><br /><span style="font-weight: bold;">1. Abstract classes:</span><br /><br />These are special type of base classes that consists of abstract class members. we can have abstract classes as well as abstract methods, and any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword.<br /><br /><blockquote>We cant create objects for abstract classes i.e ( classname <class> obj=new classname<class>), this kind of declaration is not possible for abstract classes.<br /><br />We can create object references for abstract classes i.e (<class> classname ref; ).<br /><br />We cant declare abstract static methods and abstract constructors.<br /></class></class></class></blockquote><br /><br /><span style="font-weight: bold;">2. Virtual Functions: </span><br /><br />These are the functions which do not really exists, but appear to be present in some parts of the program.<input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML"></div><input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML"></div>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com2tag:blogger.com,1999:blog-3090727938691946351.post-63307766732830546392009-09-16T09:33:00.000-07:002009-10-23T10:43:41.419-07:00Destructors<ul><li>Destructors are the special methods that r used to release the instance of the class from memory. </li><li>The main purpose of it is to perform the memory clean up action. the programmer can not have any control to call the destructor.<br /></li><li>The .Net frame work automatically runs the destructor to destroy objects in the memory.<br /></li><li>A single class can have only one destructor. </li></ul><br /><span style="font-weight: bold;">Declaration of Destructor</span><br /><br /><ul><li>Destructor follows the same rules as that of constructors but the difference comes in the functioning.<br /></li><li>Destructor has same name as class name as its class but is prefixed with a '~' called as tilde operator.<br /></li><li>Destructors can not be inherited or overloaded.<br /></li></ul>example:<br /><br />using System;<br /><br />class Addition<br />{<br /><blockquote>static int n1,n2;<br />int total;<br /><blockquote></blockquote></blockquote>}<br /><br />Addition()<br />{<blockquote>n1=5;<br />n2=3;<br />total=0;</blockquote>}<br />public void operation()<br />{<br /><blockquote>total=n1+n2;<br />Console.WriteLine("The result is {0}", total);</blockquote>}<br />~Addition()/*.............. Destructor.............*/<br />{<br /><blockquote>Console.WriteLine("Destructor invoked");</blockquote>}<br />static void Main(String [] arg)<br />{<br /><blockquote>Addition obj=new Addition();<br />obj.operation();</blockquote>}<br />}<br /><br />note: Even when u dont call the destructor, garbage collector releases the memory.<br /><br /><span style="font-weight: bold;">Garbage Collection</span><br /><br /><ul><li>It is a process that automatically frees the memory space which is used for the objects at the time of program execution.<br /></li><li>The memory for the object is freed only after observing the status of the object (i.e when object is no longer in use).<br /></li><li>The decision to invoke the destructor is made by a special program of C# called as <span style="font-style: italic;">garbage collector. </span></li><li><span style="font-style: italic;">In C#, you can not destroy an object explicitly in code, in fact it has the feature of garbage collection which destroys the objects for the programmers.<br /></span></li><li><span style="font-style: italic;">Garbage collection mainly involves,<br /></span></li></ul><ol><li><span style="font-style: italic;"> objects get destroyed: doesnt specify when the object is destroyed.<br /></span></li><li><span style="font-style: italic;">Only unused objects are destroyed: object is nt deleted unless it holds any reference.<br /></span></li></ol><ul><li>C# uses two special methods to release the instance of a class from memory</li></ul><ol><li>Finalize() </li><li>Dispose()<br /></li></ol><ul><li>Finalize destructor is a special method that is called from the class to which it belongs or from the derived class.<br /></li><li>It is called only after the last reference of the object is released from the memory.<br /></li><li>.Net frame work automatically runs the Finalize() destructor to destroy objects in the memory. and keep in mind that the Finalize method does nt invoke immediately.<br /></li></ul><ul><li>Dispose() method is called to release a resource, such as a database connection as soon as the object using such a resource is no longer in use.<br /></li><li>Unlike the Finalize() destructor , the Dispose() method is not called automatically, and you must explicitly call it from the client application when an object is no longer needed.<br /></li><li>IDisposable interface contains the Dispose() method. So in order to invoke the Dispose() method a class must implement the IDisposable interface.<br /></li></ul>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-22011746716236665902009-09-15T12:40:00.000-07:002009-09-14T09:26:17.931-07:00Constructors and Destructors<span style="font-weight: bold;">Constructors</span><br /><br /><ul><li>These form the special member functions of the class .</li><li> These have the same name as class name and constructors do not return any value .</li><li>A Constructor is complimentary to a destructor. It is a good programming to use both constructors as well as destructors. </li><li>Constructors can be defined in two ways as<blockquote></blockquote></li></ul><blockquote>1. static Constructors.<br />2.Instance Constructors.<br /></blockquote><br /><br /><span style="font-weight: bold;">Static Constructors:</span><br /><ul><li>These r used to intialise static variables of a class. </li><li>These variables r created using <span style="color: rgb(0, 153, 0);">static </span>key word and they store values that can be shared by all the instances of a class . </li><li>Static constructors have an implict private access. </li><li>These ill be invoked only once during execution.</li>exam:<br /><br />public class Example<br />{<br /><br />static int n1;<br />int n2;<br /><br />static Example()<br />{<br />n1=5; /*.................. accepts the value for variable n1............................*/<br /><br />n2=10; /.......................... puts an error msg...........*/<br />}<br />}<br /><br /></ul>note: We use constructors to intialise data members and not for any input or output operations.<br />and we can have more than one constructor for a class.<br /><br /><span style="font-weight: bold;">Constructors with parameters</span><br /><br />At times user wants to pass values to variables at run time where it cant be possible through intializing them at the starting of prog, so during this situation he can make use of passing the values to variables through parameters.<br />constructors can be used in order to supply values to variables at run time. ......<br /><br />here is an exam program to illustrate the process of functioning.........<br /><br />using System;<br /><br />namespace arithemetic_oper<br />{<br />class Addition<br />{<br />static int num1,num2;<br />int total;<br /><br />Addition(int n1,int n2)<br />{<br /> num1=n1;<br /> num2=n2;<br />}<br /><br />public void addNum()<br />{<br />total=num1+num2;<br />}<br /><br />public void displayNum()<br />{<br />Console.WriteLine(" Result is : {0}",total);<br />}<br /><br />public static void Main(String [] arg)<br />{<br />int var1,var2;<br />Console.WriteLine("Enter the var1:\t");<br />var1= Convert.ToInt32(Console.ReadLine());<br /><br />Console.WriteLine("Enter the var2:\t");<br />var2=Convert.ToInt32(Console.ReadLine());<br /><br />Addition obj=new Addition(var1,var2);<br /><br />obj.addNum();<br />obj.displayNum();<br />Console.ReadLine();<br />}<br />}<br />}<br /><br /><blockquote style="font-weight: bold;">Instance Constructors:<br /><br /><ul style="font-weight: normal;"><li>Instance constructor is called when ever instance of a class is created. </li><li>These r used to intialize data members of the class. </li><li>Constructors do not return any values. </li><li>If there r no constructors for a class, then compiler creates a default constructor for itself. </li></ul></blockquote><br /><br /><br /><blockquote></blockquote>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-42132660241674634032009-06-16T20:38:00.000-07:002009-09-14T06:53:07.943-07:00S@mple Progr@ms<blockquote></blockquote>/*............... Sample prog's 1.......................*/<br /><br />using System;<br /><br />class AgeValidation<br />{<br />int age;<br /><br />void accept()<br />{<br /> <blockquote>Console.WriteLine("Enter the Age:");<br /> age=Convert.ToInt32(Console.ReadLine());</blockquote>}<br />void display()<br />{<br /><blockquote>if(age<0)<br />{<br /> Console.WriteLine("Invalid age");<br />}<br />else<br />{<br /> Console.WriteLine("valid age");<br />}</blockquote>}<br />public static void Main(String [] arg)<br />{<br /> AgeValidation obj=new AgeValidation();<br /> obj.accept();<br /> obj.display();<br />}<br />}<br /><blockquote></blockquote>/*................ end of sample prog 1...............*/<br /><br /><br />/*................sample prog 2..................*/<br /><br />using System;<br /><br />class MyTable<br />{<br />int t,i;<br />void accept()<br />{<br /> <blockquote>Console.WriteLine("Enter the table number:");<br /> t=Convert.ToInt32(Console.ReadLine());</blockquote>}<br />void show()<br />{<br /> <blockquote>for(i=1;i>0;i++)<br /> {<br /> Console.WriteLine("{0} * {1} = {2}",i,t,(i*t));<br /> }</blockquote>}<br /><br />public static void Main(string [] arg)<br />{<br /> <blockquote>MyTable obj=new MyTable();<br /> obj.accept();<br /> obj.show();</blockquote>}<br /><blockquote></blockquote>}<br /><br />/*............. End of sample prog 2............*/vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-13421229306429335792009-02-20T00:38:00.000-08:002009-09-14T06:53:26.460-07:00Sw@pping of num's using reference type param.using System;<br /><br />public class Swapping<br />{<br /> void Swap(ref int a, ref int b)<br /> {<br /> int temp;<br /> temp=a;<br /> a=b;<br /> b=temp;<br /> <br /> }<br /><br />static void Main(string [] arg)<br />{<br /> Swapping obj=new Swapping();<br /> int n1,n2;<br />Console.WriteLine("Enter the no's:\t");<br />n1=Convert.ToInt32(Console.ReadLine());<br />n2=Convert.ToInt32(Console.ReadLine());<br /> obj.Swap(ref n1, ref n2);<br /><br />Console.WriteLine("The values of the numbers before swapping is :\t"+n1);<br />Console.WriteLine("The values of the numbers before swapping is :\t"+n2);<br />Console.ReadLine();<br />}<br />}vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-30221395732416969112009-02-19T22:03:00.000-08:002009-02-19T22:55:42.163-08:00Using Methods with P@rametersAs we know parameters allow information to be passed in and out of a method.<br />When you define method, you can include list of parameters in parentheses.<br /><br /><span style="font-weight: bold;">Declaring Methods with parameters</span><br /><br />Each parameter has a type and a name. u can declare parameters by placing parameter declaration inside parentheses. A syntax that is used to declare parameters is similar to the syntax that is used to declare local variables.<br /><br />The syntax for declaring parameters inside methods<br /><blockquote>void Method(int n, string y)<br /> {<br /> // body.<br /> }<br /></blockquote><br /><br />the preceding code declares the method (Method) with out parameters and y. The first parameter is of type int and 2nd of type string.<br /><br /><span style="font-weight: bold;">Calling methods with Parameters</span><br /><br /><span style="font-style: italic;"><span style="font-weight: bold;">value type</span>:<br /><br /></span><span>values r some time passed in parameters, therefore, the data can be transferred into the method but cannot be transferred out.<br /></span><span style="font-style: italic;"><br /><span style="font-weight: bold;">Reference type</span>:<br /><br /></span><span>These r some times called In/Out parameters, therfore, the data can be transferred into the method and out again.</span><span style="font-style: italic;"><br /></span><span><br /><span style="font-weight: bold;">Pass Parameter by value</span></span><span style="font-style: italic;"><br /><br /></span><span>The simplest definition of a value parameter is the data type name followed by a variable name.<br />When a method is called, a new storage location is created for each value parameter. The values of the corresponding expressions are copied into them. The expressions supplied for each value parameter must be similar to the declaration of the value parameter or it must be a type that cane be implicitly converted to the value type .<br /></span><span style="font-style: italic;"><br />example :<br /><blockquote>class calculator<br />{<br />void Addone(int var)<br />{<br />var++;<br />}<br />public static void Main(string [] arg)<br />{<br />Calculator Cal=new Calculator();<br />int number=6;<br />cal.Addone(number);<br />Console.WriteLine(number); //.................. Display value 6 .<br />}<br />}<br /></blockquote></span><span><span style="font-weight: bold;">pass parameter by reference </span></span><span style="font-style: italic;"><br /><br /></span><span>A reference parameter is a reference to a memory location of a data member.<br />Unlike a value parameter, a reference parameter does not create a new storage location., instead reference parameters represents the same location in memory as the variable that is supplied in the method call.<br /></span><span style="font-style: italic;"><br /></span><span>you can declare reference parameter by using the</span><span style="font-style: italic;"> ref </span><span>keyword before the data type, as shown</span><span style="font-style: italic;"> </span><span>in</span><span style="font-style: italic;"> </span><span>the following example:</span><span style="font-style: italic;"><br /><br /><blockquote>class Calculator<br />{<br />void Addone(ref int var)<br />{<br />var++;<br />}<br />public static void Main(string [] arg)<br />{<br />Calculator obj=new Calculator();<br />int number =6;<br />obj.Addone(ref number);<br />Console.WriteLine(number);//....................Displays value 7.<br />}<br />}<br /></blockquote><br /></span><br /><blockquote></blockquote>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-28427197159225622182009-02-04T08:05:00.000-08:002009-02-04T08:15:40.969-08:00Be@t the X@m RulesNow a days kids like ..........undergo stress and tension right from a young age , thanks to fierce competition.The rat race starts from school Every one aims for the first rank.In this process, student get terribly stressed out . The most efficient way to ease out the tension is meditation .When xams are around the corner, take fifteen minute break every two hours while studying to do deep breathing exercises.One can also try visualization where you picture ur self in a pleasant place and breath slowly. Playing instrumental music also elps in de stressing. Having a bath in warm water relxes the muscles and soothes the mind .Exercising for atleast 20 minutes a day is also a must for de-stressing. Here are a few study tips that worked for...:<br /><br />1. Get organized. Use a daily planner for study schedules.<br /><br />2.keep your study area organized, tidy and clutter free,it helps you think better.<br /><br />3. look over ur notes after classs to see if you have any questions and to mark important parts.<br /><br />this working style has helped me a lot at time of exams.vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-56929291139368335142009-01-18T07:49:00.000-08:002009-02-19T20:27:13.375-08:00/*..................Printing student info ......................*/<br /><br />using System;<br /><br />class Student<br />{<br /><br />//Member data.<br />string name;<br />string regno;<br />string add;<br />int age;<br /><br />//Member function<br />void Accept()<br />{<br />Console.WriteLine("Enter the name:\t");<br />name=Console.ReadLine();<br /><br />Console.WriteLine("Enter the regno:\t");<br />regno=Console.ReadLine();<br /><br />Console.WriteLine("Enter the address:\t");<br />add=Console.ReadLine();<br /><br />Console.WriteLine("Enter the age :\t");<br />age=Convert.ToInt32(Console.ReadLine());<br />}<br />void Display()<br />{<br />Console.WriteLine("The name of student is :{0}",name);<br />Console.WriteLine("The regno of student is :{0}",regno);<br />Console.WriteLine("The age of student is :{0}",age);<br />Console.WriteLine("The add of student is :{0}",add);<br />}<br />public static void Main(string [] arg)<br />{<br />Student S=new Student();<br />S.Accept();<br />S.Display();<br />}<br />}<br /><br />/*...................Fibonacci series.................*/<br /><br />using System;<br /><br />class Fibonacci<br />{<br />int f[] =new int[15];<br /><br />void Accept()<br />{<br />f[0]=0;<br />f[1]=1;<br />Console.WriteLine("Enter the num:\t");<br />int num=Convert.ToInt32(Console.ReadLine());<br />}<br /><br />void Display()<br />{<br />for(int i=2;i<=num;i++)<br />{<br />f[i]=f[i-1]+f[i-2];<br />Console.WriteLine(f[i]);<br />}<br />}<br /><br />}<br />class Result<br />{<br />public static void Main(string [] arg)<br />{<br />Fibonacci F=new Fibonacci();<br />F.Accept();<br />F.Display();<br />}<br />}vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-4580876154100591002009-01-17T04:52:00.001-08:002009-01-22T05:21:15.541-08:00Using Methods in C#<blockquote></blockquote>Before coming to methods and there types first we shall know what r the features of <span style="font-style: italic;">Methods</span>....<br /><br /><ul><li>Method is a set of one or more statements or instructions.</li></ul><ul><li>Methods r mainly used for data hiding (i.e) providing the facility of encapsulation and also abstraction.</li><li>They allow the user to implement the concept of <span style="font-style: italic;">reusability .</span></li><li>They play a key role in modular programming <span style="font-style: italic;">,</span>when a application is divided into methods then it ill be easy to maintain the code and also to debug.</li><li>Methods r used for performing repetitive tasks such as <span style="font-style: italic;">fetching specific records and text</span>. They allow user to break an application into discrete logical units, which makes the application more readable.<br /></li><li>We can reuse the code written in a method and can be executed any number of times by calling methods with little change or else no change .</li></ul>To use methods, you need to perform 2 things mainly<br /><ol><li>Define method.</li><li>Call method.</li></ol><span style="font-style: italic; font-weight: bold;">Defining methods</span><span style="font-weight: bold;">:</span><br /><br />defining a method means declaring the elements of its structure. methods r defined with the following syntax......<blockquote><access> <return> <mtd>(para list)<br />{<br />//................Method body;<br />}</mtd></return></access></blockquote><br /><ul><li><span style="font-style: italic;">Access specifier:</span><blockquote>This explains the extent to which a variable and mtd can be accessed.</blockquote></li><li><span style="font-style: italic;">Return type:<blockquote>A method can return any type of value, if method does not return any value then use void as the return type.</blockquote></span></li><li><span style="font-style: italic;">Method name:<blockquote>This plays a prominent role in shaping a class. Method names can not be keywords of the programming language and it is a good practice to follow follow pascal case (eg: Methodone). It can not be same as a variable name also .</blockquote></span></li><li><span style="font-style: italic;">Parameter list:</span><blockquote>This is used to receive and pass the data from one method to other. We can leave the parameter list empty with out any values.</blockquote></li><li><span style="font-style: italic;">Method body:</span><blockquote>Method body mainly contains the code which is required to perform some specific operation.</blockquote></li></ul><span style="font-style: italic;">example:</span><br /><blockquote>class Calculator<br />{<br />public int AddNumber(int n1, int n2)<br />{<br />int res;<br />res=n1+n2;<br />return res;<br />}<br />}<br /></blockquote><span style="font-weight: bold;">Types of Methods</span><span style="font-weight: bold; font-style: italic;"><br /></span><ul><li><span style="font-style: italic; font-weight: bold;">calling Methods</span><blockquote>After defining a method , you can use it by its name. The method name is followed by parentheses even if the method call has no parameters.<br /></blockquote></li></ul>example:<blockquote>using System;<br /><br />class Calculator<br />{<br />public int AddNumber(int n1, int n2)<br />{<br />int res;<br />res=n1+n2;<br />return res;<br />}<br />static void Main(string [] arg)<br />{<br />Calculator C=new Calculator();<br />int total=C.AddNumber(10,20);<br />Console.WriteLine("Te result is :{0}",total);<br />Console.ReadLine();<br />}<br />}<br /></blockquote>Execution process: <ul><li>In the above program the execution starts from main function . so initially memory is allocated dynamically for the Calculator class and a object is created. </li><li>Nw by using the object we r trying to access the code defined inside the method. When the method (AddNumber) is accessed the actual arguments(10, 20) supplied in the method is passed to the formal parameters(int n1,int n2) and these values r stored in n1 and n2.<br /></li><li>since the method uses a return type as int, it is the duty of the user to return a value of type int . so after performing the addition operation the data is returned and the returned value is caught by a variable (total) which is in main function.</li><li>Last but not the least.............. The value is printed.<br /></li><li>Here We have to remember one thing that return type of the main method is 'void ' so it doesnt return any value.</li></ul><span style="font-style: italic;">example:</span><br />/*.................. Recursion method...............*/<blockquote><blockquote>using System;<br /><br />class Number<br />{<br />public int Factorial(int n)<br />{<br />int res;<br />if(n==1)<br />return 1;<br />else<br />{<br />res=factorial(n-1)*n;<br />return res;<br />}<br /><br /></blockquote>}<br />static void Main(string [] arg)<br />{<br />Number obj=new Number();<br />Console.WriteLine("Factorial of 3 is "+obj.factorial(3));<br />Console.WriteLine("Factorial of 4 is "+obj.factorial(4));<br />Console.WriteLine("Factorial of 5 is "+obj.factorial(5));<br />Console.ReadLine();<br />}<br />}</blockquote><br /><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote>In above program, the factorial() method is recursive method. If the value entered by the used is not 1, this method will call itself.vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-1743908275849460882009-01-17T04:27:00.000-08:002009-01-17T05:08:03.096-08:00Example for using access specifiers in C#/*.............. Progr@m to calculate area of rectangle and square.........*/<br /><br />using System;<br /><br />class Area<br />{<br />static int res;//.............declaring static variable.<br /><br />public static void RecArea()//........... defining a static func.<br />{<br />int l, b;<br /><br />Console.WriteLine("Enter the length :\t");<br />l=Convert.ToInt32(Console.ReadLine());<br /><br />Console.WriteLine("Enter the breadth :\t");<br />b=Convert.ToInt32(Console.ReadLine());<br /><br />res=l*b;<br />Console.WriteLine("The area of a rectangle is :{0}",res);<br /><br />// (or) Console.WriteLine("The area of rectangle is :\t"+res);<br />}<br />public static void SqrArea()//........... defining a static func.<br />{<br />int side;<br /><br />Console.WriteLine("Enter the side of the square:\t");<br />side=Convert.ToInt32(Console.ReadLine());<br /><br />res=side*side;<br />Console.WriteLine("The area of a square is :{0}",res);<br />}<br />}<br />class Result<br />{<br />public static void Main(string [] arg)<br />{<br />int option;<br />Area a=new Area();<br /><br />Console.WriteLine("Main Menu");<br />Console.WriteLine("1. Area of Rectangle ");<br />Console.WriteLine("2. Area of Square ");<br />Console.WriteLine("Enter the option :\t");<br />option=Convert.ToInt32(Console.ReadLine());<br /><br />switch(option)<br />{<br />case 1:<blockquote>a.RecArea();<br />break;<br /></blockquote><blockquote></blockquote><blockquote></blockquote>case 2:<blockquote>a.SqrArea();<br />break;<br /></blockquote>default:<blockquote>Console.WriteLine("oops..!.............Wrong choice buddy, u can leave ");<br />break;<br /></blockquote>}<br />Console.ReadLine();<br />}<br />} <br /><br /><span style="font-weight: bold;">Note:</span><br /><br />steps to compile and execute<br /><ol><li>save teh file with .cs extension (i.e) Area.cs </li><li>compilation:<blockquote>csc Area.cs</blockquote></li><li>execution: <blockquote>Area.cs (or) Area.exe<br /></blockquote><br /></li></ol>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-6372850278326069702009-01-14T20:33:00.000-08:002009-01-15T01:21:47.666-08:00Access specifiersAccess specifier defines the scope of a class member. We generally use these access specifiers to give complete security to the data . We use different type of access specifiers to specify the extent of visibility of a class member. In C# we use totally 5 types of access specifiers<br /><br /><ol><li>public<br /></li><li>private<br /></li><li>protected</li><li>internal</li><li>protected internal</li></ol><ul><li><span style="font-style: italic; font-weight: bold;">public access specifier</span><blockquote><ol><li>This access specifier allows the member data and functions of a class to be exposed to other class functions and objects .<br /></li><li>The member which is declared as public can be accessed from outside the class.</li></ol></blockquote></li></ul><span style="font-weight: bold;"> example:</span><br /><br />using System;<br /><br />class Car<br />{<br />private string Car_color;<br />}<br />class Bike<br />{<br />public string bike_color;<br />}<br />class Result<br />{<br />static void Main(string [] arg)<br />{<br />Car Camry=new Car();<br />Bike unicorn=new Bike():<br /><br />Camry.Car_color="white"; //.................................. Error..! cant access private var outside class.<br />unicorn.bike_color="black"; //................................assigns black to variable.<br />Console.ReadLine();<br />}<br />}<br /><br /><span style="font-weight: bold;">example 2:</span><br />using System;<br />class Car<br />{<br />public string color;<br />public void Honk()<br />{<br />Console.WriteLine(" Member function invoked");<br />}<br />}<br />class Result<br />{<br />static void Main(string [] arg)<br />{<br />car Camry =new Car();<br />Camry.Honk();//.............. displays the msg.........................Member function invoked.<br />Console.ReadLine();<br />}<br />}<br /><br /><ul><li><span style="font-style: italic; font-weight: bold;">private access specifier </span><blockquote><ol><li>This access specifier allows the private data of a class to be hidden from out side classes. </li><li>Only the members of the same class r provided permissions to access the private members.</li></ol></blockquote></li></ul><span style="font-weight: bold;">example: </span><br /><br />using System;<br /><br />class Car<br />{<br />private string Model;<br /><br />void Honk()<br />{<br />Console.WriteLine("beep beep....!");<br />}<br />public void setModel<br />{<br />Console.WriteLine("Enter the model num:\t");<br />Model=Console.ReadLine();<br />}<br />public void Display()<br />{<br />Console.WriteLine("The model is :\t");<br />}<br />}<br />class Result<br />{<br />static int Main(string [] arg)<br />{<br />Car Camry=new Car();<br /><br />Camry.setModel(); //..................Prompts user to enter the model..<br />Camry.Display();//...............Displays the model.<br />Camry.Honk();// ................. <span style="font-weight: bold;">can not access private methods out side class</span>.<br />Console.WriteLine(Camry.Model);//...............<span style="font-weight: bold;">can not access private mem outside class.</span><br /><br />return 0;<br />Console.ReadLine();<br />}<br />}<br /><br /><br /><span style="font-weight: bold;">Note:</span><blockquote>When u do not specify any data members as either public, private, protected then the default access specifier is <span style="font-style: italic;">private.</span></blockquote><ul style="font-style: italic;"><li>Protected access specifier:<blockquote><ol><li>This specifier allows a class to expose its member data and functions only to its child classes . </li><li>It hides the data to be accessed from other class objects and functions.</li></ol></blockquote></li></ul><span style="font-weight: bold;">example</span>:<br /><br />using System;<br />class Car<br />{<br />protected string model;<br />void Method()<br />{<br />Console.WriteLine("Member function invoked");<br />}<br />public void setModel()<br />{<br />Console.WriteLine("Enter the model name :\t");<br />model=Console.ReadLine();<br />}<br />void Display()<br />{<br />Console.WriteLine("The model is :\t");<br />}<br />}<br />class Result<br />{<br />static int Main(string [] arg)<br />{<br />Car civic =new Car();<br />civic.Method();//......... <span style="font-weight: bold;">Error!...can not access this member as it is a protected member.</span><br />civic.setModel();//....... accepts the input.<br />civic.Display();//.......Error!...private member can not be accessed.<br />Console.WriteLine(civic.model);//.....<span style="font-weight: bold;">protected members can not be accessed.</span><br /><br />return 0;<br />Console.ReadLine();<br />}<br />}<br /><ul><li><span style="font-style: italic;"><span style="font-weight: bold;">internal access specifier</span>:</span><br /><ol><li><span style="font-style: italic;"><blockquote>Any member that is declared internal can be accessed from any class or method defined within an application in which the member is defined.</blockquote></span></li><li><span style="font-style: italic;"><blockquote>When u do not specify any class class as either public, private, protected then the default access specifier for a class is <span style="font-weight: bold;">internal</span>.</blockquote></span></li></ol></li></ul><span style="font-weight: bold;">example:</span><br /><br />using System;<br />class Bike<br />{<br />private string col;<br /><br />internal void Method()<br />{<br />Console.WriteLine("Method invoked");<br />}<br />}<br />class Result<br />{<br />static void Main(string[] arg)<br />{<br />Bike Fz=new Bike();<br />Console.WriteLine(Fz.col);//...........Error!...can not access private data.<br />Fz.Method();//............Displays method invoked.<br />Console.ReadLine();<br />}<br />}<br /><br /><span style="font-weight: bold;">Note:<blockquote></blockquote></span>the main difference between <span style="font-weight: bold;">public</span> and <span style="font-weight: bold;">internal</span> is<br /><ol><li>public is visible to objects of other class outside the namespace collection and also to the child classes outside the namespace collection.</li><li>But internal access specifier is not visible to objects of other class outside the namespace collection and also to the child classes outside the namespace collection.</li></ol><ul><li><span style="font-style: italic; font-weight: bold;">protected internal access specifier:</span><blockquote><ol><li>This specifier allows a class to hide its mem var, mem funct, to be accessed from other class objects and functions, except the child class ,within the application.</li><li>This access specifier ecomes important while implementing inheritance.</li></ol><span style="font-weight: bold;"></span></blockquote></li></ul><span style="font-weight: bold;">example</span>:<br /><br />using System;<br /><br />class Car<br />{<br />protected internal string col;<br />void Method()<br />{<br />Console.WriteLine(" Method invoked");<br />}<br />}<br />class Result<br />{<br />static int Main(string [] arg)<br />{<br />Car audi =mew Car();<br />audi.Method();//........Error!... private function.<br />Console.WriteLine(audi.col);//......<span style="font-weight: bold;"> cannot access protected internal members outside the class .</span><br /><br />return 0;<br />Console.ReadLine();<br />}<br />}<br /><ol><li>this Acc.Spec has the similar properties as that of <span style="font-style: italic;">internal </span>and the only dfference is that it can not be visible to objects of other class within the same namespace.<br /></li></ol><br /><br /><br /><span style="font-weight: bold;"><br /><blockquote></blockquote></span><blockquote></blockquote><blockquote></blockquote><br /><br /><blockquote></blockquote>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-766249969390926832009-01-04T04:49:00.000-08:002009-01-15T01:23:32.314-08:00Concepts of Object Oriented Programming<span style="font-style: italic;"></span>Object Oriented Programming mainly deals with concepts such as Abstraction, Encapsulation, Polymorphism, Inheritance.<br /><span style="font-style: italic;"><br /></span><br /><span style="font-style: italic; font-weight: bold;">Abstraction:</span><br /><ul><li>Abstraction mainly involves extraction of only relevant information. </li><li>The concepts of abstraction and encapsulation mainly deals with the member functions of the class. </li><li>abstraction can be explained in other words as ' <span style="font-style: italic;">Looking for what u want</span> ' in an object or a class.</li><li>after reading this summary of abstratcion one gets a doubt " does abstraction mean that information is unavailable" .....! No, It means that all the information exists, but only the relevant information is provided to the user.</li><li>we shall learn more abt abstraction by considering the below given example...</li></ul><span style="font-style: italic; font-weight: bold;">example:</span><br /><br />Anautomobile salesperson is aware that different people have different preferences. Some r intrested in the mileage of the car, some in its price, some in its engine, and some in its style.<br /><br />So every individual have diffferent views and wants . Although every individual is intrested in diff aspects of the car finally tey all come to the category of buying a car. The sales man knows the details of the car , but he presents only the relevant information to potential to the customer. As a result, the salesman practices abstraction and presents only relevant details to customer.<br /><br /><br /><span style="font-weight: bold;"><span style="font-style: italic;">Encapsulation</span>:</span><br /><br /><ul><li>Encapsulation means packing of one or more components together. </li><li>It short encapsulation means ' to enclose it or as if in a capsule' . </li><li>Encapsulation can be defined as " the process of enclosing one or more items within a physical or logical package ", it involves preventing access to non-essential data.</li></ul><span style="font-style: italic; font-weight: bold;">example:</span><br /><br />When you plug in the cord of a vacuum cleaner and turn on the switch, the vacuum cleaner starts. You do not see the complex process needed to actually convert electricity into suction power. In other words , the exact working of the cleaner has been encapsulated. Therefore, encapsulation is also explained as information hiding or data hiding because it involves hiding many of the important details of an object from the user.<br /><br />Abstraction and encapsulation are realated features. abstraction enables you to make relevant information visible. Encapsulation enables you to package information to impent the desired level of abstaraction. Therefore, encapsulation assists abstraction by providing a means of suppressing the non essential details.<br />the feature of encapsulation is achieved by access specifiers............<br /><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com2tag:blogger.com,1999:blog-3090727938691946351.post-47151001392747752482008-12-31T21:38:00.000-08:002008-12-31T21:49:01.498-08:00Quiz questions1. static void main(String [] arg)<br />{<br />char ch;<br />Console.WriteLine("Enter the character:\t");<br />ch=Convert.ToChar(Console.ReadLine());<br /><br />if(ch=='U')<br />Console.Writeline("The character is X");<br />else<br />Console.WriteLine("The char is not X");<br />Console.ReadLine();<br />}<br /><br />wats the o/p of the above code .............................?<br /><br />2. To which category does the operator ++, = belong.........?<br /><br />3. In the statement using System, System is a.........................<br /><br />4. which of the following data type is not a value type.............?<br /><ul><li>char</li><li>int</li><li>float</li><li>string</li></ul>5.The .............. compiler is used for C#.<br /><ul><li>cc</li><li>csc</li><li>cs</li></ul>6.In which of the following datatypes does the Console.readLine() function accept a value.........?<br /><ul><li>int<br /></li><li>float</li><li>bool</li><li>string</li></ul>7. Console is a ..........<br /><ul><li>class</li><li>function</li><li>namespace</li></ul>8.In any program, the execution of the program starts from ........<br /><br /><span style="font-style: italic;">Note:</span><br />Give the answers by there respective question numbers.vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-6998837269212250542008-12-31T21:16:00.000-08:002008-12-31T21:36:51.908-08:00Break @nd Continue st@tementsSome times we need to exit out of the loop with out performing next iteration. SO in such case we make use of break and continue statements.<br /><br /><span style="font-style: italic;">Break statement:</span><br /><ol><li>break statement is used in order to exit from the loop.</li><li>It Terminates a statement sequence in a switch statement.</li></ol><span style="font-style: italic;">Continue statement</span>:<br /><ol><li> Used to skip all the subsequent instructions and shift the cursor position back to the loop.</li><li>Here the compiler tries to iterate the loop when ever the condition is satisfied .</li></ol><span style="font-style: italic;">Example:</span><br /> /*....................... sum of +ve numbers..........................*/<br />using System;<br /><br />class Break_Continue<br />{<br />static void Main(string [] arg)<br />{<br />int num,sum,i;<br />for(sum=num=i=0;i<5;i++)<br />{<br />Console.WriteLine("Enter the +ve number:\t");<br />num=Convert.ToInt32(Console.ReadLine());<br /><br />if(num<=0) //............checks for +ve numbers.<br />continue;<br />sum=sum+num; //..............adds only +ve numbers.<br />}<br />Console.WriteLine("The sum of +ve numbers:\t"+sum); //..... Displays result of +ve numbers.<br />}<br />}vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-62481316226255418902008-12-29T06:24:00.000-08:002008-12-31T21:37:15.961-08:00Program to create Fibonacci seriesusing System;<br /><br />class Fibonacci<br />{<br />public static void Main(string [] arg)<br />{<br />int num;<br />int f[] =new int[15];<br />Fibonacci()<br />{<br />f[0]=1;<br />f[1]=1;<br />}<br />void accept()<br />{<br />Console.WriteLine("Enter the number :\t");<br />num =Convert.ToInt32(Console.ReadLine());<br />}<br />for(int i=2; i<=num;i++) { f[i]=f[i-1]+f[i-2]; Console.WriteLine(f[i]); } } } <span style="font-weight: bold; font-style: italic;">Note:</span><br /><br />When u write a program using C sharp u have to save the file with ' .cs' extension.<br /><br />example: Fibonacci.cs<br /><br /><span style="font-style: italic;">compilation mtd</span>: csc Fibonacci.cs<br /><br /><span style="font-style: italic;">execution mtd</span>: Fibonacci.csvivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-83461106430741832582008-12-25T00:45:00.000-08:002008-12-28T03:45:45.462-08:00Loop Constructs in C#We have three types of loop constructs<br />1. while.<br />2. do...while .<br />3. for.<br /><ul><li><span style="font-weight: bold;">while loop </span><ol><li>This construct is generally used to execute a block of statements for a definite number of times, depending on the condition. <span style="font-style: italic;"></span></li><li><span style="font-style: italic;">while</span> statements checks the condition before executing the statements within the loop.</li><li> When the execution reaches the last line of the block the control is transferred to the condition of while and if the condition is satisfied then the statements are executed again and when the condition sets to false it comes out the loop.</li></ol></li></ul><span style="font-style: italic; font-weight: bold;">syntax</span>:<span style="font-style: italic;"></span><span style="font-style: italic;"><blockquote></blockquote></span><span style="font-style: italic;"></span><span style="font-style: italic;"></span><span style="font-style: italic;"></span><blockquote><span style="font-style: italic;">while(expression)</span><br /><span style="font-style: italic;">{</span><br /><span style="font-style: italic;">statements;</span><br />}<br /></blockquote><ul><li>do..while loop</li></ul> 1. Both Iterate until the specified loop condition becomes false.<br /> 2. however, in do...while loop , the body of the loop is executed at least once and the condition is checked for next subsequent iterations.<br /> 3. The do...while loop construct is similar to the while loop constructs.<br /> <br /><span style="font-style: italic;">syntax:</span><br /><blockquote>do<br />{<br />statements;<br />}while(expression);</blockquote><br /><span style="font-style: italic;">example:</span><br /><br /><blockquote>int var=10;<br />do<br />{<br />Console.WriteLine("value of var is :{0}",var);<br />var=var+10;<br />}<br />while(var<20);</blockquote><ul><li><span style="font-weight: bold;">for loop<br /></span></li></ul><ol><li>for loop is used to execute a block of statements for a specific number of times.</li><li>Here the main difference between for and while is mainly in syntactical format. The syntax used for loop is ......</li></ol>Syntax:<br /><blockquote>for(initialization; condition; increment/decrement)<br />{<br />statements;<br />}</blockquote><span style="font-style: italic;">example:</span><br /><br /><blockquote>for(int i=1;i<=10;i++) { Console.WriteLine("{0}",i); }</blockquote><span style="font-style: italic;">Note</span>:<br />We can follow different syntax's for <span style="font-style: italic;">for loop<blockquote>for(; ;)<br />{<br />statements;<br />}<br /></blockquote></span><blockquote></blockquote> <blockquote></blockquote><ol></ol><blockquote></blockquote><ul></ul><ul></ul>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0tag:blogger.com,1999:blog-3090727938691946351.post-82192047695978961162008-12-20T23:43:00.000-08:002008-12-21T08:16:39.363-08:00Conditional ConstructsConditional constructs are used to control the flow of program . conditional constructs allow the flow selective execution of statements , depending on the value of the expressions associated with them. The comparison operators are required for evaluating the conditions . various conditional constructs are :<br /><br />1. if....else construct.<br />2. switch... case construct.<br /><br /><ul><li>if....else construct <blockquote style="font-style: italic;">if...else conditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison. syntax is<br /></blockquote></li></ul> if (expression)<br /> {<br /> statements;<br /> }<br /> else<br /> {<br /> statements;<br /> }<br /><span style="font-style: italic;"> <span style="font-weight: bold;">example</span>:</span><blockquote></blockquote><br />class Number<br />{<br />static void Main(string [] arg)<br />{<br />int num;<br /><br />Console.WriteLine("Enter the number: \t");<br />num=Convert.ToInt32(Console.ReadLine());<br /><br />if(num%!=0)<br />{<br />Console.WriteLine(" Number is odd");<br />}<br />else<br />{<br />Console.WriteLine(" Number is even");<br />}<br /><br /><br />if....else constructs can be nested inside each other. This process of using multiple if....else constructs is known as <span style="font-style: italic;">cascading if ...else</span>. consider the example :<blockquote>int n1=5;<br />int n2=6;<br />int n3=7;<br />if(n1>n2)<br />if(n1>n3)<br />{<br />Console.WriteLine("Number 1 is great");<br />}<br />else<br />{<br />Console.WriteLine("number 3 is great");<br />}<br />else if(n2>n3)<br />{<br />Console.WriteLine("number 2 is great");<br />}<br />else<br />{<br />Console.WriteLine("number 3 is great");<br />}<br /></blockquote><ul><li>switch...case construct<br /></li></ul>This construct is used when there are multiple values for a single variable. syntax for this construct is<br /><blockquote>switch(variable name)<br />{<br /> case option: statement;<br /> break;<br /> case option: statement;<br /> break;<br /> default: statement;<br />}<br /></blockquote>vivekhttp://www.blogger.com/profile/11975717829465802718noreply@blogger.com0