Monday, 30 December 2013

Most frequently asked interview question on String in java

Most frequently asked interview question on String in java

 Here we describe difference between (==) and .equals() :-

Both of them very much differ in their significance and working as equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.
Let us see an example we have two String objects and they are being pointed by two different reference variables as a1 and a2.

 a1 = new String("xyz");
 a2 = new String("xyz");
Now, if you use the "equals()" method to check for their equivalence as

 if(a1.equals(a2))
      System.out.println("a1.equals(a2) is TRUE");
 else
      System.out.println("a1.equals(a2) is FALSE");
You will get the output as TRUE as the 'equals()' method check for the content equivality.
Lets check the '==' operator..

if(s1==s2)
     System.out.printlln("a1==a2 is TRUE");
   else
     System.out.println("a1==a2 is FALSE");
Now you will get the FALSE as output because both a1 and a2 are pointing to two different objects even though both of them share the same string content. It is because of 'new String()' everytime a new object is created.
Try running the program without 'new String' and You will get TRUE for both the tests. 

   String a1 = "xyz";
   String s2 = "xyz";

                                                         

Posted By:M@ndeep R@w@t
On:Shareyourconscience


Wednesday, 16 October 2013

Hide Div tag By-Tanuj Kumar

 Hide Div tag


Javascript Code to Show or Hide Div:

This function can be done easily by java script program .

<html>
<head>

    <title>Javascript Program to Show/Hide Div Visibility</title>
  
    <script language="javascript" type="text/javascript">
   
        function showHideDiv() {
            var divstyle = new String();
            divstyle = document.getElementById("div1").style.visibility;
            if (divstyle.toLowerCase() == "visible" || divstyle == "") {
                document.getElementById("div1").style.visibility = "hidden";
            }
            else {
                document.getElementById("div1").style.visibility = "visible";
            }
        }
  
    </script>
</head>
<body>
    <div id="div1" class="divStyle">
        Show/Hide Div tag using java script.
    </div>
    <center>
        <input type="button" 
                value="show hide div" 
                onclick="showHideDiv()" />
    </center>
</body>
</html>

Posted By:Tanuj Kumar 
On:Shareyourconscience

Tuesday, 8 October 2013

Transaction in .net By:Tanuj Kumar

share your conscience

Transaction Example in .Net Program:

//first of all take button click event
button click()
   {
//create SqlConnection class object      
SqlConnection con = new SqlConnection("Connnection string");
      SqlTransaction transaction;
//open connection
       con.Open();
//begin transaction
      transaction = con.BeginTransaction();
      try 
      {
         new SqlCommand("INSERT INTO Transaction " +
            "(Text) VALUES ('Row1');", con, transaction)
            .ExecuteNonQuery();
         new SqlCommand("INSERT INTO Transaction " +
            "(Text) VALUES ('Row2');", con, transaction)
            .ExecuteNonQuery();
         new SqlCommand("INSERT INTO transaction2 VALUES " +
            "('Die', 'Die', 'Die');", con, transaction)
            .ExecuteNonQuery();
         transaction.Commit();
      } 
      catch (SqlException sqlError) 
      {
         transaction.Rollback();
      }
      con.Close();
   }



Posted By:Tanuj Kumar 
On:Shareyourconscience

Tuesday, 1 October 2013

Find Prime No. Using Java Script By:Tanuj Kumar

Find Prime No. Using Java Scrip

Find Prime No. Using Java Script

var num =prompt("Enter a number");
parseInt(num);var i;var b=0; if(num==1)
{
alert("A prime number is a number which has only two factors, 1, and itself. But in the case of 1, there is only one factor: 1. So, 1 is not a prime number");
}
else{
for(i=1;i<=num;i++)
{
var result = num%i;
if(result==0)
{
b++;
} }
if(b<=2)
{
alert("Prime no.");
}
else
{
alert("Not Prime");
}
}

Posted By:Tanuj Kumar 
On:Shareyourconscience

Unhide all sheet in MS EXCEL 2010 Using Macro By:Tanuj Kumar

Unhide all sheet in MS EXCEL 2010 Using Macro

Unhide all sheets in MS Excel 2010 using Macros.

Goto view>>Macros>>view macros>>create
then copy paste the below code then save the sheet.

Dim all_ws As Worksheet
For Each all_ws In ActiveWorkbook.Worksheets
all_ws.Visible = xlSheetVisible
Next all_ws

confirm to save.
Now run the create Macros.




Posted By:Tanuj Kumar 
On:Shareyourconscience

Posted on October 01, 2013 | Categories: ,

Thursday, 29 August 2013

Generic Collection:Stack By:Tanuj Kumar

Share your conscience

Stack<t>: simple .net program to show working of stack collection.

The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.
An introduction to DOT NET:
Generic collection: Stack
Generic means it support specific data type (int,float string..etc) 
Stack is LIFO Last in first out manner.
Here stack is used to save data and retrieved it in LIFO manner.
You can run program and see the working procedure of it.

Stack

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace stackgenriccollection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Stack<string> st = new Stack<string>();

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {

                MessageBox.Show("enter value in textbox");
            }
            else
            {
                st.Push(textBox1.Text);
                MessageBox.Show("inserted");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (st.Count > 0)
            {
                Object obj = new Object();
                obj = st.Pop();
                MessageBox.Show(obj.ToString());
            }
            else
            {

                MessageBox.Show("no element in stack");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

            if (st.Count <= 0)
            {

                MessageBox.Show("stack is empty");
            }
            else
            {
                Object obj = new Object();
                obj = st.Peek();
                MessageBox.Show(obj.ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show(st.Count.ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (st.Count <= 0)
            {

                MessageBox.Show("stack is empty");
            }
            else
            {
                if (st.Contains(textBox2.Text))
                {

                    MessageBox.Show("available");
                }
                else
                {
                    MessageBox.Show("not available");} } }  }     }


Posted By:Tanuj Kumar 
On:Shareyourconscience

Event:Adding a button on run time: By:Tanuj Kumar

Event    
Adding a button on run time:
Adding a button on run time

Adding a button on run time
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eventbuttonexample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button b1 = new Button();
            this.Controls.Add(b1);
            b1.Location = new Point(50, 50);
            b1.Size = new Size(100, 50);
            b1.BackColor = System.Drawing.Color.Aqua;
            b1.Text = "tanuj";
            b1.Click += new EventHandler(a);
        }
        private void  a(Object sender, EventArgs e)
        {
  MessageBox .Show ("button is clicked");}      }}
Adding a textbox on run time:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TextBox tb = new TextBox();
            this.Controls.Add(tb);
            tb.Location = new Point(50, 50);
            tb.TextChanged +=new EventHandler(a);
            tb.BackColor = System.Drawing.Color.Aqua;
        }
        private void a(Object sender, EventArgs e) {

            MessageBox.Show("text is changed");
        }
    }
}


Posted By:Tanuj Kumar