Thursday, December 8, 2011

LinearSearch

import javax.swing.*;
public class LinearSearch{
    int [] array;
int n;
   
   
    public LinearSearch(int n){
        this.n = n;
        array = new int[n];
        for(int i = 0; i< n; i++){
            array[i]=getRandom(0,2*n);
        }
    }
   
    public int search(int key){
        int index=0;
        while (index < n && key!=array[index]) index++;
       
        if (index==n) return -1;
        return index;
    }
   
    public static int getRandom(int from, int to){
        return (int)Math.round(Math.random()*(to-from)+from);
    }

public void shortarray(int arraySize){
int temp=0;
for(int i=0;i<arraySize;i++){
for(int j=i;j<arraySize;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
/*System.out.println();

for(int i=0;i<2000;i++){
System.out.print(array[i]+" ");
}*/
}


     public static void main(String args[]){
        int arraySize=2000;
LinearSearch ls = new LinearSearch(arraySize);

ls.shortarray(arraySize);

        for(int repeat=0; repeat< 100; repeat++){
   int numberOfTries=5000;
            int key, result;
            int successCount=0;
            int failureCount=0;
            int averageComparisonForSuccess=0;
            for(int i=0; i< numberOfTries; i++){
                key=ls.getRandom(0,2*arraySize);
                result=ls.search(key);
               
                if (result==-1) failureCount++;
                else{successCount++;
                    averageComparisonForSuccess+=result+1;
                }
            }
            if (successCount>0) averageComparisonForSuccess=averageComparisonForSuccess/successCount;
            String analysisResult="---Search Analysis Report "+(repeat+1)+"---\n"+
            "Array Size="+ls.array.length+
            "\nNumber of Tries="+numberOfTries+
            "\nSuccessfull Searches="+successCount+"\n"+
            "Unsuccessfull Searches="+failureCount+"\n"+
            "Average Count of Comparisons in Successfull Searches="+ averageComparisonForSuccess+
            "\n---End of Report--\n";
           
         
     
            System.out.println(analysisResult);

        }

    }


}

Wednesday, November 30, 2011

convert the 1D array to 2D array

public class darry{
int[] d={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7};
int[][] td=new int[4][4];

public void put(int k,int e){
int i=k/4;
int j=k%4;
td[i][j]=e;
}
public void stor(){
for(int k=0;k<16;k++){
int e=d[k];
put(k,e);
}
}
public int get(int k){
int i=k/4;
int j=k%4;
return td[i][j];
}
public void print(){
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] a){
darry s=new darry();
s.stor();
s.print();
System.out.println(s.get(5));
System.out.println(s.get(10));
System.out.println(s.get(15));
}
}

convert the 2D array to 1D array

import java.util.*;
public class tDarry{
int n,c,r,e;

int[] d=new int[16];
int td[][]={{1,2,3,4},{5,4,6,8},{9,8,7,5},{3,6,4,7}};

public void put(int i,int j,int e){
int k=i*4+j;
  d[k]=e;

}
public int get(int i,int j){
int[] d=new int[16];
int k=i*4+j;
return d[k];
}
public void stor(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){

int e=td[i][j];
put(i,j,e);

}
}

}
public void print(){

for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
}
public static void main(String[] a){

tDarry s=new tDarry();
s.stor();
s.print();

}
}

Wednesday, November 16, 2011

find next odd,even,prime and Composite numbers

import java.util.*;
public class mynumber{
int num,odd,even,comp,prime;
public mynumber(){
num=0;
odd=1;
even=1;
comp=4;
prime=3;
}

public int next_number(){
num=num+1;
return num;
}

public boolean isodd(int n){
if(n%2==1)
return true;
else return false;
}

public int next_odd(){
odd=odd+1;
while(!isodd(odd)){
odd=odd+1;
}
return odd;
}

public int next_even(){
even=even+1;
while(isodd(even)){
even=even+1;
}
return even;
}
public int next_comp(){
comp=comp+1;

for(int i=2;i<comp;i++){
if(comp%i==0) break;
else comp=comp+1;
}
return comp;
}
public int next_prime(){
prime=prime+1;
int x=0;
for(int i=2;i<prime;i++){
if(prime%i==0)  prime=prime+1;
else break;
}
return prime;
}

public static void main(String[] a){
mynumber n=new mynumber();

System.out.println("next odd number= "+n.next_odd());
System.out.println("next even number= "+n.next_even());
System.out.println("next Composite number= "+n.next_comp());
System.out.println("next prime number= "+n.next_prime());
}
}
/*C:\Users\KAPILAN\Desktop>javac mynumber.java

C:\Users\KAPILAN\Desktop>java mynumber
next odd number= 3
next even number= 2
next Composite number= 6
next prime number= 5*/

