tag:blogger.com,1999:blog-54128780851983310852024-03-14T02:18:25.270-07:00KodingExamplesAnonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.comBlogger16125tag:blogger.com,1999:blog-5412878085198331085.post-8976569756590538022014-07-14T05:05:00.001-07:002014-11-16T09:36:55.266-08:00Arrow controlled object motion in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Hi Guys!!<br />
In this post you will learn how to control a sprite's movement in C# with Arrow Keys<br />
Below is the Code for arrow controlled object motion. Its easier than it sounds check it out guys.<br />
<br /></div>
</div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9: namespace KeyControlledMotion
10: {
11: public partial class Form1 : Form
12: {
13: static int xpos , ypos , circleWidth , circleHeight;
14: static int panelWidth,panelHeight;
15: static SolidBrush bgBrush , circleBrush;
16: static Rectangle backgroundRect;
17: public Form1()
18: {
19: xpos = ypos = 0;
20: circleWidth = circleHeight = 50;
21: panelWidth = 315;
22: panelHeight = 295;
23: backgroundRect = new Rectangle(0, 0, panelWidth, panelHeight);
24: circleBrush = new SolidBrush(Color.SteelBlue);
25: bgBrush = new SolidBrush(Color.Black);
26: InitializeComponent();
27: }
28: private void Form1_Load(object sender, EventArgs e){ }
29: private void Form1_MouseHover(object sender, EventArgs e){}
30: private void panel1_MouseHover(object sender, EventArgs e)
31: {
32: Cursor.Hide();
33: }
34: private void panel1_Paint(object sender, PaintEventArgs e)
35: {
36: Graphics g = panel1.CreateGraphics();
37: g.FillRectangle(bgBrush, backgroundRect);
38: g.FillEllipse(circleBrush,xpos,ypos,circleWidth,circleHeight);
39: }
40: void reDrawBG(Graphics gr) {
41: gr.FillRectangle(bgBrush,backgroundRect);
42: }
43: public void move(SolidBrush sb,int x,int y) {
44: Graphics gr = panel1.CreateGraphics();
45: reDrawBG(gr);
46: gr.FillEllipse(sb,x,y,50,50);
47: }
48: private void Form1_KeyDown(object sender, KeyEventArgs e)
49: {
50: if (e.KeyCode == Keys.Left)
51: {
52: if(xpos-2 >= 0)
53: xpos -= 2;
54: move(circleBrush,xpos,ypos);
55: }
56: else if (e.KeyCode == Keys.Right)
57: {
58: if(xpos < panel1.Width-circleWidth)
59: xpos += 2;
60: move(circleBrush, xpos, ypos);
61: }
62: else if (e.KeyCode == Keys.Up)
63: {
64: if(ypos >= 0)
65: ypos -= 2;
66: move(circleBrush, xpos, ypos);
67: }
68: else if (e.KeyCode == Keys.Down)
69: {
70: if(ypos < panel1.Height-circleHeight)
71: ypos += 2;
72: move(circleBrush, xpos, ypos);
73: }
74: }//keyDown event
75: }//class
76: }//namespace</code></pre>
<br />
<h4 style="text-align: left;">
Explanation : </h4>
In <b>keyDown Event</b> i have used if else conditions to check which key is pressed down after certain statements are executed if a condition is true. For Example if the down key is pressed down the circle will move 2 pixels down and same is the case with left , right and up keys.<br />
<br />
The method <b>reDrawBG</b> is just used to redraw the background after every keyDown(left,down,right,up).<br />
<br />
The method <b>move</b> is used to move or update the circle's position after each <b>keyDown</b>.<br />
All the global variables are initialized inside the constructor.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<b>Output:</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<b><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtt1Iq-o0Aoj3KvZEV0R2DJLEvc4VlLC19M6zXLDz454VxTo9chffrEFFwu70W6wJZfIYOvHJ35AFoeKpbii7Jf5uoHi2raodvEKxzO3RV4bwhubDzJ4Yl-7gS6EOld7dacsYPYGuW3VOz/s1600/Arrow+controlled+object.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtt1Iq-o0Aoj3KvZEV0R2DJLEvc4VlLC19M6zXLDz454VxTo9chffrEFFwu70W6wJZfIYOvHJ35AFoeKpbii7Jf5uoHi2raodvEKxzO3RV4bwhubDzJ4Yl-7gS6EOld7dacsYPYGuW3VOz/s1600/Arrow+controlled+object.jpg" height="400" width="360" /></a></b></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
Download the Complete Project <span style="background-color: #0b5394;"><a href="https://drive.google.com/file/d/0B1AuZzdiP8vSRm1WMHlwakhoMUk/edit?usp=sharing" rel="nofollow" target="_blank">Arrow controlled object</a></span> <span style="background-color: #3d85c6;"><span style="background-color: #3d85c6;"></span></span></div>Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-78103830452902086902014-06-21T13:35:00.001-07:002014-07-15T10:50:14.799-07:00How to make TicTacToe in Java?<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Things You will Learn:</h2>
<div>
<ul style="text-align: left;">
<li>Make tictactoe in Java/C#</li>
<li>Deciding a winner in TicTacToe</li>
<li>TicTacToe pseudo code</li>
</ul>
<h3 style="text-align: left;">
Pseudo Code :</h3>
</div>
<div>
<ol style="text-align: left;">
<li>Player selects a box i.e '1' </li>
<li>If that box is already filled with any player symbol gives an error and repeats the current iteration</li>
<li>If its not filled already,that box is filled with the current player's symbol</li>
<li>when the the number of filled boxes is greater than or equal to 5 the decidewinner method is called to check if we have a winner right now continues until all the boxes are filled.</li>
</ol>
</div>
<div>
<br /></div>
<div>
<h4 style="text-align: left;">
Here is the code :</h4>
</div>
<div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">1: import java.util.Scanner;
2: class TicTacToe{
3:
4: Scanner in = new Scanner(System.in);
5:
6: int choosedbox , iteration ;
7:
8: char player1Symbol = 'x' , player2Symbol = '0' , currentSymbol = 'x' , box[] = {'0','1','2','3','4','5','6','7','8','9'};
9:
10: String p1 = "Player1", p2 = "Player2" , currentPlayer = p1;
11:
12: //*******draws a tic tac toe board on the screen*******
13: public void Board(){
14:
15: System.out.println( " " + box[1] + " | " + box[2] + " | " + box[3] );
16: System.out.println( " ___|___|____");
17: System.out.println( " | |");
18: System.out.println( " " + box[4] + " | " + box[5] + " | " + box[6] );
19: System.out.println( "____|___|____");
20: System.out.println( " | |");
21: System.out.println( " " + box[7] + " | " + box[8] + " | " + box[9]);
22: System.out.println();
23: System.out.println();
24:
25: }// *********drawying gameboard finished*********
26:
27: // ********Starts playing********
28: public void startPlay() {
29:
30: Board(); // calling method "Board"
31:
32: System.out.println( " The Symbol for player1 is 'x' and for player2 is '0 " ) ;
33:
34: out: // titled or named for loop with name out
35: for( iteration = 0; iteration < 9; iteration++ )
36: { //body of the loop starts
37: if( iteration % 2 == 0 ){
38: currentSymbol = player1Symbol;
39: currentPlayer = p1;
40: }
41: else
42: {
43: currentSymbol = player2Symbol;
44: currentPlayer = p2;
45: }
46:
47: System.out.println( currentPlayer + " turn please select a box= " );
48:
49: choosedbox = in.nextInt(); //asks the user to enter the box he want to choose
50:
51: if( box[choosedbox] == player1Symbol || box[choosedbox] == player2Symbol )
52: {
53: System.out.println( " ERROR,This box is already filled select another box " );
54: iteration = iteration - 1 ; //Repeat the current iteration if box selected by the user is already filled
55: continue out ;
56: } //continues the loop and do not move to else if statement
57:
58: box[choosedbox] = currentSymbol; // filling the selected box with the symbol
59:
60: Board();
61:
62: if(iteration >= 4) //decideWinner(); is only called when five boxes of the gameboard are filled by symbols of the players
63: decideWinner();
64:
65: }//end of for loop
66:
67: }//end of method
68:
69: public void decideWinner(){
70:
71: if( box[1] == box[2] && box[2] == box[3] && box[3] == currentSymbol)
72: {
73: System.out.println( currentPlayer + " Wins." );
74: iteration = 9;
75: }
76:
77: else if( box[4] == box[5] && box[5] == box[6] && box[6] == currentSymbol)
78: {
79: System.out.println( currentPlayer + " Wins." );
80: iteration = 9;
81: }
82:
83: else if(box[7] == box[8] && box[8] == box[9] && box[9] == currentSymbol)
84: {
85: System.out.println( currentPlayer + " Wins. " );
86: iteration = 9;
87: }
88:
89: else if( box[1] == box[4] && box[4] == box[7] && box[7] == currentSymbol )
90: {
91: System.out.println( currentPlayer + " Wins." );
92: iteration = 9;
93: }
94:
95: else if( box[2] == box[5] && box[5] == box[8] && box[8] == currentSymbol)
96: {
97: System.out.println( currentPlayer + " Wins." );
98: iteration = 9;
99: }
100:
101: else if( box[3] == box[6] && box[6] == box[9] && box[9] == currentSymbol)
102: {
103: System.out.println( currentPlayer + " Wins." );
104: iteration = 9;
105: }
106:
107: else if( box[1] == box[5] && box[5] == box[9] && box[9] == currentSymbol)
108: {
109: System.out.println( currentPlayer + " Wins." );
110: iteration = 9;
111: }
112:
113: else if( box[3] == box[5] && box[5] == box[7] && box[7] == currentSymbol)
114: {
115: System.out.println( currentPlayer + " Wins." );
116: iteration = 9;
117: } //finished calculating the winner
118:
119: if( iteration < 9 && iteration > 4 )
120: System.out.println( " No Result so Far Continue Playing...... " );
121:
122: else if(iteration == 8)
123: System.out.println( " The Game finished and its a TIE!!!!! " );//prints this if game is Draw
124:
125: }
126: // MAIN Method
127: public static void main(String argss[]){
128: TicTacToe game = new TicTacToe();
129: game.startPlay();
130: }
131: }
</code></pre>
<br /></div>
<h3 style="text-align: left;">
Explanation : </h3>
<div>
<br />
<ul style="text-align: left;">
<li>variable <b>currentSymbol</b> stores the symbol of the active player in it </li>
</ul>
<br />
<br />
When we run the program "startPlay()" method is called which prints the board.</div>
<div>
In that method there is a for loop which iterates from 0 to less than 9. On line# 37 we check if the iteration is even or odd(even for player1 and odd for player2) if its even the "currentSymbol" is set equal to "player1Symbol" and if its odd "currentSymbol" is set equal to "player2Symbol".</div>
<div>
</div>
<div>
After that the player selects a box to fill and the currentSymbol is printed in the selected box if the selected box is empty. If the selected box is not empty the current iteration is repeated and the player is asked again to select a box. Then the board is printed again then. Then the process repeats until the iteration = 4.</div>
<div>
<h4 style="text-align: left;">
Deciding Winner: </h4>
When Iteration becomes Equal to 4(i.e five boxes are filled)then the decidewinner method is called to check if someone Won? In decide Winner Method we have if conditions checking for all winning combinations(i.e (1,2,3),(1,4,7),(4,5,6),(7,8,9),(2,5,8),(3,6,9),(1,5,9),(3,5,7)) . If someone has won then the loop is broken and the message showing that the current player has won is displayed.<br />
<br />
You can Download the Source Code by Clicking Below Links<br />
<h4 style="text-align: left;">
<a href="https://drive.google.com/file/d/0B1AuZzdiP8vSamdrSmFKS3kyZGM/edit?usp=sharing" rel="nofollow" target="_blank"><span style="color: red;">TicTacToe Code in Java</span></a></h4>
<h4 style="text-align: left;">
<span style="color: red;"><a href="https://drive.google.com/file/d/0B1AuZzdiP8vSREtsVEowWUk0Z2s/edit?usp=sharing" rel="nofollow" target="_blank"><span style="color: red;">TicTacToe Code in C#</span></a></span></h4>
</div>
<h3 style="text-align: left;">
Share this Post With Your Friends!!!</h3>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-28389373670960986392014-05-13T06:31:00.000-07:002014-07-15T10:42:50.656-07:00Body Mass Index Calculator in C# | kodingexamples.blogspot<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: center;">
</h2>
<h2 style="text-align: center;">
Find out your Body Mass Index</h2>
<div>
Here are the Screen Shots of the Application :</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY8S1PHMGtXWWIl_B6obSxW96Y5l_kH3VAIdsEzFa9Iyp123o-bIOtlTaYk_9YOSUo37regaXzwOXnsh4AnB5cVPzVnxLugKKp3f51abUjIk-Ndc_qKBpV1U-A-QIqMWG0j00Z9-ecL-4V/s1600/BMI1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY8S1PHMGtXWWIl_B6obSxW96Y5l_kH3VAIdsEzFa9Iyp123o-bIOtlTaYk_9YOSUo37regaXzwOXnsh4AnB5cVPzVnxLugKKp3f51abUjIk-Ndc_qKBpV1U-A-QIqMWG0j00Z9-ecL-4V/s1600/BMI1.jpg" height="390" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiikMB0ly82bWOsc1y40CwMTOYQFy9aK4cY4asBGqXa-imnSpc-EcQLpEPRD9gS8ErvbZh51E1B5IwW7ivrhToE7ZhZM3cyU2PHe7pQoIk9lHxg8BqV1H7dKK8bNPT63rDIfRkxoiJlwuUf/s1600/BMI2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiikMB0ly82bWOsc1y40CwMTOYQFy9aK4cY4asBGqXa-imnSpc-EcQLpEPRD9gS8ErvbZh51E1B5IwW7ivrhToE7ZhZM3cyU2PHe7pQoIk9lHxg8BqV1H7dKK8bNPT63rDIfRkxoiJlwuUf/s1600/BMI2.jpg" height="367" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXkDJYSqMJ1h0lfGpFEZXxnBc8F4XD_dM-2RJlg_2SZT3vJu-cDYiswmbQPmDOCgdofbiB6qU_H_Pc-7xULtkI9wpKD7Kl5-MjOjn5JrkiJ4ICPvg10a2U0CQM_OhvYicAniixFiDVZYsy/s1600/BMI3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXkDJYSqMJ1h0lfGpFEZXxnBc8F4XD_dM-2RJlg_2SZT3vJu-cDYiswmbQPmDOCgdofbiB6qU_H_Pc-7xULtkI9wpKD7Kl5-MjOjn5JrkiJ4ICPvg10a2U0CQM_OhvYicAniixFiDVZYsy/s1600/BMI3.jpg" height="385" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div>
This app is developed in C# programming language and the software used to develop is C# 2010 Express</div>
<div>
<a href="https://drive.google.com/file/d/0B1AuZzdiP8vSbkVQMUJZT3pyZlE/edit?usp=sharing" rel="nofollow" target="_blank">CLICK HERE</a> to Download.</div>
<div>
<br /></div>
<div>
<h2 style="text-align: left;">
Comment your Email Address below to get the code for this program. Like our facebook page to get the latest stuff!!!</h2>
</div>
<div>
<br /></div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fkodingexamples%3Fref%3Dhl&width&layout=standard&action=like&show_faces=true&share=true&height=80" style="border: none; height: 80px; overflow: hidden;"></iframe></div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-60474987953981619232014-05-11T16:04:00.003-07:002014-05-11T16:04:58.705-07:00Static/Class Variables VS Instance Variables | Java - kodingexamples.blogspot <div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
What You will Learn :</h2>
<div>
<ul style="text-align: left;">
<li>What are Static/Class variables</li>
<li>What are Instance Variables</li>
<li>Scope of Static and Instance variables</li>
<li>Difference b/w Static and Class variables</li>
</ul>
<h3 style="text-align: left;">
</h3>
<div>
<br /></div>
<h3 style="text-align: left;">
Static Variables :</h3>
<div>
<ul style="text-align: left;">
<li>Static variables are associated with all the objects of the class</li>
<li>Declared in the class outside of any method or block</li>
<li>They are assigned default values when created i.e 0 for integers</li>
<li>They have the same state for all objects of the class</li>
<li>No object is needed to access them i.e we access them like this "<b>ClassName.VariableName</b>"</li>
<li>They can be used in the static methods</li>
<li>Just one copy of static variables per class</li>
</ul>
</div>
<div>
<br />
<br />
This image will clear all the confusion :</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1hKAxDDlNGlcPbVf0YtNuf39JprA89cIfR18Y1gDsR43xIq4F7RUI8U5MvT5db2jeP8-TchlmWmoPyRt6sSWDqVJdssmfeUsFHwzpBrwbU0SSRT2t0rw-bUdc8Vvdv8DSJnwCsavKDXFg/s1600/StaticVariables.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1hKAxDDlNGlcPbVf0YtNuf39JprA89cIfR18Y1gDsR43xIq4F7RUI8U5MvT5db2jeP8-TchlmWmoPyRt6sSWDqVJdssmfeUsFHwzpBrwbU0SSRT2t0rw-bUdc8Vvdv8DSJnwCsavKDXFg/s1600/StaticVariables.jpg" height="280" width="400" /></a></div>
<div>
<br /></div>
<div>
<br />
<br /></div>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
Instance Variables :</h3>
<div>
<ul style="text-align: left;">
<li>As the name tells Instance Variables are associated with the Instances or Objects of the class</li>
<li>Declared in the class outside of any method or block</li>
<li>They are assigned default values when created i.e 0 for integers</li>
<li>Every object of the class have its own set of Instance Variables.</li>
<li>They may have different or same values(states) for all the objects</li>
<li>They require the object to be accessed outside of the class as "<b>ObjectName.VariableName</b>"</li>
<li>One copy of Instance Variables per Object</li>
</ul>
</div>
<div>
<br />
<br />
<br /></div>
<div>
Here is an image for further explanation :</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilBnKdwZ0oPeZriyb885EEvfCtV5izpVDOJDByd-4rtXfLscB5GqeNSUjz-jgaxShS7JrwEALj3oQIp9pF9tazdplZw4Xi_WcCtGKPJlnYiNYHG7NmXYOQXoyDBvbLfOIbKmSbsX1flwBq/s1600/InstanceVariables.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilBnKdwZ0oPeZriyb885EEvfCtV5izpVDOJDByd-4rtXfLscB5GqeNSUjz-jgaxShS7JrwEALj3oQIp9pF9tazdplZw4Xi_WcCtGKPJlnYiNYHG7NmXYOQXoyDBvbLfOIbKmSbsX1flwBq/s1600/InstanceVariables.jpg" height="318" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h3 style="clear: both; text-align: left;">
I hope you learned something from this post if you have any questions feel free to comment them below. Like us for more free stuff!!!</h3>
<ul style="text-align: left;">
</ul>
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fkodingexamples%3Fref%3Dhl&width&layout=standard&action=like&show_faces=true&share=true&height=80" style="border: none; height: 80px; overflow: hidden;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-70973119567444277792014-05-11T04:19:00.000-07:002014-05-11T04:19:17.913-07:00Binary Search vs Linear Search<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
You will learn :</h3>
<ul style="text-align: left;">
<li>Binary search</li>
<li>Linear Search</li>
<li>Where to use Linear Search</li>
<li>Where to use Binary Search</li>
<li>How Binary Search Works</li>
</ul>
<br />
<h3 style="text-align: left;">
Linear Search :</h3>
<ul style="text-align: left;">
<li>It used on unsorted List </li>
<li>It is very slow for large Lists</li>
<li>Its easy to write code for Linear Search</li>
<li>Should be only used if the list is a few elements long</li>
</ul>
<h4 style="text-align: left;">
Code :</h4>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">1: public class Search {
2:
3: public static void main(String args[]){
4: int array[] = {1,5,9,11,25,90,122};
5: int key = 122;
6:
7: Search.LinearSearch(array,key);
8: }
9:
10: public static void LinearSearch(int array[],int key){
11: int last = array.length-1;
12:
13: for(int i = 0; i < array.length;i++){
14: if(array[i] == key){
15: System.out.println("The key found at index : "+i);
16: return;
17: }else if(i == last)
18: System.out.println("Not in the Array");
19: else System.out.println("Searching.....");
20: }//loop
21:
22: }//method
23:
24: }
25:
</code></pre>
<br />
<h4 style="text-align: left;">
Explanation :</h4>
In this program an array is searched for a specific value. A for loop is used to go through and check each element if the element is found at any location in the array the loop breaks and message is displayed. In the other case the message is displayed that "Not i the Array".<br />
<h3 style="text-align: left;">
<br /></h3>
<h3 style="text-align: left;">
<br /></h3>
<h3 style="text-align: left;">
Binary Search:</h3>
<ul style="text-align: left;">
<li>Cuts the array half after each iteration</li>
<li>Suitable for searching in large Lists</li>
<li>Used only if the List is large enough</li>
<li>Faster than Linear Search</li>
<li>A bit complex to code</li>
</ul>
<h4 style="text-align: left;">
Code :</h4>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">1: public class Search {
2:
3: public static void main(String args[]){
4: int array[] = {1,5,9,11,25,90,122};
5: int key = 122;
6:
7: Search.BinarySearch(array,key);
8: }
9:
10: public static void BinarySearch(int array[],int key){
11: int last = array.length-1;
12: int first = 0,mid = (first+last)/2;
13:
14: for(int i = 0; first <= last;i++){
15: mid = (first+last)/2;
16: if(array[mid] == key){
17: System.out.println("The key found at index : "+mid);
18: return;
19: }else if(array[mid] > key)
20: last = mid - 1;
21:
22: else if(array[mid] < key)
23: first = mid + 1;
24: else if(i == array.length-1)
25: System.out.println("Not in the Array!!!");
26: System.out.println("Searching......");
27: }//loop
28:
29: }//method
30:
31: }
</code></pre>
<br />
<h4 style="text-align: left;">
Explanation :</h4>
Look at line# 16 if middle element of the array is equal to the the search number then Display the message "Found the key" and break the loop. At line#19 if middle element is greater than key then the second half of the array is ignored and the end of the array shifts at mid - 1. Similarly At line# 22 if middle element is less than the key than first half is ignored and first is shifted to 'mid + 1'. Note that your array must be sorted in the ascending order first to apply this algorithm.<br />
Here is an image which may also help :<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWynt6mBpjWzBhunhL3V1kn7Rz7tHeTRqWtNYS4WQXOsb5tyo7usRcX787rHtDKykGlDILBelqjRcwzyuOssfaXdqMvr4tPYFk02V9KWUhzecGxjF6Y4GISUp7HQOrjOZVB1QDA7UmpKVR/s1600/BinarySearch.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWynt6mBpjWzBhunhL3V1kn7Rz7tHeTRqWtNYS4WQXOsb5tyo7usRcX787rHtDKykGlDILBelqjRcwzyuOssfaXdqMvr4tPYFk02V9KWUhzecGxjF6Y4GISUp7HQOrjOZVB1QDA7UmpKVR/s1600/BinarySearch.jpg" height="265" width="400" /></a></div>
<br />
<h3 style="text-align: left;">
I hope this post have cleared out the problems and questions related to Linear and Binary Search if not Comment the question below. Like us for more!!!</h3>
<div id="fb-root">
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fkodingexamples%3Fref%3Dhl&width&layout=standard&action=like&show_faces=true&share=true&height=80" style="border: none; height: 80px; overflow: hidden;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-76020744431075187212014-04-29T16:39:00.000-07:002014-04-29T16:47:10.186-07:00Set Environment Variable for Java | kodingexamples.blogspot<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<b>Today You will learn :</b></h2>
<br />
<ul style="text-align: left;">
<li>How to set environment variable for Java</li>
<li>How to Run Java from Command Prompt</li>
<li>How to Compile Java code from Command Prompt</li>
</ul>
<br />
<br />
Before Starting You must check if <b><a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html" target="_blank">JDK</a> </b>is installed if not download install it from<b> </b>oracle's official website.<br />
<br />
<h3 style="text-align: left;">
<b>Lets get Started :</b></h3>
<br />
<ul style="text-align: left;">
<li>Click <b>Advanced System Settings</b> under <b>Computer Properties<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQlTqEdctVEE-U3u711n8JMypajkUAOH1c2u2eSGizeR6msyKwdQ3CSt4YGbkSyHz16p7H-vfTJWIQVNZD7EGVAief2sTKXMABhAlQznARyAlJhmrLZxoRIQGGU-LSRdjAZ4a-9HlBMGoG/s1600/Runjava1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQlTqEdctVEE-U3u711n8JMypajkUAOH1c2u2eSGizeR6msyKwdQ3CSt4YGbkSyHz16p7H-vfTJWIQVNZD7EGVAief2sTKXMABhAlQznARyAlJhmrLZxoRIQGGU-LSRdjAZ4a-9HlBMGoG/s1600/Runjava1.jpg" height="340" width="400" /></a></div>
</b></li>
<li>Click <b>Environment Variables<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQnFbF6Z5RpgdengND6lJSouHaaNzTbQzn_Ukuaw8SWO0K_r5ilN7bcHg2ulvRlviRCzqKpEYPoofCMXx-764tQnqvdNjzRtKuB94hvRgPQkuMt4KvMjYKFhgrrDxpIyOSBQkTYaeH-jI-/s1600/Runjava2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQnFbF6Z5RpgdengND6lJSouHaaNzTbQzn_Ukuaw8SWO0K_r5ilN7bcHg2ulvRlviRCzqKpEYPoofCMXx-764tQnqvdNjzRtKuB94hvRgPQkuMt4KvMjYKFhgrrDxpIyOSBQkTYaeH-jI-/s1600/Runjava2.jpg" height="400" width="352" /></a></div>
</b></li>
<li><div class="separator" style="clear: both; text-align: left;">
Create a <b>New</b> one with the name <b>path</b></div>
<div class="separator" style="clear: both; text-align: center;">
<b><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQk-jy64FQX4OFVCusyWtK-L65_xTkaoZTRCF4tIsJ7Yf9XsS8sMTBrMUOhhieTimfhnMFOyAi6105Edwj1g_OLgWBL5dy96CuPADLgrbETIChwyIw5B4ILtUI2__nG7k1osWNGOz6t8QZ/s1600/Runjava3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQk-jy64FQX4OFVCusyWtK-L65_xTkaoZTRCF4tIsJ7Yf9XsS8sMTBrMUOhhieTimfhnMFOyAi6105Edwj1g_OLgWBL5dy96CuPADLgrbETIChwyIw5B4ILtUI2__nG7k1osWNGOz6t8QZ/s1600/Runjava3.jpg" height="173" width="400" /></a></b></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Go to the <b>bin</b> in the <b>JDK</b> installation directory and <b>copy the path</b></div>
<div class="separator" style="clear: both; text-align: center;">
<b><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_TsCEr0O3aSbGJmKrOM0CoTW7_XknJWOZAQxQ-VSt6TFcdFYbxPbcAfRiIa4V7ELQx8ylJNIdQImtPj7SDTi9d_WIXRo3cNk8Q-69g2wmoPGsdUnt5aYHxvomoVXyKsq5SuBlwdJHYtj8/s1600/Runjava4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_TsCEr0O3aSbGJmKrOM0CoTW7_XknJWOZAQxQ-VSt6TFcdFYbxPbcAfRiIa4V7ELQx8ylJNIdQImtPj7SDTi9d_WIXRo3cNk8Q-69g2wmoPGsdUnt5aYHxvomoVXyKsq5SuBlwdJHYtj8/s1600/Runjava4.jpg" height="138" width="400" /></a></b></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Now <b>paste this path as the Variable Value</b> and <b>click ok</b></div>
</li>
</ul>
<b><br /></b>
<br />
<h3 style="text-align: left;">
Check :</h3>
<br />
<ul style="text-align: left;">
<li><b>Open Command Prompt</b></li>
<li>Type <b>javac</b> and <b>press enter</b></li>
<li>Alot<b> </b>of<b> junk will Appear below<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmYl5g_FBJKZX4PIrILk4qbdpdM1Jiu4-pEbvsxNBQ8recBLy1N8axqO7xndF31LBhiBhthIV6KTRyUowaUlsTo7JJujXuzEmBEpiI9OZnWZFI6r5pxGPXBdKfM_mdF1RbAqf4utHKuQTO/s1600/Runjava5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmYl5g_FBJKZX4PIrILk4qbdpdM1Jiu4-pEbvsxNBQ8recBLy1N8axqO7xndF31LBhiBhthIV6KTRyUowaUlsTo7JJujXuzEmBEpiI9OZnWZFI6r5pxGPXBdKfM_mdF1RbAqf4utHKuQTO/s1600/Runjava5.jpg" height="198" width="400" /></a></div>
</b></li>
</ul>
<br />
<h3 style="text-align: left;">
<b> Like our Facebook Page for more!!!</b></h3>
<div id="fb-root">
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fkodingexamples%3Fref%3Dhl&width&layout=standard&action=like&show_faces=true&share=true&height=80" style="border: none; height: 80px; overflow: hidden;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-86967015904830267492014-04-14T14:28:00.002-07:002014-04-15T04:06:24.087-07:00How to create Header Files in C/C++?<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Things you will learn :</h3>
<ul style="text-align: left;">
<li>What are <b>Headers</b></li>
<li>How to <b>create Headers</b></li>
<li>Why should i create Headers</li>
<li>How to<b> include header</b> in your program</li>
</ul>
<h3 style="text-align: left;">
What are Header files?</h3>
Header files are similar to the library files in Java. They are created once and can be used in any program in which they are included. Actually you define your functions in the header files to use them again and again without typing the whole code again. All you have to do is to include them in your program first. The most common header files in C/C++ is<b>"conio.h"</b> inside this header some functions are defined which are used to perform actions on console screen.<br />
<br />
<h3 style="text-align: left;">
Create Header Files</h3>
Creating a Header file is not difficult at all here is the step by step method:<br />
<ul style="text-align: left;">
<li>Open <b>Notepad</b></li>
<li>Create your function definations there</li>
<li>Open the folder where you compiler saves its<b> Header Files</b></li>
<li>For turbo C++ its in <b>C drive --> TurboC++ --> Disk --> TurboC3 --> INCLUDE</b> </li>
<li>Save the file with <b>".h"</b> extension in<b> INCLUDE</b> folder</li>
</ul>
Congrats You have created a header file. Now you just have to include your header file in your program and check if its working in the way you want it to. You can include the file like this<b> #include<headerfilename.h.></b>. There should be no errors if you followed the step by step method above.<br />
<br />
<h4 style="text-align: left;">
Here is an Example :</h4>
<div>
<br /></div>
<div id="noIframeDivfad4df92-410f-475d-83ab-b73ebe6eb533" style="display: block; margin: 0px;">
<span style="color: black;">Save this code as "sum.h" in INCLUDE directory.</span>
<br />
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> int sum(int num1,int num2){
return (num1+num2);
}
</code></pre>
</div>
<script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"></script><script language="javascript" type="text/javascript">bt_code_init('0ebe5545-9082-4024-98f5-b585f9f47710');</script>
Now include the header file in the program like this:<br />
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> #include<stdio.h>
#include<conio.h>
#include<sum.h>
void main(){
clrscr();
printf("%d\n",sum(5,5));
getch();
}
</code></pre>
<div id="iframeDiv482c0bd5-9601-4266-9bdf-e6d2df65f151" style="display: none; margin: 0px;">
<iframe frameborder="0" id="iframe482c0bd5-9601-4266-9bdf-e6d2df65f151" src="http://www.blogtrog.com/code.aspx?id=482c0bd5-9601-4266-9bdf-e6d2df65f151" style="border: 1px solid #e0e0e0; height: 200px; margin: 0px; width: 400px;"></iframe></div>
<div id="noIframeDiv482c0bd5-9601-4266-9bdf-e6d2df65f151" style="display: block; margin: 0px;">
<pre><div>
</div>
</pre>
</div>
<div id="noIframeDiv5ddfa539-e790-4cf0-9cf9-87fd69d787b7" style="display: block; margin: 0px;">
<div style="text-align: left;">
<span style="color: black;">
</span>
</div>
</div>
<h3 style="text-align: left;">
<span style="color: black;">Explanation : </span></h3>
<div style="text-align: left;">
<span style="color: black;">In the header file i have defined a function which takes two numbers as parameters and returns their sum. And in my program i just included header </span>file(<b>#include<sum.h></b>) and called the function which i defined earlier in my header file(<b>printf("%d\n",sum(5,5))</b>). The output is 10.</div>
<div style="text-align: left;">
<br /></div>
<h4 style="text-align: left;">
<span style="font-size: large;">Today i discussed probably the easiest way of creating the header files. If you know even easier method than that. Share it with others in the comments below. And if you like this post <b>Share it</b> with your friends and <b>Subscribe for more</b> :)</span></h4>
<div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
<span style="font-size: large;"><br /></span></div>
</div>
</div>
</div>
<script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"></script><script language="javascript" type="text/javascript">bt_code_init('5ddfa539-e790-4cf0-9cf9-87fd69d787b7');</script>
</div>
<script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"></script><script language="javascript" type="text/javascript">bt_code_init('fad4df92-410f-475d-83ab-b73ebe6eb533');</script>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-21156313571360470582014-04-08T14:20:00.002-07:002014-04-15T04:18:32.921-07:00How to make a JTextField accept only numbers in Java?<div dir="ltr" style="text-align: left;" trbidi="on">
<h4 style="text-align: left;">
<span style="font-size: large;"><b>Today You will learn :</b></span></h4>
<div>
<ul style="text-align: left;">
<li>How to use<b> <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html" target="_blank">Inner Class</a></b> to use Key Events and Mouse Events</li>
<li>How to <b>cancel key typed event</b> in Java</li>
<li>How make numeric <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html" target="_blank"><b>JTextField</b></a></li>
<li>How to<b> limit the number of characters</b> in <b>JTextField</b></li>
<li><b>consume() </b>method of the KeyListener interface</li>
<li><b>Character.isDigit()</b> method and <b>getKeyChar()</b> method</li>
</ul>
<div>
<b><br /></b></div>
<div>
The main things here are the <b>JTextField</b> , <b>KeyEvents</b> , <b>Inner Classes in Java</b> and how to create <b>numeric TextFields in Java</b> and if you want <b>numeric text box in C#</b> <a href="http://kodingexamples.blogspot.com/2014/04/how-to-make-numeric-textbox-in-c.html" target="_blank">Click Here</a> </div>
<h3 style="text-align: left;">
How to use Inner Classes to Implement <b>KeyEvents</b>?</h3>
<div>
When i first used inner classes i really avoided them because they a bit complex. But trust me Inner classes are'nt that difficult as they are thought to be. Here's how its done :</div>
<div>
<ul style="text-align: left;">
<li>Create a private inner class</li>
<li>Use the implement keyword to implement the <b>KeyListener</b> interface</li>
<li>Override all the methods of the interface <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html" target="_blank"><b>KeyListener</b></a></li>
<li>Create the Object of the Inner Class in the enclosing Class like that<b> "InnerClass i = outerclassobject.new InnerClass()"</b>(you have to create outer class object first)</li>
<li>Add the object of inner class to the object of component class which you want to implement KeyEvents for example <b>"JTextField.add(KeyListenerObject)"</b></li>
</ul>
<h4 style="text-align: left;">
</h4>
<h4 style="text-align: left;">
Here is the code:</h4>
</div>
</div>
<div>
<br /></div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class NumericBox{
static JTextField tf = new JTextField();
public static void main(String arh[]){
NumericBox g = new NumericBox();
Handle h = g.new Handle();
JFrame f = new JFrame();
JPanel p = new JPanel();
tf.setColumns(5);
tf.addKeyListener(h);
p.add(tf);
f.add(p);
f.setSize(200, 200);
f.setVisible(true);
}//end of main method
private class Handle implements KeyListener{
public void keyTyped(KeyEvent e) {
if(tf.getText().length() == 6)
e.consume();
if(e.getKeyChar() != 13 && !Character.isDigit(e.getKeyChar())
&& e.getKeyChar() != 42 && e.getKeyChar() != 43 //key codes of the keys
&& e.getKeyChar() != 45 && e.getKeyChar() != 46 //you want to enable
&& e.getKeyChar() != 47)
e.consume();
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}//end of inner class
}//end of outer class
</code></pre>
<h4 style="text-align: left;">
Output:<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1eSUhfZe2qiQ_akBBHb2GJtyUuzG3nemZ2R6P4OVUcGQ41c0LjGsLQ87poArEvOPMozgKFDGO17CA8VCHBbKCdzc8Wbp0lGsqL1Ree_9E1vm-_6krVcZV6c3XuB_wLmdJY8kDmdO0Mu0h/s1600/Javanumerictextfield.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1eSUhfZe2qiQ_akBBHb2GJtyUuzG3nemZ2R6P4OVUcGQ41c0LjGsLQ87poArEvOPMozgKFDGO17CA8VCHBbKCdzc8Wbp0lGsqL1Ree_9E1vm-_6krVcZV6c3XuB_wLmdJY8kDmdO0Mu0h/s1600/Javanumerictextfield.jpg" height="223" width="400" /></a></div>
</h4>
<h4 style="text-align: left;">
<b>Explanation : </b></h4>
In the keyTyped method of the inner class at I have checked if the the characters typed in the text field is equal to 6 then the key event is canceled by using the <b>consume()</b> method in the this way i have restricted from entering more than 6 character in the text field. To restrict the user to enter digits only i have used <b>Character.isDigit(e.getkeyChar())</b>. <b>e.getKeyChar()</b> returns the ASCII code of the key pressed here if the key pressed do not represent a digit the key event is canceled similarly i have enabled several other keys using ASCII code if you don't know the ASCII code of keyboard keys <a href="http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html" target="_blank">CLICK HERE</a>.<br />
<br />
<h3 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">I hope You have learned Something from this post if have any suggesions for me contact me at daniequreshigmail.com or comment below And Follow Me for more stuff!!!</span></h3>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-56395436211173110392014-04-03T13:39:00.002-07:002014-04-15T04:28:33.575-07:00How to reverse a String in Java?<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
What You are About to Learn?</h2>
<br />
<ul style="text-align: left;">
<li>Array Reversing</li>
<li>Java String.charAt(index) method</li>
<li>String Reversing</li>
<li>StringBuilder</li>
<li>StringBuffer.reverse() method in Java</li>
<li>Java String.toCharArray() method</li>
</ul>
<br />
<div>
<br /></div>
<div>
<b><span style="font-size: large;">String Reversing Method # 1 : </span></b></div>
<div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> public class StringReversing {
public static void main(String args[]){
char temp;
String s = new String("Hello");
System.out.println(s);
char str[] = s.toCharArray();
int last = s.length() - 1;
for(int first = 0; first < s.length()/2; first++){
temp = s.charAt(first);
str[first] = str[last];
str[last] = temp;
last--;
}
s = new String(str);
System.out.println(s);
}//end of main
}//end of class
</code></pre>
</div>
<div>
<b> OUTPUT :</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhy8YD_9VEffPpoSAMguFY9B-aiAMxt9yB95Ph6eWAZH1uFfWCdMWsfD_jaQABOVK6eSMZBgM4OgfnN_Eeikro1lnCjH0NUsHPAVy-MysMX6DQMwX262nO3hRiY4LPmG4FfsMeEcmI5ZW3w/s1600/StringReversing_java1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhy8YD_9VEffPpoSAMguFY9B-aiAMxt9yB95Ph6eWAZH1uFfWCdMWsfD_jaQABOVK6eSMZBgM4OgfnN_Eeikro1lnCjH0NUsHPAVy-MysMX6DQMwX262nO3hRiY4LPmG4FfsMeEcmI5ZW3w/s1600/StringReversing_java1.jpg" height="204" width="400" /></a></div>
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: left;">
<b>Explanation :</b> In line number 06 i have used the <b>String.toCharArray()</b> method to store the converted(to array of characters) value of the string in Array of characters "str" .</div>
<div class="separator" style="clear: both; text-align: left;">
<b>charAt(index)</b> is the build in method in Java String it simply returns the character at the index "index"(index is an integer) it can not be used to assign the value For example i cant do this <b> s.charAt(0) = 'c'</b> . At line #10 the value of first character of the "str" is copied in the variable temp. In 1st iteration of the loop the first char in "str" is replaced by the last and last is replaced by the first one. </div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirx9VaFv0zp8aoo9qPLoVy-LYqQDP8YEnaNnbMT6p6l-uDgG-HTNih6bbw4gY4N7PEjbRmG-9hcpOoF78IKv-5DJDxVOj94w63mASxz6D9qdKeOuyuA8U0I68Mpages7HJVfkVZtwqoy4V/s1600/Hello.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirx9VaFv0zp8aoo9qPLoVy-LYqQDP8YEnaNnbMT6p6l-uDgG-HTNih6bbw4gY4N7PEjbRmG-9hcpOoF78IKv-5DJDxVOj94w63mASxz6D9qdKeOuyuA8U0I68Mpages7HJVfkVZtwqoy4V/s1600/Hello.jpg" height="215" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Similarly in second iteration of the loop 2nd character is replaced by second last and vice versa. This process will carry on until the whole Array is reversed. And at the end the result is printed in line #18.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<b><span style="font-size: large;">String Reversing</span></b><span style="font-size: large;"><b> Method #2 :</b></span></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> public class StringReversing {
2
3 public static void main(String args[]){
4
5 String s = new String("Hello");
6 StringBuffer sb = new StringBuffer(s);
7 System.out.println(s);
8
9 sb.reverse();
10
11 s = new String(sb.toString());
12 System.out.println(s);
13 }//end of main
14
15 }//end of class
</code></pre>
<br />
<div class="separator" style="clear: both; text-align: left;">
<span style="text-align: center;"> </span></div>
<div class="separator" style="clear: both; text-align: left;">
Output will be the same.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<b>Explanation :</b> Here i used StringBuffer. I saved the string in the StringBuffer. Then i used the reverse(reverse method is build in for StringBuffer) method for StringBuffer to reverse the it. At the end i copied the saved the converted(to string) value of StringBuffer in the string value and displayed it.</div>
<h4 style="clear: both; text-align: left;">
Share other methods which you find useful too!!</h4>
<div class="separator" style="clear: both; text-align: left;">
</div>
<h3 style="clear: both; text-align: left;">
<span style="font-family: Verdana, sans-serif;">I Hope You Learned Something from this Post if you have any problem comment below i will reply as soon as possible, FOLLOW Me for more Good Stuff :) </span></h3>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-43993304030826411932014-04-01T14:18:00.000-07:002014-04-20T15:54:13.220-07:00Numeric TextBox, TextBox useful Properties | C# TextBox<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;"><b>Why to make a TextBox numeric ?</b></span><br />
<div>
</div>
<div>
Imagine the situation in which you are making a calculator app and if the user types chars except digits that may result in undesired output. A wise programmer will not let that happen and he will restrict the user to type only numbers so that the chance of user mistyping is eliminated. </div>
<div>
</div>
<div>
Today i am going to tell you how to make numeric textbox and other useful stuff you can do with textbox<br />
Here is the code :<br />
<br />
<br />
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(e.KeyChar >= 48 && e.KeyChar <= 57 && e.KeyChar != 8))
e.Handled = true;
}
</code></pre>
<br />
"textBox1_KeyPress" method is called every time a key is pressed inside the "textBox1". Inside the method there is an if which checks weather or not ASCII key Code of the key pressed by the user is greater or equal to 48 and less than or equal to 57 if both the conditions becomes true then the corresponding character appears in the "textBox1" if not then the "keyEvent" is cancelled. KeyEvent is cancelled by setting "e.Handled = true;".<br />
<br />
You can restrict your textbox to accept only the keys you want it to accept for this you need to know ASCII values for keyboard keys.<br />
If you don't know the ASCII values for keyboard keys you can check them <a href="http://adf.ly/iJQZ7" target="_blank">HERE</a><br />
<br />
There is another method for doing this which is a bit easier for beginners :<br />
<br />
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8)
e.Handled = true;
}
</code></pre>
<br />
In this case whenever the user presses the key C# checks weather or not the key pressed by the user represents a digit if the key represents a digit the key event goes fine but if not then the key event is cancelled. The other thing you have to do is to enable the backspace button by using its <a href="http://adf.ly/iJQZ7" target="_blank">ASCII</a> value.<br />
<br />
<br />
<br />
<br />
<b><span style="font-size: large;">Other useful textbox Properties</span></b><br />
<b><br /></b>
You can make your textbox secure by using the <b>PasswordChar</b> property you can set the <b>PasswordChar</b> to any char and when you type in the textbox the digits are hidden and password char is diplayed instead. This is useful in many situations for example if you are creating the facebook login app you don't want anyone to see what you typed in password textbox.<br />
<br />
You can set the maximum length of the characters the textbox accepts by setting <b>textbox.MaxLength</b>.<br />
<br />
If you want some different text color in the textbox you can use <b>textbox.ForeColor</b> property and you can do the same for Background as well. See which colors C# supports <a href="http://adf.ly/iJQMc" target="_blank">HERE</a>.<br />
<br />
<h3 style="text-align: left;">
Hope you Liked this FOLLOW Me for more!!!</h3>
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-42104738042933703062014-03-29T15:04:00.000-07:002014-04-19T05:28:51.818-07:00How to Create Executable JAR files?<div dir="ltr" style="text-align: left;" trbidi="on">
You can launch your Java project by just double clicking the jar file of the project. For that you have create the jar file first. Don't worry you don't have to download additional tools for that purpose it can be done using <a href="http://adf.ly/hzZ63" target="_blank">Eclipse</a>.<br />
<br />
If you don't have Eclipse you can download it for your platform <a href="http://adf.ly/hzZvk" target="_blank">HERE</a><br />
If you don't have JDK you can download it free for you OS <a href="http://adf.ly/hzabE" target="_blank">HERE</a><br />
<br />
Here is the step by step method to create Executable Jars using Eclipse :<br />
<br />
<br />
<ul style="text-align: left;">
<li><span style="background-color: white; color: #656e7f; font-size: 18px; line-height: 27px;"><span style="font-family: Arial, Helvetica, sans-serif;"><b>Right Click</b> the project from which you want to create jar file</span><span style="font-family: Source Sans Pro, Helvetica, Arial, sans-serif;"> </span><div class="separator" style="clear: both; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; text-align: center;">
<br /></div>
</span></li>
<li><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;">Click <strong style="border: 0px; box-sizing: border-box; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Export</strong> <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1pU-Ikm7BxmlYbc74TPFVlaRJola7gCkKUbBxZKnlnoXgidewhOjXIxR8BxKYqEr551ehaTN0okeHAO2HIQB5yeJGLAy8MSRUzpQZCSBd6Vyi25POcgFOq4Qdq4md-dXvVPKfuy_2syMZ/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1pU-Ikm7BxmlYbc74TPFVlaRJola7gCkKUbBxZKnlnoXgidewhOjXIxR8BxKYqEr551ehaTN0okeHAO2HIQB5yeJGLAy8MSRUzpQZCSBd6Vyi25POcgFOq4Qdq4md-dXvVPKfuy_2syMZ/s1600/1.jpg" height="223" width="400" /></a></div>
</span></li>
<li><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;">Choose </span><strong style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Export</strong><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;"> destination as</span><strong style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> Java <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVNCpEeueCVUL_7ESakkVXyWucvbDcFTLTF4OYNlZPHNQUSZVmCVGx7D9gHp5ISB6_UFUk85ETvePP-D39o4ycjMzeSimDqhm10Se0E-i0Hwn41B_4ufc5RwPdbhzgkiwk3GbFtNRfipZD/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVNCpEeueCVUL_7ESakkVXyWucvbDcFTLTF4OYNlZPHNQUSZVmCVGx7D9gHp5ISB6_UFUk85ETvePP-D39o4ycjMzeSimDqhm10Se0E-i0Hwn41B_4ufc5RwPdbhzgkiwk3GbFtNRfipZD/s1600/2.jpg" height="223" width="400" /></a></div>
</strong></li>
<li><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;">Select</span><strong style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> Runnable JAR file</strong><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;"> under</span><strong style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> Java <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgi2utqdT5t1uNG7e04pL49ZP2Zgq22mse48pEV3rjrU_hJfNsTYyNEoon4IwyUcCCjtgr99DkJMAz6YQEcpvQaSiuXX2xx6G4lMDsZ9wjqFMLFeGtb60w3b9WVRJY9e8EM3zKggLvhRhtX/s1600/3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgi2utqdT5t1uNG7e04pL49ZP2Zgq22mse48pEV3rjrU_hJfNsTYyNEoon4IwyUcCCjtgr99DkJMAz6YQEcpvQaSiuXX2xx6G4lMDsZ9wjqFMLFeGtb60w3b9WVRJY9e8EM3zKggLvhRhtX/s1600/3.jpg" height="223" width="400" /></a></div>
</strong></li>
<li><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;">Select a</span><strong style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> Launch configuration</strong><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;"> i.e select the</span><strong style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> main class <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ7egCbZJeKXz57TSVa0Ru4CrR-Civ9pjqrE3_9LAIOTticlTbBa1-LldO4C38VMYmdhINUvmozU-ys0QJr7gwg_N4Yj3-NXADeHepbm7y0P2e-TOhwRU-zenI_YCmstmOvMQGwKL0eXDG/s1600/4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ7egCbZJeKXz57TSVa0Ru4CrR-Civ9pjqrE3_9LAIOTticlTbBa1-LldO4C38VMYmdhINUvmozU-ys0QJr7gwg_N4Yj3-NXADeHepbm7y0P2e-TOhwRU-zenI_YCmstmOvMQGwKL0eXDG/s1600/4.jpg" height="223" width="400" /></a></div>
</strong></li>
<li><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;">Select the <b>destination</b> where u want to create JAR file</span></li>
<li><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;">Select </span><strong style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Extract required libraries into Generated JAR file</strong><span style="background-color: white; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px;"> under</span><strong style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> Library Handling </strong></li>
</ul>
<span style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></span><br />
<div class="separator" style="clear: both; font-weight: bold; text-align: center;">
<span style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrjltxFdSM6kVToxjhXyZ9To_561H02XGErJGAOqTtavXjlZwPBdRjGDXzZSUZhyphenhyphennWyWzYWW9n4hNKXS4nFysx_P3MO0rEqP-XJZqjz_YGBpuUvTv8QeyNtrkqowD9nWSRBM_2nhEhw9PY/s1600/5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrjltxFdSM6kVToxjhXyZ9To_561H02XGErJGAOqTtavXjlZwPBdRjGDXzZSUZhyphenhyphennWyWzYWW9n4hNKXS4nFysx_P3MO0rEqP-XJZqjz_YGBpuUvTv8QeyNtrkqowD9nWSRBM_2nhEhw9PY/s1600/5.jpg" height="222" width="400" /></a></span></div>
<span style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span>
<br />
<div class="separator" style="clear: both; font-size: 18px; font-weight: bold; line-height: 27px; text-align: center;">
<span style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><br /></span></div>
<span style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span>
<div class="separator" style="clear: both; font-size: 18px; font-weight: bold; line-height: 27px; text-align: center;">
</div>
<span style="border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul>
<li><b style="font-size: large; text-align: left;">Click Finish</b></li>
</ul>
<br />
<h3 style="clear: both; font-size: 18px; line-height: 27px; text-align: left;">
<b><br /></b></h3>
<h3 style="clear: both; font-size: 18px; line-height: 27px; text-align: left;">
<b>Hope You liked this post FOLLOW ME For more HAPPY CODING :)</b></h3>
<div style="font-size: 18px; line-height: 27px;">
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
<b><br /></b></div>
</span></div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-78478137105065889212014-03-24T02:25:00.000-07:002014-03-26T01:12:55.437-07:006 Reasons to Learn Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Java is extremely powerful and Easy Language. But don’t misunderstand its easy but powerful and popular language. It is a must learn programming language for you weather you want to develop web Applications or desktop Applications. It is also good for you if you are willing to develop for mobile phone. Java is Ideal choice for everyone.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcpknxy5uADnnXXFUcA0hrpITVyZ4EBbiaJTcsK4H8qVaen1Wru71r8vVgCFTcUhVDbCMe7XSoA7aSthiwuRzi7UyAU0LxssZGPw_0YTuuV43UnG87arwXDYPKTmuWGpt-ycGLEDktIFLN/s1600/java2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcpknxy5uADnnXXFUcA0hrpITVyZ4EBbiaJTcsK4H8qVaen1Wru71r8vVgCFTcUhVDbCMe7XSoA7aSthiwuRzi7UyAU0LxssZGPw_0YTuuV43UnG87arwXDYPKTmuWGpt-ycGLEDktIFLN/s1600/java2.jpg" /></a></div>
<br />
Lest's check the reasons behind Java's success and why it is the first choice for every programmer in application development market.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">1 : Its Easy to Learn :</strong></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Java is the easiest programming language to start with. Believe me if you are a beginner you are gonna love its syntax as it is quiet easy and almost same to the natural language. Beginners can learn and understand a lot faster as compared to C++. The syntax of Java is easy to remember so the beginners do not have to waste a lot of their time in learning the complicated syntax unlike C++ so beginners can give more time for learning programming rather than learning the syntax. Java is free and it is object Oriented.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">2 : Platform Independent and Powerful development tools :</strong></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Java introduced the concept of WORA(Write Once Run Anywhere)which is One of the biggest reasons of Java’s success. You can develop on one platform and can run it on many others which saves a lot of time and effort. But JRE(Java Runtime Environment) should be installed on the target platform first. You can get latest version of JRE for your platform at <a href="http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html" target="_blank">Oracle Website</a>.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Java has powerful development tools like <strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Eclipse</strong> ,<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> NetBeans</strong> and<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> IntelliJ</strong>.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">3 : 3rd Party Support:</strong></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Many other companies support Java. Java supports <strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Android</strong> application an OS development (which attracts mobile developers). It can also easily integrate with databases like <strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">MySQL</strong>. Web pages can also be created using Java. <strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">GWT</strong>(Google Web Toolkit) supports java to create web pages. Java is also used to create dynamic webpages and Server side Applications(<b>JSP's and Servlets</b>).</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">4 : Highly in Demand :</strong></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
JAVA is highly in demand by employers and its not going end up soon due to its advanced features and ease of usage. Most of the applications developed now a days are coded in Java. </div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Java developers are more likely to get a good job as compared to C++ developers. As mentioned above java has 3rd party support which makes it much more popular as compared to other programming languages.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
<b>5 : Free notes plus free IDE's :</b></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
Java have a lot of online support and free notes are available in thousands of websites. It also has the better IDE's than others like <a href="https://www.eclipse.org/downloads/" target="_blank">Eclipse EE</a> ,<a href="https://netbeans.org/downloads/" target="_blank">Net Beans</a>. And all of that for Free!!! </div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">6 : Java is an All Rounder :</strong></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; vertical-align: baseline;">
You can do anything using Java you can create web pages and web applications using Java. Java is also a must learn if you want to develop mobile applications. You can create Databases using Java. You can create games with high quality graphics. There is nothing which you can not do with java. Java is fun And i Bet you will never feel tired of Java.</div>
<h3 style="background-color: white; border: 0px; box-sizing: border-box; color: #656e7f; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 18px; line-height: 27px; margin-bottom: 27px; outline: 0px; padding: 0px; text-align: left; vertical-align: baseline;">
If You Liked my Post FOLLOW ME for more stuff :) !!!</h3>
<div id="fb-root">
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-83903912752504131872014-03-22T10:46:00.000-07:002014-04-02T06:56:47.399-07:008 Great Tips For Developing Logic in Programming<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
The biggest problem a beginner faces is the problem of Logic Developing. Many of Beginners do not have too much creative thinking. Today i m going to share with you the techniques which i used to strengthen my logic in programming and they helped me a Ton. Believe me you are going to love them.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Know what you are doing 1 : </span><span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Most of the beginners just want to make their code run</span> rather then thinking of logic behind it. Trust me you are not going to learn anything if you continue doing this.The better way is to break your problems into pseudo code and then try to implement each line of pseudo code into the program.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Read Other programmer’s Code 2 : </span>Obviously I am not asking to copy other programmer’s code always use your own methods. But after developing your own logic for a problem try to compare that with other’s programs. You can learn a lot by doing this.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Challenge Your Skills 3 : </span>Keep testing and Challenging yourself on a regular basis. Try to do something new and challenging whenever you find time. Your quest for knowledge should never end.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Challenge The Rules 4 :</span> Always keep challenging the rules of the language for example java does not allow constructor overriding. Think that why the developers chose not to allow constructor overriding. What problems would arise if it was allowed? and what are the possible benefits if it is allowed?</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Understand The Algorithms 5 :</span> Try to understand the algorithms you find on the internet and write code for them by yourself rather than copying code from the internet some example algorithms are Bubble Sort , Array Reversing , Even or odd functions , drying shapes using asteriks and loops and many more.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Teach others and Discuss 6 :</span> Teaching others will help you a lot to review the things you skipped and its also going to make you much more confident of what you can do. </div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Write Your Own Code 7 :</span> Always try to develop your own logic don’t copy code from the internet, internet should always be your last option.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: 700; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Never Lose Heart 8 :</span> A large number of beginners fail to become a successful programmer because they are impatient and lose heart too early. So Never even think of giving up just keep trying and you will see the improved version of yourself everyday.<br />
<br />
<b><br /></b>
<b><br /></b>
<b>Hope this helped. If there are some other tips which worked for U comment below and and FOLLOW ME for more :) !!!</b><br />
<b><br /></b>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-53963115060319415952014-03-20T11:04:00.000-07:002014-04-13T18:48:44.474-07:00How to Calculate the Running time of C/C++ code?<div dir="ltr" style="text-align: left;" trbidi="on">
Have you ever thought about what if you could know the time taken by your code to run. That's what todays post is all about.<br />
<br />
Knowing the speed of your program or algorithm is very important. Because that's the only way to make it better and faster. Today i am going to show you how you can calculate the running time of your program.<br />
For doing this you first need to include the header file "time.h". Then declare two variables of "time_t" type i.e "time1" and "time2". Save the current time in variable time1(time (&time1);) then at the end of the program again save the value of current time this time in "time2"(time (&time);). Then call the method "difftime(time2,time1);" like this. And this will return you the time taken in seconds.<br />
Similarly if you want to calculate the time for a specific method save the current time before and after the method and call the function <b>"difftime()"</b>.<br />
<br />
<div id="iframeDiv5c2d581d-8627-4c11-b6ad-252e9d0163aa" style="display: none; margin: 0px;">
<iframe frameborder="0" id="iframe5c2d581d-8627-4c11-b6ad-252e9d0163aa" src="http://www.blogtrog.com/code.aspx?id=5c2d581d-8627-4c11-b6ad-252e9d0163aa" style="border: 1px solid #e0e0e0; height: 200px; margin: 0px; width: 600px;"></iframe></div>
<div id="noIframeDiv5c2d581d-8627-4c11-b6ad-252e9d0163aa" style="display: block; margin: 0px;">
<pre><div>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><span style="color: black;">#include </span><span style="color: black;"><</span><span style="color: black;">stdio.h</span><span style="color: black;">></span><span style="color: black;">
#include </span><span style="color: black;"><</span><span style="color: black;">time.h</span><span style="color: black;">></span><span style="color: black;">
#include</span><span style="color: black;"><</span><span style="color: black;">conio.h</span><span style="color: black;">></span><span style="color: black;">
#include</span><span style="color: black;"><</span><span style="color: black;">iostream.h</span><span style="color: black;">></span><span style="color: black;">
</span><span style="color: blue;">void</span><span style="color: black;"> main (</span><span style="color: blue;">void</span><span style="color: black;">)
{
clrscr();
</span><span style="color: blue;">int</span><span style="color: black;"> sum,a,b;
time_t time1,time2;
</span><span style="color: blue;">double</span><span style="color: black;"> dif_sec;
time (</span><span style="color: black;">&</span><span style="color: black;">time1);
cout </span><span style="color: black;"><<</span><span style="color: black;"> </span><span style="color: black;">"</span><span style="color: black;">Enter the value of a :</span><span style="color: black;">"</span><span style="color: black;"><<</span><span style="color: black;">endl;
cin </span><span style="color: black;">>></span><span style="color: black;"> a;
cout </span><span style="color: black;"><<</span><span style="color: black;"> </span><span style="color: black;">"</span><span style="color: black;">Enter the value of b :</span><span style="color: black;">"</span><span style="color: black;"><<</span><span style="color: black;">endl;
cin </span><span style="color: black;">>></span><span style="color: black;"> b;
sum </span><span style="color: black;">=</span><span style="color: black;"> a </span><span style="color: black;">+</span><span style="color: black;"> b;
time (</span><span style="color: black;">&</span><span style="color: black;">time2);
dif_sec </span><span style="color: black;">=</span><span style="color: black;"> difftime (time2,time1);
cout </span><span style="color: black;"><<</span><span style="color: black;"> </span><span style="color: black;">"</span><span style="color: black;">\nThe sum is : </span><span style="color: black;">"</span><span style="color: black;"> </span><span style="color: black;"><<</span><span style="color: black;"> sum</span><span style="color: black;"><<</span><span style="color: black;">endl;
cout </span><span style="color: black;"><<</span><span style="color: black;"> </span><span style="color: black;">"</span><span style="color: black;">\nIt took you </span><span style="color: black;">"</span><span style="color: black;"> </span><span style="color: black;"><<</span><span style="color: black;"> dif_sec </span><span style="color: black;"><<</span><span style="color: black;"> </span><span style="color: black;">"</span><span style="color: black;"> seconds to enter the numbers and calculate the sum</span><span style="color: black;">"</span><span style="color: black;"><<</span><span style="color: black;">endl;
cout </span><span style="color: black;"><<</span><span style="color: black;"> </span><span style="color: black;">"</span><span style="color: black;">Press Any key to Continue.....</span><span style="color: black;">"</span><span style="color: black;">;
getch();
}
</span></div>
</pre>
</div>
<script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"></script><script language="javascript" type="text/javascript">bt_code_init('5c2d581d-8627-4c11-b6ad-252e9d0163aa');</script>
<br />
<h3 style="text-align: left;">
<b>Output :</b></h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ2U-NXbphalZlatbBeV7DewM2hOsiORLqpUk_ik3xYVbEVsKtbqg4FTSCP6nCbmgLDAzBcWdgYnd4ghqfq_3iDPP8_NU-UaNyGBWtw0tP0m6lifcnKmKEw7hxJNb1xqbk1vK46SUnf2o1/s1600/Output0.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ2U-NXbphalZlatbBeV7DewM2hOsiORLqpUk_ik3xYVbEVsKtbqg4FTSCP6nCbmgLDAzBcWdgYnd4ghqfq_3iDPP8_NU-UaNyGBWtw0tP0m6lifcnKmKEw7hxJNb1xqbk1vK46SUnf2o1/s1600/Output0.jpg" height="179" width="320" /></a></div>
<div>
<br /></div>
<h3 style="text-align: left;">
<b>If you like my post Comment below and subscribe for more :) !!!</b></h3>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-83386165205915320652014-03-19T00:36:00.001-07:002014-03-23T09:54:17.554-07:00Why is main method static in Java?<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
To understand this first you need to know why do we create static methods and how do we call them learn about static methods at :</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
Main method is declared static because static methods do not need any instance to get themselves called. Java is Object Oriented language so it is necessary to declare the main method as static because their are some conditions in which we don’t want to create the object for example if we want to do simple programming without using Concepts of OOP then their is no way to call the main method but by declaring it static. The code below will help you understand.</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
public class MyClass{</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
public void main(String arguments[]){</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
System.out.println(“KodingExamples.blogspot.com”);</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
}</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
}</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
The above code generates an error: main method is not static please define the main method as:<br />
public static void main(String[] args)<br />
But when the main is made static this code runs fine and gives the output: KodingExamples.blogspot.com</div>
<div style="border: 0px; color: #2b2b2b; font-family: Lato, sans-serif; font-size: 16px; line-height: 24px; margin-bottom: 24px; outline: 0px; padding: 0px; vertical-align: baseline;">
<b> If you find my answer interesting and easy to understand invite your friends to read this and Don’t Forget to Like and give your feedback and suggessions and FOLLOW me for More</b> <img alt=":)" class="wp-smiley" src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1394649645g" style="border: 0px; height: auto; line-height: inherit; margin-bottom: 0px; margin-top: 0px; max-width: 100%; padding: 0px; vertical-align: middle;" />!!</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com0tag:blogger.com,1999:blog-5412878085198331085.post-10653285662701620902014-03-18T10:59:00.001-07:002014-08-08T04:17:14.321-07:00Calculate Tying Speed Using Java Code <div dir="ltr" style="text-align: left;" trbidi="on">
Today i m going tell you how you can calculate your typing speed using Java code<br />
<b>Here is the Code :</b><br />
<b><br /></b>
<br />
<div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI1Qp3HOPW_PjqmyOnBXztnpCgOZVLzKOK-wpGeQcnpY2va24RIaWGEGvgPhmUPxWs7NJyctaG9cy0zkykFcKCpLj0d3Kz5Acd1onsouqJEQWIIr5-x6xgqknoWY3V_1kd6sAQQUqNeB-e/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> import java.util.Scanner;
public class TypingSpeedCalculator {
int no_of_words=0;
static int z;
double minutes;
double start,end;
public int count_Words(){
int a=1;
String b;
Scanner s1 = new Scanner(System.in);
System.out.println("Type some text : ");
start = System.currentTimeMillis();
b = s1.nextLine();
end = System.currentTimeMillis()-start;
minutes = end/60000;
for(int i = 0;i < b.length();i++){
if(b.charAt(i) == ' '){
a = a + 1;
}
}
no_of_words = a;
return a;
}
public void typing_Speed(){
double a = 0;
a = no_of_words/minutes;
System.out.println("Your typing Speed is "+a+" WPM");
}
public static void main(String args[]){
TypingSpeedCalculator ui=new TypingSpeedCalculator();
System.out.println("\nOutput : \n");
ui.count_Words();
ui.typing_Speed();
}
}
</code></pre>
<script language="javascript" src="http://www.blogtrog.com/scripts/bt_code.js" type="text/javascript"></script><script language="javascript" type="text/javascript">bt_code_init('d8c05b7e-2612-43b1-9626-45c18317491c');</script>
</div>
<div>
<b>Output:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0hpQ_z1u-UK32jSqCgwAJKxNpa-_WSP21WDOFmcuhP-F3trBsUZSzzx5zOGp4S9qc3dbYyGfoiCK4FZT_jQWmjEeGXTiaSvWITBPBclW4y6Z7E6FSyLwC1tAJUDJcTGg040ijvpgtpSJr/s1600/Output.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0hpQ_z1u-UK32jSqCgwAJKxNpa-_WSP21WDOFmcuhP-F3trBsUZSzzx5zOGp4S9qc3dbYyGfoiCK4FZT_jQWmjEeGXTiaSvWITBPBclW4y6Z7E6FSyLwC1tAJUDJcTGg040ijvpgtpSJr/s1600/Output.jpg" height="161" width="320" /></a></div>
<b><br /></b>
<br />
<h3 style="text-align: left;">
<b> Hope you Liked this. FOLLOW me for more awesome stuff!!! </b></h3>
<blink>
<br />
</blink><br />
<h4 style="text-align: left;">
<a href="http://www.fiverr.com/danyalfayyaz/resolve-any-csharp-or-java-programming-error" rel="nofollow" target="_blank"><b><span style="color: #cc0000;">Get Your Programming Related Problems SOLVED</span></b></a></h4>
<b><br /></b>
<b><br /></b>
<b><br /></b></div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="//www.facebook.com/plugins/follow?href=https%3A%2F%2Fwww.facebook.com%2Fdanyal.qureshi.370&layout=standard&show_faces=true&colorscheme=light&width=450&height=80" style="border: none; height: 80px; overflow: hidden; width: 450px;"></iframe>
</div>
Anonymoushttp://www.blogger.com/profile/14712780933871394353noreply@blogger.com2