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);
}
}
}
Thursday, December 8, 2011
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);
}
}
Subscribe to:
Posts (Atom)

