Sunday, May 13, 2012
N-Queen
public class NQueens {
int[] x;
public NQueens(int N) {
x = new int[N];
}
public boolean canPlaceQueen(int r, int c) {
for (int i = 0; i < r; i++) {
if (x[i] == c || (Math.abs(i - r) == Math.abs(x[i] - c)) )
{
return false;
}
}
return true;
}
public void printQueens(int[] x) {
int N = x.length;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (x[i] == j) {
System.out.print("Q ");
} else {
System.out.print("* ");
}
}
System.out.println();
}
System.out.println();
}
public void placeNqueens(int r, int n) {
for (int c = 0; c < n; c++) {
if (canPlaceQueen(r, c)) {
x[r] = c;
if (r == n - 1) {
printQueens(x);
} else {
placeNqueens(r + 1, n);
}
}
}
}
public void callplaceNqueens() {
placeNqueens(0, x.length);
}
public static void main(String args[]) {
NQueens Q = new NQueens(5);
Q.callplaceNqueens();
}
}
output
Q * * * *
* * Q * *
* * * * Q
* Q * * *
* * * Q *
Q * * * *
* * * Q *
* Q * * *
* * * * Q
* * Q * *
* Q * * *
* * * Q *
Q * * * *
* * Q * *
* * * * Q
* Q * * *
* * * * Q
* * Q * *
Q * * * *
* * * Q *
* * Q * *
Q * * * *
* * * Q *
* Q * * *
* * * * Q
* * Q * *
* * * * Q
* Q * * *
* * * Q *
Q * * * *
* * * Q *
Q * * * *
* * Q * *
* * * * Q
* Q * * *
* * * Q *
* Q * * *
* * * * Q
* * Q * *
Q * * * *
* * * * Q
* Q * * *
* * * Q *
Q * * * *
* * Q * *
* * * * Q
* * Q * *
Q * * * *
* * * Q *
* Q * * *
Tuesday, February 28, 2012
• Read value from a file and sort the values use merge sort and quick sort in java
import java.io.*;
import java.util.*;
public class Arraysort{
int[] list;
int n;
public Arraysort(int n){
this.n=n;
list=new int[n];
}
public void Mergesort(){
System.out.println("merge sort");
mergesort(0,n-1);
print();
}
public void QuickSort(){
System.out.println("Quick sort");
quickSort(0,n-1);
print();
}
public void readFile() throws IOException{
Scanner filesean=new Scanner(new FileReader("inputtext.txt"));
int i=0;
while(filesean.hasNext() && i<n){
list[i]=filesean.nextInt();
i++;
}
System.out.println("input array");
for(int j=0;j<n;j++){
System.out.print(list[j]+" ");
}
System.out.println();
filesean.close();
}
public void mergesort(int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(l,mid);
mergesort(mid+1,r);
merge(l,mid,r);
}
}
private void merge(int l, int m, int r) {
int i=l; int j=m+1; int k=0;
int[] temp=new int[r-l+1];
while(i<=m && j<=r){
if(list[i]<list[j])temp[k++]=list[i++];
else temp[k++]=list[j++];
}
while(i<=m) temp[k++]=list[i++];
while(j<=r) temp[k++]=list[j++];
for(k=0;k<r-l+1;k++) list[l+k]=temp[k];
}
public void quickSort(int p,int q){
int j;
if(p<q){
j=partition(p,q);
quickSort(p,j-1);
quickSort(j+1,q);
}
}
private int partition(int p, int q) {
int i=p;
int j=q+1;
int pivot=list[p];
do{
do{ i++;
}while(list[i]<pivot);
do{j--;
}while(list[j]>pivot);
if(i<j){
swap(i,j);}
}while(i<j);
swap(p,j);
return j;
}
private void swap(int i, int i0){
int temp=list[i];
list[i]=list[i0];
list[i0]=temp;
}
public void print(){
for(int j=0;j<n;j++){
System.out.print(list[j]+" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException{
Arraysort s=new Arraysort(15);
s.readFile();
s.Mergesort();
System.out.println();
s.QuickSort();
}
}
/* Output
input array
1 5 3 2 6 4 9 1 6 8 22 78 25 46 11
merge sort
1 1 2 3 4 5 6 6 8 9 11 22 25 46 78
Quick sort
1 1 2 3 4 5 6 6 8 9 11 22 25 46 78
*/
import java.util.*;
public class Arraysort{
int[] list;
int n;
public Arraysort(int n){
this.n=n;
list=new int[n];
}
public void Mergesort(){
System.out.println("merge sort");
mergesort(0,n-1);
print();
}
public void QuickSort(){
System.out.println("Quick sort");
quickSort(0,n-1);
print();
}
public void readFile() throws IOException{
Scanner filesean=new Scanner(new FileReader("inputtext.txt"));
int i=0;
while(filesean.hasNext() && i<n){
list[i]=filesean.nextInt();
i++;
}
System.out.println("input array");
for(int j=0;j<n;j++){
System.out.print(list[j]+" ");
}
System.out.println();
filesean.close();
}
public void mergesort(int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(l,mid);
mergesort(mid+1,r);
merge(l,mid,r);
}
}
private void merge(int l, int m, int r) {
int i=l; int j=m+1; int k=0;
int[] temp=new int[r-l+1];
while(i<=m && j<=r){
if(list[i]<list[j])temp[k++]=list[i++];
else temp[k++]=list[j++];
}
while(i<=m) temp[k++]=list[i++];
while(j<=r) temp[k++]=list[j++];
for(k=0;k<r-l+1;k++) list[l+k]=temp[k];
}
public void quickSort(int p,int q){
int j;
if(p<q){
j=partition(p,q);
quickSort(p,j-1);
quickSort(j+1,q);
}
}
private int partition(int p, int q) {
int i=p;
int j=q+1;
int pivot=list[p];
do{
do{ i++;
}while(list[i]<pivot);
do{j--;
}while(list[j]>pivot);
if(i<j){
swap(i,j);}
}while(i<j);
swap(p,j);
return j;
}
private void swap(int i, int i0){
int temp=list[i];
list[i]=list[i0];
list[i0]=temp;
}
public void print(){
for(int j=0;j<n;j++){
System.out.print(list[j]+" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException{
Arraysort s=new Arraysort(15);
s.readFile();
s.Mergesort();
System.out.println();
s.QuickSort();
}
}
/* Output
input array
1 5 3 2 6 4 9 1 6 8 22 78 25 46 11
merge sort
1 1 2 3 4 5 6 6 8 9 11 22 25 46 78
Quick sort
1 1 2 3 4 5 6 6 8 9 11 22 25 46 78
*/
Sunday, January 29, 2012
Finding Maximum and Minimum
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serch;
/**
*
* @author KAPILAN
*/
public class MaxMin {
int [] array;
int n,max,min;
public MaxMin(int n){
this.n=n;
array=new int[n];
for(int i=0;i<n;i++){
array[i]=(int) Math.round(Math.random()*22+1000);
}
}
public void findmaxmin(){
max=array[0];
min=array[0];
for(int i=0;i<n;i++){
if(array[i]<min)min=array[i];
else if(array[i]>max)max=array[i];
}
}
public void print(){
findmaxmin();
for(int i=0;i<n;i++){
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("max = "+max);
System.out.print("min = "+min);
}
public static void main(String[] a){
MaxMin s1=new MaxMin(10);
s1.print();
}
}
/*
* 1002 1020 1012 1007 1016 1012 1001 1021 1020 1001
max = 1021
min = 1001
*/
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serch;
/**
*
* @author KAPILAN
*/
public class MaxMin {
int [] array;
int n,max,min;
public MaxMin(int n){
this.n=n;
array=new int[n];
for(int i=0;i<n;i++){
array[i]=(int) Math.round(Math.random()*22+1000);
}
}
public void findmaxmin(){
max=array[0];
min=array[0];
for(int i=0;i<n;i++){
if(array[i]<min)min=array[i];
else if(array[i]>max)max=array[i];
}
}
public void print(){
findmaxmin();
for(int i=0;i<n;i++){
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("max = "+max);
System.out.print("min = "+min);
}
public static void main(String[] a){
MaxMin s1=new MaxMin(10);
s1.print();
}
}
/*
* 1002 1020 1012 1007 1016 1012 1001 1021 1020 1001
max = 1021
min = 1001
*/
binary search
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serch;
/**
*
* @author KAPILAN
*/
public class BSearch {
int [] array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int n;
int key;
public BSearch(){
//this.key=key;
// array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
n=array.length;
}
public int serch(int key){
return serch(key,0,n-1);
}
public int serch(int key,int s, int e) {
int mid=(s+e)/2;
if(e<s){return -1;}
else if(key==array[mid]){return mid;}
else if(key<array[mid]){return serch(key,s,mid-1);}
else{return serch(key,mid+1,e);}
}
public void print(int key){
System.out.println("index of "+key+" = "+serch(key));
}
public static void main(String[] a){
BSearch s1=new BSearch();
s1.print(20);
}
}
/*
index of 10 = 9
* index of 22 = -1
* index of 2 = 1
* index of 0 = -1
* index of 20 = 19
*/
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serch;
/**
*
* @author KAPILAN
*/
public class BSearch {
int [] array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int n;
int key;
public BSearch(){
//this.key=key;
// array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
n=array.length;
}
public int serch(int key){
return serch(key,0,n-1);
}
public int serch(int key,int s, int e) {
int mid=(s+e)/2;
if(e<s){return -1;}
else if(key==array[mid]){return mid;}
else if(key<array[mid]){return serch(key,s,mid-1);}
else{return serch(key,mid+1,e);}
}
public void print(int key){
System.out.println("index of "+key+" = "+serch(key));
}
public static void main(String[] a){
BSearch s1=new BSearch();
s1.print(20);
}
}
/*
index of 10 = 9
* index of 22 = -1
* index of 2 = 1
* index of 0 = -1
* index of 20 = 19
*/
MergeSort
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mergesort;
import javax.swing.JOptionPane;
/**
*
* @author KAPILAN
*/
public class MergeSort {
int[] array;
int size;
public MergeSort(int n){
size=n;
array=new int[size];
for(int i=0;i<size;i++){
array[i]=(int) Math.round(Math.random()*900+100);
}
}
public void mergesort(){
mergesort(0,size-1);
}
public void mergesort(int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(l,mid);
mergesort(mid+1,r);
merge(l,mid,r);
}
}
private void merge(int l, int m, int r) {
int i=l; int j=m+1; int k=0;
int[] temp=new int[r-l+1];
while(i<=m && j<=r){
if(array[i]>array[j])temp[k++]=array[i++];
else temp[k++]=array[j++];
}
while(i<=m) temp[k++]=array[i++];
while(j<=r) temp[k++]=array[j++];
for(k=0;k<r-l+1;k++) array[l+k]=temp[k];
}
public void display(){
int psize=15;
String ds="";
int rows=(size-1)/psize+1;
int k=0;
int i,row;
for(row=1;row<rows;row++){
for(i=0;i<psize;i++)
ds=ds+" : "+array[k++];
ds=ds+"\n";
}
for(i=k;i<size;i++)ds=ds+" : "+array[i];
JOptionPane.showMessageDialog(null,ds,"contents of the array",1);
System.out.print(ds);
}
public static void main(String[] a){
MergeSort s1=new MergeSort(20);
System.out.println("Before Sort");
s1.display();
System.out.println();
s1.mergesort();
System.out.println("After Sort");
s1.display();
}
}
/*Before Sort
: 594 : 646 : 503 : 606 : 156 : 333 : 574 : 878 : 284 : 884 : 742 : 941 : 768 : 517 : 269
: 563 : 853 : 783 : 989 : 117
After Sort
: 989 : 941 : 884 : 878 : 853 : 783 : 768 : 742 : 646 : 606 : 594 : 574 : 563 : 517 : 503
: 333 : 284 : 269 : 156 : 117*/
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mergesort;
import javax.swing.JOptionPane;
/**
*
* @author KAPILAN
*/
public class MergeSort {
int[] array;
int size;
public MergeSort(int n){
size=n;
array=new int[size];
for(int i=0;i<size;i++){
array[i]=(int) Math.round(Math.random()*900+100);
}
}
public void mergesort(){
mergesort(0,size-1);
}
public void mergesort(int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(l,mid);
mergesort(mid+1,r);
merge(l,mid,r);
}
}
private void merge(int l, int m, int r) {
int i=l; int j=m+1; int k=0;
int[] temp=new int[r-l+1];
while(i<=m && j<=r){
if(array[i]>array[j])temp[k++]=array[i++];
else temp[k++]=array[j++];
}
while(i<=m) temp[k++]=array[i++];
while(j<=r) temp[k++]=array[j++];
for(k=0;k<r-l+1;k++) array[l+k]=temp[k];
}
public void display(){
int psize=15;
String ds="";
int rows=(size-1)/psize+1;
int k=0;
int i,row;
for(row=1;row<rows;row++){
for(i=0;i<psize;i++)
ds=ds+" : "+array[k++];
ds=ds+"\n";
}
for(i=k;i<size;i++)ds=ds+" : "+array[i];
JOptionPane.showMessageDialog(null,ds,"contents of the array",1);
System.out.print(ds);
}
public static void main(String[] a){
MergeSort s1=new MergeSort(20);
System.out.println("Before Sort");
s1.display();
System.out.println();
s1.mergesort();
System.out.println("After Sort");
s1.display();
}
}
/*Before Sort
: 594 : 646 : 503 : 606 : 156 : 333 : 574 : 878 : 284 : 884 : 742 : 941 : 768 : 517 : 269
: 563 : 853 : 783 : 989 : 117
After Sort
: 989 : 941 : 884 : 878 : 853 : 783 : 768 : 742 : 646 : 606 : 594 : 574 : 563 : 517 : 503
: 333 : 284 : 269 : 156 : 117*/
QuickSort
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sort;
/**
*
* @author KAPILAN
*/
public class QuickSort {
int[] num;
int n;
public QuickSort(int n){
this.n=n;
// p=0;q=n;
num=new int[n+1];
for(int i=0;i<n;i++){
num[i]= (int) Math.round(Math.random()*n/2+1000);
}
num[n]=99999;
// quickSort(0,n-1);
}
public void quickSort(){
quickSort(0,n-1);
}
public void quickSort(int p,int q){
int j;
if(p<q){
j=partition(p,q);
quickSort(p,j-1);
quickSort(j+1,q);
}
}
private int partition(int p, int q) {
int i=p;
int j=q+1;
int pivot=num[p];
do{
do{ i++;
}while(num[i]<pivot);
do{j--;
}while(num[j]>pivot);
if(i<j){
swap(i,j);}
}while(i<j);
swap(p,j);
return j;
}
private void swap(int i, int i0){
int temp=num[i];
num[i]=num[i0];
num[i0]=temp;
}
public void print(){
for(int i=0;i<n;i++){
System.out.print(num[i]+" ");
}
}
public static void main(String []a){
QuickSort s=new QuickSort(100);
System.out.println("Before Sort");
s.print();
s.quickSort();
System.out.println();
System.out.println("after Sort");
s.print();
}
}
/*Before Sort
1033 1006 1047 1045 1004 1050 1008 1014 1004 1034 1044 1040 1016 1033 1038 1018 1004 1046 1002 1027 1026 1017 1023 1026 1049 1022 1013 1050 1046 1033 1009 1002 1026 1003 1047 1005 1003 1006 1042 1042 1029 1044 1003 1021 1025 1036 1009 1040 1047 1024 1007 1041 1044 1047 1004 1021 1025 1039 1007 1024 1043 1030 1017 1039 1043 1029 1019 1047 1049 1040 1013 1001 1039 1016 1017 1007 1018 1028 1010 1047 1033 1007 1042 1013 1050 1018 1005 1016 1028 1046 1022 1008 1039 1020 1040 1002 1024 1043 1042 1030
after Sort
1001 1002 1002 1002 1003 1003 1003 1004 1004 1004 1004 1005 1005 1006 1006 1007 1007 1007 1007 1008 1008 1009 1009 1010 1013 1013 1013 1014 1016 1016 1016 1017 1017 1017 1018 1018 1018 1019 1020 1021 1021 1022 1022 1023 1024 1024 1024 1025 1025 1026 1026 1026 1027 1028 1028 1029 1029 1030 1030 1033 1033 1033 1033 1034 1036 1038 1039 1039 1039 1039 1040 1040 1040 1040 1041 1042 1042 1042 1042 1043 1043 1043 1044 1044 1044 1045 1046 1046 1046 1047 1047 1047 1047 1047 1047 1049 1049 1050 1050 1050 */
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sort;
/**
*
* @author KAPILAN
*/
public class QuickSort {
int[] num;
int n;
public QuickSort(int n){
this.n=n;
// p=0;q=n;
num=new int[n+1];
for(int i=0;i<n;i++){
num[i]= (int) Math.round(Math.random()*n/2+1000);
}
num[n]=99999;
// quickSort(0,n-1);
}
public void quickSort(){
quickSort(0,n-1);
}
public void quickSort(int p,int q){
int j;
if(p<q){
j=partition(p,q);
quickSort(p,j-1);
quickSort(j+1,q);
}
}
private int partition(int p, int q) {
int i=p;
int j=q+1;
int pivot=num[p];
do{
do{ i++;
}while(num[i]<pivot);
do{j--;
}while(num[j]>pivot);
if(i<j){
swap(i,j);}
}while(i<j);
swap(p,j);
return j;
}
private void swap(int i, int i0){
int temp=num[i];
num[i]=num[i0];
num[i0]=temp;
}
public void print(){
for(int i=0;i<n;i++){
System.out.print(num[i]+" ");
}
}
public static void main(String []a){
QuickSort s=new QuickSort(100);
System.out.println("Before Sort");
s.print();
s.quickSort();
System.out.println();
System.out.println("after Sort");
s.print();
}
}
/*Before Sort
1033 1006 1047 1045 1004 1050 1008 1014 1004 1034 1044 1040 1016 1033 1038 1018 1004 1046 1002 1027 1026 1017 1023 1026 1049 1022 1013 1050 1046 1033 1009 1002 1026 1003 1047 1005 1003 1006 1042 1042 1029 1044 1003 1021 1025 1036 1009 1040 1047 1024 1007 1041 1044 1047 1004 1021 1025 1039 1007 1024 1043 1030 1017 1039 1043 1029 1019 1047 1049 1040 1013 1001 1039 1016 1017 1007 1018 1028 1010 1047 1033 1007 1042 1013 1050 1018 1005 1016 1028 1046 1022 1008 1039 1020 1040 1002 1024 1043 1042 1030
after Sort
1001 1002 1002 1002 1003 1003 1003 1004 1004 1004 1004 1005 1005 1006 1006 1007 1007 1007 1007 1008 1008 1009 1009 1010 1013 1013 1013 1014 1016 1016 1016 1017 1017 1017 1018 1018 1018 1019 1020 1021 1021 1022 1022 1023 1024 1024 1024 1025 1025 1026 1026 1026 1027 1028 1028 1029 1029 1030 1030 1033 1033 1033 1033 1034 1036 1038 1039 1039 1039 1039 1040 1040 1040 1040 1041 1042 1042 1042 1042 1043 1043 1043 1044 1044 1044 1045 1046 1046 1046 1047 1047 1047 1047 1047 1047 1049 1049 1050 1050 1050 */
Wednesday, January 4, 2012
palindrome
public class Stack{
private Object element[];
private int top;
int maxSize;
private String x,y;
public Stack(int s,String x){
maxSize=s;
element=new Object[maxSize];
top=-1;
this.x=x;
y="";
}
public boolean isEmpty(){
return(top==-1);
}
public void push(Object elt){
top=top+1;
element[top]=elt;
}
public Object pop(){
Object e=element[top];
top=top-1;
return e;
}
public Object peek(){
return element[top];
}
public void change(){
for(int i=0;i<x.length();i++){
push(x.charAt(i));
}
}
public void set(){
while(!isEmpty()){
y=y+pop();
}
System.out.println(y);
}
public boolean eguals(){
return(x.compareTo(y)==0);
}
public static void main(String a[]){
Stack s=new Stack(5,"amma");
s.change();
s.set();
System.out.println("palindrome "+s.eguals());
Stack s2=new Stack(5,"ammaa");
s2.change();
s2.set();
System.out.println("palindrome "+s2.eguals());
}
}
Output
/*C:\Users\KAPILAN\Desktop>java Stack
amma
palindrome true
aamma
palindrome false
C:\Users\KAPILAN\Desktop>*/
private Object element[];
private int top;
int maxSize;
private String x,y;
public Stack(int s,String x){
maxSize=s;
element=new Object[maxSize];
top=-1;
this.x=x;
y="";
}
public boolean isEmpty(){
return(top==-1);
}
public void push(Object elt){
top=top+1;
element[top]=elt;
}
public Object pop(){
Object e=element[top];
top=top-1;
return e;
}
public Object peek(){
return element[top];
}
public void change(){
for(int i=0;i<x.length();i++){
push(x.charAt(i));
}
}
public void set(){
while(!isEmpty()){
y=y+pop();
}
System.out.println(y);
}
public boolean eguals(){
return(x.compareTo(y)==0);
}
public static void main(String a[]){
Stack s=new Stack(5,"amma");
s.change();
s.set();
System.out.println("palindrome "+s.eguals());
Stack s2=new Stack(5,"ammaa");
s2.change();
s2.set();
System.out.println("palindrome "+s2.eguals());
}
}
Output
/*C:\Users\KAPILAN\Desktop>java Stack
amma
palindrome true
aamma
palindrome false
C:\Users\KAPILAN\Desktop>*/
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);
}
}
}
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));
}
}
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();
}
}
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*/
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;
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();
}
}
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 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
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
*/
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);
}
}
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));
}
}
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);
}
}
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);
}
}
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>*/
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>*/
Subscribe to:
Posts (Atom)


