Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

Friday, 31 May 2013

Add numbers in Java program

import java.util.Scanner;

class AddNumbers
{
   public static void main(String args[])
   {
      int x, y, z;
      System.out.println("Enter two integers to calculate their sum ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = x + y;
      System.out.println("Sum of entered integers = "+z);
   }
}

Java program to Add Two Matrices

import java.util.Scanner;

class AddTwoMatrix
{
   public static void main(String args[])
   {
      int m, n, c, d;
      Scanner in = new Scanner(System.in);

      System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n  = in.nextInt();

      int first[][] = new int[m][n];
      int second[][] = new int[m][n];
      int sum[][] = new int[m][n];

      System.out.println("Enter the elements of first matrix");

      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the elements of second matrix");

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            second[c][d] = in.nextInt();

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = first[c][d] + second[c][d];  //replace '+' with '-' to subtract matrices

      System.out.println("Sum of entered matrices:-");

      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
            System.out.print(sum[c][d]+"\t");

         System.out.println();
      }
   }
}

Add elements at beginning and end of LinkedList Java example

import java.util.LinkedList;

public class AddElementsAtStartEndLinkedListExample {

  public static void main(String[] args) {

    //create LinkedList object
    LinkedList lList = new LinkedList();
 
    //add elements to LinkedList
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
 
    System.out.println("LinkedList contains : " + lList);
 
    /*
     * To add an element at the beginning of the LinkedList, use
     * void addFirst(Object obj) method.
     *
     * This method inserts object at the beginning of the LinkedList.
     */
 
     lList.addFirst("0");
     System.out.println("After inserting 0 at beginning, LinkedList contains :"
     + lList);

    /*
     * To append an element at end of the LinkedList, use
     * void addLast(Object obj) method.
     *
     * This method append specified element at the end of the LinkedList.
     */  
 
     lList.addLast("6");
    System.out.println("After appending 0 at end, LinkedList contains :" + lList);

  }
}

/*
Output would be

LinkedList contains : [1, 2, 3, 4, 5]
After inserting 0 at beginning, LinkedList contains :[0, 1, 2, 3, 4, 5]
After appending 0 at end, LinkedList contains :[0, 1, 2, 3, 4, 5, 6]
*/

Add an element to specied index of Java ArrayList



import java.util.ArrayList;

public class AddElementToSpecifiedIndexArrayListExample {

  public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
 
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
 
    /*
      To add an element at the specified index of ArrayList use
      void add(int index, Object obj) method.
      This method inserts the specified element at the specified index in the
      ArrayList.
    */
    arrayList.add(1,"INSERTED ELEMENT");
 
    /*
      Please note that add method DOES NOT overwrites the element previously
      at the specified index in the list. It shifts the elements to right side
      and increasing the list size by 1.
    */

    System.out.println("ArrayList contains...");
    //display elements of ArrayList
    for(int index=0; index < arrayList.size(); index++)
      System.out.println(arrayList.get(index));
 
  }
}

/*
Output would be
ArrayList contains...
1
INSERTED ELEMENT
2
3
*/

Add an element at specified index of java vector

import java.util.Vector;

public class AddElementToSpecifiedIndexVectorExample {

  public static void main(String[] args) {
    //create an Vector object
    Vector v = new Vector();
 
    //Add elements to Vector
    v.add("1");
    v.add("2");
    v.add("3");
 
    /*
      To add an element at the specified index of Vector use
      void add(int index, Object obj) method.
      This method inserts the specified element at the specified index in the
      Vector.
    */
    v.add(1,"INSERTED ELEMENT");
 
    /*
      Please note that add method DOES NOT overwrites the element previously
      at the specified index in the Vector. It shifts the elements to right side
      and increasing the Vector size by 1.
    */

    System.out.println("Vector contains...");
    //display elements of Vector
    for(int index=0; index < v.size(); index++)
      System.out.println(v.get(index));
 
    /*
       To append an element at the end of Vector use
       boolean add(Object o) method.
       It returns true as a general behavior of the Collection.add method and
       appends the specified element at the end of Vector.
    */
  }
}

/*
Vector contains...
1
INSERTED ELEMENT
2
3
*/

A Simple Filter Java Program

public class RangeFilter {
    public static void main(String[] args) {

        // read in two command-line arguments
        int lo = Integer.parseInt(args[0]);
        int hi = Integer.parseInt(args[1]);

        // repeat as long as there's more input to read in
        while (!StdIn.isEmpty()) {

            // read in the next integer
            int t = StdIn.readInt();

            // print out the given integer if it's between lo and hi
            if (t >= lo && t <= hi) {
                StdOut.print(t + " ");
            }
        }
        StdOut.println();
    }
}

Hello World - Java Program

public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello, World");
   }
}

Sunday, 26 May 2013

JavaScript Popup Window Program

<html>
<head>
<script type="text/javaScript">
function see()
{
var c= confirm("Click OK to see Google Homepage or CANCEL to see Bing Homepage");
if (c== true)
{
window.location="http://www.google.co.in/";
}
else
{
window.location="http://www.bing.com/";
}
}
</script>
</head>
<body>
<center>
<form>
<input type="button" value="Click to chose either Google or Bing" onclick="see()">
</form>
</center>
</body>
</html>

Form validation in JavaScript program

<html>
<head>
<script type="text/javascript">
function sub()
{
if(document.getElementById("t1").value == "")
alert("Please enter your name");
else if(document.getElementById("t2").value == "")
alert("Please enter a password");
else if(document.getElementById("t2").value != document.getElementById("t3").value)
alert("Please enter correct password");
else if(document.getElementById("t4").value == "")
alert("Please enter your address");
else
alert("Form has been submitted");
}
</script>
</head>
<body>
<form>
<p align="center">
User Name:<input type="text" id="t1"><br><br>
Password:<input type="text" id="t2"><br><br>
Confirm Password:<input type="text" id="t3"><br><br>
Address:<textarea rows="2" cols="25" id="t4"></textarea><br><br>
<input type="button" value="Submit" onclick="sub()">
<input type="reset" value="Clear All">
</p>
</form>
</body>
</html>

Copy Text JavaScript Program

<html>
<head>
<title>
Copy text</title>
</head>
<body>
<center>
<h2>Copy text from different field</h2>
<p>
<input type="text" style="color: #FF0080;background-color: #C9C299" id="field1" value="Good Morning">
<input type="text" style="color: #FF0080;background-color: #C9C299" id="field2">
<button style="background-color: #E799A3" onclick="document.getElementById('field2').value = document.getElementById('field1').value">Click to Copy Text
</p>
</center>
</body>
</html>

Fibonacci Series JavaScript Program

<html>
<body>
<script type="text/javascript">
var a=0,b=1,c;
document.write("Fibonacci");
while (b<=10)
{
document.write(c);
document.write("<br/>");
c=a+b;
a=b;
b=c;
}
</script>
</body>
</html>