Monday, October 24, 2011

Color Change

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class colorchange extends JPanel implements ActionListener{
static JFrame frame1;
JButton b1;
JButton b2;
JButton b3;

public colorchange(){
b1=new JButton("Red");
b2=new JButton("Green");
b3=new JButton("Blue");
b1.setMnemonic('R');
b2.setMnemonic('G');
b3.setMnemonic('B');
b1.setToolTipText("Red");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1);
add(b2);
add(b3);
b1.requestFocus();
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if(source==b1)
setBackground(Color.red);
else if(source==b2)
setBackground(Color.green);
else if(source==b3)
setBackground(Color.blue);
}
public static void main(String[] a){
frame1=new JFrame("Color Change");
colorchange c1=new colorchange();
frame1.getContentPane().add("Center",c1);
frame1.setSize(200,150);
frame1.addWindowListener(new WindowAdapter(){
public void WindoClosing(WindowEvent e){
System.exit(0);
}
});
frame1.setVisible(true);
}
}

Tuesday, October 11, 2011

Find prime number

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Findprimes extends JFrame implements Runnable, ActionListener{
Thread go;
JLabel howmanylabel=new JLabel("Quantity: ");
JTextField howmany=new JTextField("400",10);
JButton display=new JButton("Display primes");
JTextArea primes=new JTextArea(8,40);

Findprimes(){
super("Find prime Numbers");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content=getContentPane();
BorderLayout bord=new BorderLayout();
content.setLayout(bord);
display.addActionListener(this);

JPanel topPanel=new JPanel();
topPanel.add(howmanylabel);
topPanel.add(howmany);
topPanel.add(display);

content.add(topPanel,BorderLayout.NORTH);
primes.setLineWrap(true);
JScrollPane textPane=new JScrollPane(primes);
content.add(textPane,BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
display.setEnabled(true);
if(go==null){
go=new Thread(this);
go.start();
}

}
public void run(){
int quantity=Integer.parseInt(howmany.getText());
int numprimes=0;
int candidate=2;
primes.append("First "+quantity+" primes;");
while(numprimes<quantity){
if(isPrime(candidate)){
primes.append(candidate+" ");
numprimes++;
}
candidate++;
}
}
public static boolean isPrime(int checkNumber){
double root=Math.sqrt(checkNumber);
for(int i=2;i<root;i++){
if(checkNumber%i==0)
return false;
}
return true;
}
public static void main(String[] a){
Findprimes fp=new Findprimes();
}
}

Thursday, September 29, 2011

Triangle

public class point{
public double x;
public double y;
public point(double x,double y){
this.x=x;
this.y=y;
}
public void setx(double nx){x=nx;}
public void sety(double ny){y=ny;}
public double getx(){return x;}
public double gety(){return y;}
public String toString(){
return"("+x+","+y+")";
}
}

........................................................................................

public class line{
point p1,p2;
 public line(point p1, point p2){
 this.p1=p1;
 this.p2=p2;
 }
public line(double x1,double y1,double x2, double y2){
this.p1=new point(x1,y1);
this.p2=new point(x2,y2);
}
public double getlength(){
double dx=p1.x-p2.x;
double dy=p1.y-p2.y;
return Math.round(Math.sqrt(dx*dx+dy*dy)*100)/100.0;
}
public point getp1(){return p1;}
public point getp2(){return p2;}
public void setp1(point np){p1=np;}
public void setp2(point np){p2=np;}
public void setp1(double x,double y){
p1=new point(x,y);
}
public void setp2(double x,double y){
p2=new point(x,y);
}
public String toString(){
return "line "+p1+"__"+p2+"of length "+getlength();
}
}
............................................................................................

public class triangle{
private point p1,p2,p3;
private double area,perimeter;
public triangle(double x1,double y1,double x2,double y2,double x3,double y3){
p1=new point(x1,y1);
p2=new point(x2,y2);
p3=new point(x3,y3);
computearea();
computeperimeter();
}
public triangle(point p1,point p2,point p3){
this.p1=p1;
this.p2=p2;
this.p3=p3;
computearea();
computeperimeter();
}
public point getp1(){return p1;}
public point getp2(){return p2;}
public point getp3(){return p3;}
public void computearea(){
double a=new line(p1,p2).getlength();
double b=new line(p1,p3).getlength();
double c=new line(p3,p2).getlength();
double s=(a+b+c)/2.0;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public void computeperimeter(){
double a=new line(p1,p2).getlength();
double b=new line(p1,p3).getlength();
double c=new line(p3,p2).getlength();
perimeter=a+b+c;
}
public double getarea(){return Math.round(area*100)/100.0;}
public double getperimeter(){return perimeter;}
public boolean isrectangledtriangle(){
double a=new line(p1,p2).getlength();
double b=new line(p1,p3).getlength();
double c=new line(p3,p2).getlength();
double tmp;
if(b>a){tmp=a;a=b;b=tmp;}
if(c>a){tmp=a;a=c;c=tmp;}
return(a*a==b*b+c*c);
}
public String toString(){
String retString="";
String arectangled="not a rectangled";
if(isrectangledtriangle()) arectangled=" a rectangled";
retString="triangle "+p1+"::"+p2+"::"+p3+"::"+"is"+arectangled+"triangle\n";
retString=retString+"and it is of aea::"+getarea()+", and primeter::"+getperimeter();
return retString;
}
}
....................................................................................................
import javax.swing.JOptionPane;
public class triangledemo{

public static void main(String[] argc){
triangle t1=new triangle(0,0,4,4,2,3);
line l1=new line(new point(0,0),new point(4,4));
line l2=new line(new point(0,0),new point(2,3));
line l3=new line(4,4,2,3);
point A=new point(3,4);
point B=new point(6,4);
point C=new point(3,8);
triangle t2=new triangle(A,B,C);
JOptionPane.showMessageDialog(null,l1+"\n"+l2+"\n"+l3+"\n"+t1+"\n"+t2,"triangles and line",1);
}
}

Wednesday, September 28, 2011

print

public class print{
public static void main(String[] a){
int intNum1=100;
int intNum2=50;
System.out.print("the values are; ");
System.out.printf("10%d and %10d\n",intNum1,intNum2);
System.out.printf("%-10d\n",intNum1);
System.out.printf("%-10d\n",intNum2);
System.out.printf("%10d\n",intNum1);
System.out.printf("%10d\n",intNum2);

}
}
/*the values are; 10100 and         50
100
50
       100
        50
*/

JOptionPane

import java.util.*;
import javax.swing.JOptionPane;
public class WelcometoJavaDialog{

public static void main(String[] a){
Scanner input=new Scanner(System.in);
String fullname=JOptionPane.showInputDialog("What is your name,in full?");
String nam1=input.next();
String nam2=input.next();

System.out.print("\nhellow  "+nam1+" "+nam2);
System.out.println("-congratuations on write your first "+" java program which features some input!\n\n");

JOptionPane.showMessageDialog(null,"\nhello  "+nam1+" "+nam2+"\n-congratuations on write your first "+"\njava program which features some input!","welcome to java input",JOptionPane.PLAIN_MESSAGE);
}
}

Friday, September 23, 2011

Z score

import java.util.*;
public class Z{
public double mew(int[] c){
double n=0;
for(int i=0;i<c.length;i++){
n+=c[i];
}
return (n/c.length);
}
public double valu(int[] c){
double k=0;
for(int i=0;i<c.length;i++){
k+=Math.pow((c[i]-mew(c)),2);
}
return Math.sqrt((k/(c.length-1)));
}
public double zs(int[] c,int n){
return ((c[n-1]-mew(c))/valu(c));
}
public static void main(String[] a){
Scanner s=new Scanner(System.in);
int[] c={80,99,40,30,98,48,55,88,66,63,11,20,30,70,50,90,32,33,65,69,89,90,70};
int n;
System.out.println("enter the number");
n=s.nextInt();
Z b=new Z();
System.out.println("Z= "+b.zs(c,n));

}
}

yesterday and tomorrow

import java.util.*;
public class date{
public static boolean isleapeyear(int y){
if(y%100==0){
return (y%400==0);}
else{
return (y%4==0);}
}

public static int daysinthemonth(int m,int y){
int d=0;
switch(m){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:d=31;break;
case 4:case 6:case 9:case 11:d=30;break;
case 2:
if(isleapeyear(y)){
d=29;}
else {d=28;}break;
default:d=-1;
}
return d;
}

public void tomorrow(int d,int m,int y){
if((d==31)&&(m==12)){
System.out.print("tomorrow is"+" "+"1"+" "+"1"+" "+(y+1));
}
else if(d==daysinthemonth(m,y)){
System.out.print("tomorrow is "+"1"+" "+(m+1)+" "+(y));
}
else{
System.out.print("tomorrow is "+(d+1)+" "+m+" "+y);
}
}

public void yesterday(int d,int m,int y){
if((d==1)&&(m==1)){
System.out.print("yesterday is"+" "+"31"+" "+"12"+" "+(y-1));
}
else if(d==1){
System.out.print("yesterday is"+" "+(daysinthemonth((m-1),y))+" "+(m-1)+" "+y);
}
else{
System.out.print("yesterday is"+" "+(d-1)+" "+m+" "+y);
}
}


public static void main(String[] a){
Scanner input=new Scanner(System.in);
int m,y,n,d;
System.out.println("year");
y=input.nextInt();
System.out.println("month");
m=input.nextInt();
System.out.println("day");
d=input.nextInt();
date b=new date();


b.yesterday(d,m,y);
System.out.println();
System.out.println("today "+d+" "+m+" "+y);
b.tomorrow(d,m,y);

}
}

Friday, July 22, 2011

your aeg

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Date extends JFrame implements ActionListener
{
JLabel de1,mo1,ye1,de2,mo2,ye2,de;
JTextField di1,mi1,yi1,di2,mi2,yi2,di;
JButton ok,reset;
Date()
{
super("your age");
Container c=getContentPane();
c.setLayout(new FlowLayout());
de1=new JLabel("day ");
di1=new JTextField(2);

mo1=new JLabel("month ");
mi1=new JTextField(2);

ye1=new JLabel("year ");
yi1=new JTextField(4);

de2=new JLabel("birth day");
di2=new JTextField(2);

mo2=new JLabel(" birth month ");
mi2=new JTextField(2);

ye2=new JLabel("birth year ");
yi2=new JTextField(4);

de=new JLabel("your age");
di=new JTextField(20);

ok=new JButton("OK");
reset=new JButton("Reset");
ok.addActionListener(this);
reset.addActionListener(this);

c.add(de1);
c.add(di1);

c.add(mo1);
c.add(mi1);

c.add(ye1);
c.add(yi1);

c.add(de2);
c.add(di2);

c.add(mo2);
c.add(mi2);

c.add(ye2);
c.add(yi2);

c.add(de);
c.add(di);

c.add(ok);
c.add(reset);
setSize(500,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton but=(JButton)e.getSource();
if(but.getText()=="OK")
{
int d1=Integer.parseInt(di1.getText());
int m1=Integer.parseInt(mi1.getText());
int y1=Integer.parseInt(yi1.getText());
int d2=Integer.parseInt(di2.getText());
int m2=Integer.parseInt(mi2.getText());
int y2=Integer.parseInt(yi2.getText());
//int d1,d2,m1,m2,y1,y2;
int[] mo={31,28,31,30,31,30,31,31,30,31,30,31};
int m3=0;
int y3=0;
int d=0;
int m=0;
int y=0;


if((y1%4==0)&&(y1%400==0)){

mo[1]=29;
}
else{
mo[1]=28;
}
for(int i=0;i<mo.length;i++){
if(d1<d2){
d=d1+mo[m1-1]-d2;
m3=m1-1;
if(m3<m2){
m=m3+12-m2;
y3=y1-1;
y=y3-y2;
}
else{
m=m1-m2;
y=y1-y2;
}
}
else{
d=d1-d2;
if(m1<m2){
m=m1+12-m2;
y=y1-1-y2;
}
else{
m=m1-m2;
y=y1-y2;
}
}
}

di.setText("day="+Integer.toString(d)+"  "+"month="+Integer.toString(m)+"  "+"year="+Integer.toString(y));
}
else if(but.getText()==("Reset"))
{
di1.setText("");
mi1.setText("");
yi1.setText("");

di2.setText("");
mi2.setText("");
yi2.setText("");

di.setText("");
}
}
public static void main(String[] a)
{
Date b=new Date();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Wednesday, July 20, 2011

java 3

public class ex{
public static void main(String[] args){
int[][] a={{1,2,3,4},{2,3,4,5},{3,4,5,6},{5,5,6,7},{4,6,7,8}};
int max=0;
for(int r=0;r<5;r++)
{
for(int c=0;c<4;c++)
{
System.out.print(a[r][c]+" ");
}
System.out.println();
}
System.out.println();

max=a[0][0];

for(int r=1;r<5;r++)
{
for(int c=1;c<4;c++)
{
if(a[r][c]>max)
{
max=a[r][c];
}
}
System.out.print(max+" ");
}
}
}
/*C:\Users\KAPILAN\Desktop\kk>javac ex.java

C:\Users\KAPILAN\Desktop\kk>java ex
1 2 3 4
2 3 4 5
3 4 5 6
5 5 6 7
4 6 7 8

5 6 7 8
C:\Users\KAPILAN\Desktop\kk>*/

Tuesday, July 19, 2011

java 2

public class Sortings
{
//int[] a;//
private  void bubbleSort(int[] a,int n)
{

int temp=0;
System.out.println(" ");

for(int i=0;i<n;i++)
{
for(int j=1;j<(n-i);j++)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
/*print()
{
System.out.print(a[i]+" ");
}
*/
}
}
private void print(int[] a,int n)
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
public static void main(String[] args)
{
int a[]={5,1,2,4,3};
int n=a.length;
//bubbleSort.print();//
Sortings b=new Sortings();

b.bubbleSort(a,n);
b.print(a,n);
}
}
/*
C:\Users\Level 1G\Desktop\level1g\kk>javac Sortings.java

C:\Users\Level 1G\Desktop\level1g\kk>java Sortings

1 2 3 4 5
C:\Users\Level 1G\Desktop\level1g\kk>*/

Saturday, July 16, 2011

java 1

public class b
{
 public double recDome(int x,int n)
{

if(n==0) return 1;
if(n==1) return x;
if(n>1)
{
double p=recDome(x,n/2);
if(n%2==0) return p*p;
return x*p*p;
}
return 1;
}

public void print(int l)
{

System.out.print(l);
}
public static void main(String[] a)
{
b t=new b();

}
}
.......................................................................................................................


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Bases extends JFrame implements ActionListener
{
JLabel Base,exponent,output;
JTextField b,m,o;
JButton ok,reset;
Bases()
{
super("base");
Container c=getContentPane();
c.setLayout(new FlowLayout());
Base=new JLabel("Base");
b=new JTextField(9);

exponent=new JLabel("exponent");
m=new JTextField(9);

output=new JLabel("output");
o=new JTextField(9);

ok=new JButton("OK");
reset=new JButton("Reset");
ok.addActionListener(this);
reset.addActionListener(this);

c.add(Base);
c.add(b);

c.add(exponent);
c.add(m);

c.add(output);
c.add(o);
c.add(ok);
c.add(reset);
setSize(500,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton but=(JButton)e.getSource();
if(but.getText()=="OK")
{
String B=b.getText();
String E=m.getText();
int x=Integer.parseInt(B);
int n=Integer.parseInt(E);

b t=new b();


double l=t.recDome(x,n);
o.setText(Double.toString(l));
}
else if(but.getText()==("Reset"))
{
b.setText("");
m.setText("");
o.setText("");
}
}
public static void main(String[] a)
{
Bases b=new Bases();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Friday, July 15, 2011

java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NIC extends JFrame implements ActionListener
{
JLabel NICno, Datofbath,Sex;
JTextField NIC,date,sex;
JButton ok,reset;
NIC()
{
super("Find the date of bath use NIC number");
Container c=getContentPane();
c.setLayout(new FlowLayout());
NICno=new JLabel("NIC number");
NIC=new JTextField(9);

Datofbath=new JLabel("Date of bath");
date=new JTextField(9);

Sex=new JLabel("SEX");
sex=new JTextField(9);

ok=new JButton("OK");
reset=new JButton("Reset");
ok.addActionListener(this);
reset.addActionListener(this);

c.add(NICno);
c.add(NIC);

c.add(Datofbath);
c.add(date);

c.add(Sex);
c.add(sex);
c.add(ok);
c.add(reset);
setSize(500,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton but=(JButton)e.getSource();
if(but.getText()=="OK")
{
String ic=NIC.getText();
String t,d,l,s;
t=ic.substring(0,2);
d=ic.substring(2,5);
l=ic.substring(6,9);
int x,y,z,m;
m=0;
z=0;
x=Integer.parseInt(d);
y=Integer.parseInt(l);
System.out.print(d+"\n");
int[] a={31,60,91,121,152,182,213,244,274,305,335,366};
for(int i=0;i<a.length-1;i++)
{
if((x>=a[i])&&(x<a[i+1]))
{
z=x-a[i];
m=i+2;
}
else if(x<=a[1])
{
z=x;
m=1;
}
}
if(y>=500)
{
s="male";
}
else
{
s="female";
}
date.setText(t+"."+Integer.toString(m)+"."+Integer.toString(z));
sex.setText(s);
}
else if(but.getText()==("Reset"))
{
NIC.setText("");
date.setText("");
sex.setText("");
}
}
public static void main(String[] a)
{
NIC b=new NIC();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}