Friday, 21 October 2016

Write a java Program for Construstor Overloading


import java.util.*;
class Cricketer
{
  String name,team;
  int age;
  Cricketer()
 {
   name="";
   team="";
   age=0;
 }
 Cricketer(String n,String t,int a)

 {
    name=n;
    team=t;
    age=a;
 }

 Cricketer(Cricketer ckt)
 {
      name=ckt.name;
      team=ckt.team;
      age=ckt.age;
 }

 public String toString()
 {
   return "This is" + name + "of" +team;
 }

public static void main(String args[])
{
  Cricketer c1 = new Cricketer();

  Cricketer c2 = new Cricketer("VIRAT","INDIA",27);

  Cricketer c3 = new Cricketer(c2);

System.out.println(c2);
System.out.println(c3);
c1.name="Dhoni";
c1.team="INDIA";
c1.age=35;
System.out.println(c1);
}
}

Write a java Applet Program TO display Check Box (Tea Coffee)

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class NewApplet extends Applet implements ItemListener
{
    Checkbox c=null,t=null;
    public void init()
{
        c=new Checkbox("coffee");
        t=new Checkbox("tea");
        add(c);
        add(t);
        c.addItemListener(this);
        t.addItemListener(this);
     
    }
    public void itemStateChanged(ItemEvent ie)
{
        repaint();
     
    }
    public void paint(Graphics g)
{
    if(c.getState()==true)
{
        g.drawString("cofee",100,100);    
     
    }
    if(t.getState()==true)
{
        g.drawString("tee",120,100);

    }
    }
}

Write a java program to display Rhombus

import java.util.*;
class Rhombus
{
  public static void main(String args[])
 {
   Scanner sc=new Scanner(System.in);
  System.out.print("Enter the pattern number");
  int pat=sc.nextInt();
  System.out.print("Enter the pattern character symbol");
  String s=sc.next();
  char c = s.charAt(0);
  for( int i=1;i<=pat;i++)
  {
    for(int j=1;j<=(pat-i);j++)
    System.out.print(" ");
    for(int k=1;k<=i;k++)
    System.out.print(c+" ");
    System.out.println();
  }

   for(int i=pat-1;i>=0;i--)
  {
    for(int j=1;j<=(pat-i);j++)
    System.out.print(" ");
    for(int k=1;k<=i;k++)
    System.out.print(c+" ");
    System.out.println();
  }
 }
}

Write a java Program to Sort In Ascending and Descending

import java.util.*;
class SortOrder
{
 public static void main(String args[])
{
  Scanner sc = new java.util.Scanner(System.in);
  System.out.print("Enter the array size");
  int size=sc.nextInt();
  int[] arr= new int[size];
  System.out.println("Enter Array Elements");
  for(int i=0;i<size;i++)
  {
    System.out.print("Element number" +(i+1)+" :");
    arr[i]=sc.nextInt();
  }
  System.out.println("Before Swapping");
  for(int i=0;i<size;i++)
  System.out.println(" "+arr[i]);
  for(int i=0;i<size;i++)
  {
    int temp;
   for(int j=i+1;j<size;j++)
   {
      if(arr[i]>arr[j])
      {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
      }
     }
    }
     System.out.println();
     System.out.println("After sorting in ascending order:");
     for(int i=0;i<size;i++)
     {
       System.out.println(""+arr[i]);
     }
      System.out.println();
      System.out.println("After sorting in descending order:");
      for(int i=size-1;i>=0;i--)
      {
       System.out.println(""+arr[i]);
      }
   }
}
loading...

Write a java program to display Prime Number

import java.util.*;

public class PrimeNumber
{
public static void main(String args [])
{
Scanner sc = new Scanner(System.in);
int num1, num2;
System.out.println("Please enter the first number");
num1 = sc.nextInt();
System.out.println("Please enter the Second number");
num2 = sc.nextInt();
System.out.println("Prime number: ");
for (int i=num1; i <= num2; i++ ){
int j;
for (j=2; j<i; j++){
int n = i%j;
if (n==0){
break;
}
}
if(i == j){
System.out.print(" "+i);
}
}
System.out.println();
}
}

Write a java Program to Perform String Operation

import java.util.*;
class StringOperation
{
  public static void main(String args[])
{
  String first=" ",second=" ";
  Scanner sc=new Scanner(System.in);
  System.out.println("String_Operation");
  System.out.println();
  System.out.print("Enter the first string1");
  first=sc.nextLine();
  System.out.print("Enter the second string2");
  second=sc.nextLine();
  System.out.println("The string are:"+first+","+second);
  System.out.println("The length of string is:"+first.length());
  System.out.println("The length of string is:"+second.length());
  System.out.println("The lowercase of"+first+"is:"+first.toLowerCase());
  System.out.println("The concatenation first and second string:"+first.concat(""+second));
  System.out.println("The first character of"+first+"is:"+first.charAt(0));
  System.out.println("The uppercase of"+first+"is:"+first.toUpperCase());
   System.out.println("The lowercase of"+first+"is:"+first.toLowerCase());
  System.out.println("The occurance of a character in "+first+":");
 String str = sc.next();
 char c = str.charAt(0);
 System.out.println("The "+c+" occurs at position" +first.indexOf(c)+ "in" +first);
 System.out.println("The substring of "+first+" starting from index1 and ending at 4 is:" +first.substring(1,4));
System.out.println("Replacing 'a' with 'o' in" +first+" is:"+first.replace('a','o'));
boolean check=first.equals(second);
if(!check)
System.out.println(first+"and"+second+" are not same");
else

System.out.println(first+"and"+second+" are same");

}
}

Thursday, 20 October 2016

Write a java Applet program to perform KeyBoard Event

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
public class Key extends Applet
implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
case KeyEvent.VK_F1:
showStatus("F1");
break;
case KeyEvent.VK_F2:
showStatus("F2");
break;
case KeyEvent.VK_F3:
showStatus("F3");
break;
case KeyEvent.VK_F4:
showStatus("F4");
break;
case KeyEvent.VK_F5:
showStatus("F5");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}



**********************************************
HTML

<html>
<applet code="Key.class" width="800" height="500">
</applet>
</html>