Gadgets

Monday, 23 November 2015

Swapping two numbers without using third variable

Swapping two numbers without using third variable
 Type one:
   public class Swapping {
    public static void main(String[] arg){
        int a=100,b=200;
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("a value is : "+a+" b value is :"+b);
    }   
}
Output:
a value is : 200 b value is :100


Type two:
public class Swapping {  
    public static void main(String[] arg){
        int a=57,b=67;
        a=a*b;
        b=a/b;
        a=a/b;
        System.out.println("a value is : "+a+" b value is :"+b);
    }   
}
Output:
a value is : 67 b value is :57

Swapping two numbers using third variable

Swapping two numbers using third variable
public class Swapping {
    public static void main(String[] arg){
        int a=10,b=20,c;
        c=a;
        a=b;
        b=c;
        System.out.println("a value is : "+a+" b value is :"+b);
    }   
}
Output:

a value is : 20 b value is :10

Sunday, 22 November 2015

Enter the values through keyboard

Enter the values through keyboard: core java Programs
We have two ways to give input through keyboard
1.    Using BufferedReader class
2.    Using Scanner class
Using BufferedReader class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
               public static void main(String[] args)throws IOException
               {
                               BufferedReader br=new BufferedReader(new                                                                                    InputStreamReader(System.in));
                               System.out.println("Enter name");
                               String name=br.readLine();
                               System.out.println("Enter Id");
                               int id=Integer.parseInt(br.readLine());
                               System.out.println("Enter Salary");
                               double salary=Double.parseDouble(br.readLine());
                               System.out.println("Enter Section");
                               char section=(char)br.read();
                               System.out.println("Name : "+name+" Id : "+id);
                               System.out.println("Salary : "+salary+" Section : "+section);
               }
}

Output:-
D:\Java>javac Test.java
D:\Java>java Test
Enter name
Narendar
Enter Id
1214
Enter Salary
20000
Enter Section
A
Name : Narendar Id : 1214
Salary : 20000.0 Section : A


Using Scanner class
import java.io.IOException;
import java.util.Scanner;
public class Test {
               public static void main(String[] args)throws IOException 
               {
                               Scanner sc=new Scanner(System.in);
                               System.out.println("Enter name");
                               String name=sc.nextLine();
                               System.out.println("Enter Id");
                               int id=sc.nextInt();
                               System.out.println("Enter Salary");
                               double salary=sc.nextDouble();
                               System.out.println("Enter Section");
                               String section=sc.next();
                               System.out.println("Name : "+name+" Id : "+id);
                               System.out.println("Salary : "+salary+" Section : "+section);
 
               }
}
Output:-
D:\Java>javac Test.java
D:\Java>java Test
Enter name
Durga
Enter Id
1212
Enter Salary
20000
Enter Section
B
Name : Durga Id : 1212
Salary : 20000.0 Section : B
 


Thursday, 19 November 2015

Disabling the right click on web page

Disabling the right click on web page
We can secure our web page by using disable the right click option on web page, if we use this option users can’t copy the matter from our web page and they can’t copy images also.
This is one of the security option for our web page and it is a simple technique to avoid right click option.
 Here is the code for avoid right click option
<html>
<head>
<script>
document.onmousedown=disableclick;
function disableclick(event)
{
  if(event.button==2)
   {    
     return false;    
   }
}
</script>
        </head>
    <body oncontextmenu="return false">
               </body>

               </html>

Include one HTML file into other HTML file

Include one HTML file into other HTML file 
We have different ways to include one html file into other html page, using 
1.            <frame>
2.            <iframe>
3.            <java script>. we have different ways in java script.

if we use <frame> and <iframe> to include one html file into other html file, that web page shows border lines for each html file and it look like separate pages.  
Here is the simple way to include one html file into other html file by using javascript. 
Using <frame>: 
Here I taken 4 HTML files
header.html, body.html, footer.html
I included above three html files into home.html 
<html>
    <frameset rows="50px,150px,50px">
        <frame src="header.html"/>
        <frame src="body.html"/>
        <frame src="footer.html"/>
    </frameset>   
</html> 

Output for above example:
 Using javascript: 
Here I taken 3 html files: header.html, body.html, footer.html
I included header.html and footer.html files into body.html file.

       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Above link is required 
Javascript code for include html file
        <script>
             $(function(){
        $("#headerContent").load("header.html");
        $("#footerContent").load("footer.html"); 
    });
        </script>
<div id="headerContent"></div>
<div id="footerContent"></div>
 Header.html 
<html>   
    <body>       
       <h1>Welcome to Hyderabad</h1>
        <h3> HOME | ABOUTUS | SERVICES | VISIT PLACES | CAREER | CONTACT </h3>       
    </body>
</html> 
 Footer.html 
<html>   
    <body>
              <h1>Thank you, visit  again</h1>       
    </body>
</html>  
Body.html
<html>
    <head>
       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
             $(function(){
        $("#headerContent").load("header.html");
        $("#footerContent").load("footer.html"); 
    });
        </script>        
    </head>
    <body>
        <div id="headerContent"></div>        
        <h1>Tourist Places</h1>
        <p>1. Charminar</p>
        <p>2. Hussain sagar</p>
        <p>3. Birla mandir</p>
        <p>4. Ramoji film city</p>
        <p>5. Golconda fort</p>       
        <div id="footerContent"></div>
    </body>
</html>
Output for above example:


Saturday, 14 November 2015

set environment variables and first java program in detailed

set environment variables and first java program in detailed


1.    Set Permanent path:

Go to MyComputer properties à advanced tab à environment variables ànew tab of user variable à write path in variable name à write path of bin folder in variable value   à ok à ok à ok.

1.    Right click on MyComputer and select Properties

2.    Click on Advanced system settings

3.    Click on Environment Variables

4.    Click on New in user variables

5.    Copy bin folder path

6.    Write “path” in variable name and past the path of bin folder.in variable value and click on “ok”.

7.    Click on “ok”

8.    Click on “ok”


1.   
Temporary path:

Copy the bin path and past it in command prompt as
“set path= C:\Program Files\Java\jdk1.6.0\bin”

Here I set temporary path for D driveàprograms folder because I save all my programs in that folder.

First java program in detailed:

public class Test
{
  public static void main(String[] arg)
    {
           System.out.println("This is my first java program");
     }
}

Open notepad and write the above program



I saved that program as “Test.java” in drive Dàprograms folder


Compile it “javac Test.java” and after compiled it, created a .class file


Run that .class file as “java Test” command


Explanation:

public class Test

public keyword is an access modifier, it mean other classes can access this class.
class is a keyword to declare the class name.
“Test” is a class name of this program.
{ open brace, it mean starting of the class scope.

public static void main(String[] arg)

static is a keyword. If we declare a method as static, no need to create object for that method. Here no need to create object to invoke main method. The main method is executed by JVM.
void is return type. It does not return any value.
“main” is a method name. It represents startup of the program.
(String[] arg) is main method parameter, it stores the  values as String array format  “arg” is a variable, we can give other name whatever we want(a[],b[],ect..).

Example:

String a=arg[0];
String b=arg[1];
{ open brace for main method.

System.out.println("This is my first java program");

System is a class in the java.lang package.
“out” is a static member of the System class
“println” is a method of the java.io.PrintStream
} close main method
} close class

Execution:


Save the above program as: “Test.java”
Compile:  javac “Test.java”
Execution:  java “Test.java”
Output:   This is my first java program