Mini Project


            
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;

public class calc extends Frame implements ActionListener
{
           String msg= " ";
           String ts=" ";
           String n1,n2;
           char op;
           double a,b,c,prev,memory;
           Button one,two,three,four,five,six,seven,eight,nine,ten,zero,dot,off;
           Button backspace,clear,eq,plus,minus,mul,div,pm,sqrt,obx,sqr,sin,cos,tan;
           Button mc,mr,ms,mplus,mstatus;
           Panel p1=new Panel();
           Panel p2=new Panel();
           Panel p3=new Panel();
           Panel p4=new Panel();

           TextField display;

           public calc()
           {
                      setTitle("Calculator");
                      setLayout(new BorderLayout());
                      display=new TextField(30);
                      one=new Button("1");
                      two=new Button("2");
                      three=new Button("3");
                      four=new Button("4");
                      five=new Button("5");
                      six=new Button("6");
                      seven=new Button("7");
                      eight=new Button("8");
                      nine=new Button("9");
                      ten=new Button("10");
                      zero=new Button("0");
                      dot=new Button(".");
                      eq=new Button("=");
                      plus=new Button("+");
                      minus=new Button("-");
                      div=new Button(" / ");
                      mul=new Button("*");
                      sqrt=new Button("sqrt");
                      sin=new Button("sin");
                      cos=new Button("cos");
                      tan=new Button("tan");
                      clear=new Button("C");
                      mc=new Button("MC");
                      mr=new Button("MR");
                      ms=new Button("MS");
                      mplus=new Button("M+");
                      mstatus=new Button();
                     
                      backspace=new Button("B");
                      off=new Button("off");
                     
                      p1.add(display);
                      add(p1,BorderLayout.NORTH);
                     
                      p4.setLayout(new GridLayout(3,1,5,5));
                      p4.add(backspace);
                      p4.add(clear);
                      p4.add(off);
                      add(p4,BorderLayout.EAST);
                     
                      p3.setLayout(new GridLayout(5,2,5,5));
                      p3.add(mstatus);
                      p3.add(mc);
                      p3.add(mr);
                      p3.add(ms);
                      p3.add(mplus);
                      add(p3,BorderLayout.WEST);
                     
                      p2.setLayout(new GridLayout(5,4,5,5));
                     
                      p2.add(seven);
                      p2.add(eight);
                      p2.add(nine);
                      p2.add(div);
                     
                      p2.add(four);
                      p2.add(five);
                      p2.add(six);
                      p2.add(mul);
                     
                      p2.add(one);
                      p2.add(two);
                      p2.add(three);
                      p2.add(minus);
                     
                      p2.add(zero);
                      p2.add(dot);
                      p2.add(eq);
                      p2.add(plus);
                     
                      p2.add(sin);
                      p2.add(cos);
                      p2.add(tan);
                      p2.add(sqrt);
                     
                      add(p2,BorderLayout.CENTER);
                     
                      display.setEditable(false);
                      p1.setBackground(Color.lightGray);
                      p2.setBackground(Color.lightGray);
                      p3.setBackground(Color.lightGray);
                      p4.setBackground(Color.lightGray);
                     
                      display.addActionListener(this);
                      one.addActionListener(this);
                      two.addActionListener(this);
                      three.addActionListener(this);
                      four.addActionListener(this);
                      five.addActionListener(this);
                      six.addActionListener(this);
                      seven.addActionListener(this);
                      eight.addActionListener(this);
                      nine.addActionListener(this);
                      zero.addActionListener(this);
                      dot.addActionListener(this);
                      eq.addActionListener(this);
                      plus.addActionListener(this);
                      minus.addActionListener(this);
                      clear.addActionListener(this);
                      backspace.addActionListener(this);
                      div.addActionListener(this);
                      mul.addActionListener(this);
                      sqrt.addActionListener(this);
                      sin.addActionListener(this);
                      cos.addActionListener(this);
                      tan.addActionListener(this);
                      off.addActionListener(this);
                      mc.addActionListener(this);
                      mr.addActionListener(this);
                      ms.addActionListener(this);
                      mplus.addActionListener(this);
                     
                      setVisible(true);
                      addWindowListener(new w());
                      setSize(300,300);
                      setResizable(false);
                      pack();
           }

           public void actionPerformed(ActionEvent ae)
           {

                      String str=ae.getActionCommand();
                      if(str.equals("1"))
                      {
                                 ts=ts+"1";
                                 display.setText(ts);
                      }
                      else if (str.equals("2"))
                      {
                                 ts=ts+"2";
                                 display.setText(ts);
                      }
                      else if (str.equals("3"))
                      {
                                 ts=ts+"3";
                                 display.setText(ts);
                      }
                     
  else if (str.equals("4"))
                      {
                                 ts=ts+"4";
                                 display.setText(ts);
                      }
                      else if (str.equals("5"))
                      {
                                 ts=ts+"5";
                                 display.setText(ts);
                      }
                      else if (str.equals("6"))
                      {
                                 ts=ts+"6";
                                 display.setText(ts);
                      }
                      else if (str.equals("7"))
                      {
                                 ts=ts+"7";
                                 display.setText(ts);
                      }
                      else if (str.equals("8"))
                      {
                                 ts=ts+"8";
                                 display.setText(ts);
                      }
                      else if (str.equals("9"))
                      {
                                 ts=ts+"9";
                                 display.setText(ts);
                      }
                      else if (str.equals("0"))
                      {
                                 ts=ts+"0";
                                 display.setText(ts);
                      }
                      else if (str.equals("."))
                      {
                                 ts=ts+".";
                                 display.setText(ts);
                      }
          
                      else if (str.equals("+"))
                      {
                      n1=display.getText();
                      ts=" ";
                      a=Double.parseDouble(display.getText().trim());
                      op='+';
                      display.setText(ts);
                      }
          
                      else if (str.equals("-"))
                      {
                                 n1=display.getText();
                                 ts=" ";
                                 display.setText(ts);
                                 a=Double.parseDouble(n1.trim());
                                 op='-';
                      }
          
                      else if (str.equals(" / "))
                      {
                                 n1=display.getText();
                                 ts=" ";
                                 display.setText(ts);
                                 a=Double.parseDouble(n1.trim());
                                 op='/';
                      }
          
                      else if (str.equals("*"))
                      {
                                 n1=display.getText();
                                 ts=" ";
                                 display.setText(ts);
                                 a=Double.parseDouble(n1.trim());
                                 op='*';
                      }
          
                      else if (str.equals("C"))
                      {
                                 ts=" ";
                                 display.setText(ts);
                                 n1=" ";
                                 n2=" ";
                                 a=0;b=0;c=0;
                                 op='C';
                      }
          
                      else if (str.equals("B"))
                      {
                                 msg=display.getText();
                                 ts=msg.substring(0,msg.length()-1);
                                 display.setText(ts);
                                 op='B';
                      }
          
                      else if (str.equals("sqrt"))
                      {
                                 n1=display.getText();
                                 a=Double.parseDouble(n1.trim());
                                 a=Math.sqrt(a);
                                 ts=" "+a;
                                 display.setText(ts);
                                 //op='*';
                      }
          
                      else if (str.equals("sin"))
                      {
                                 n1=display.getText();
                                 a=Double.parseDouble(n1.trim());
                                 a=Math.sin(a);
                                 ts=" "+a;
                                 display.setText(ts);
                                 //op='*';
                      }
          
                      else if (str.equals("cos"))
                      {
                                 n1=display.getText();
                                 a=Double.parseDouble(n1.trim());
                                 a=Math.cos(a);
                                 ts=" "+a;
                                 display.setText(ts);
                                 //op='*';
                      }
          
                      else if (str.equals("tan"))
                      {
                                 n1=display.getText();
                                 a=Double.parseDouble(n1.trim());
                                 a=Math.tan(a);
                                 ts=" "+a;
                                 display.setText(ts);
                                 //op='*';
                      }
          
                      else if (str.equals("MC"))
                      {
                                 memory=0;
                                 mstatus.setLabel(" ");
                      }
          
                      else if (str.equals("MS"))
                      {
                                 n1=display.getText();
                                 if(n1!=null)
                                 {         a=Double.parseDouble(n1.trim());
                                            memory=a;
                                            mstatus.setLabel("M");
                                 }
                      }

                      else if (str.equals("MR"))
                      {
                                 ts=" "+memory;
                                 display.setText(ts);
                      }
                      else if (str.equals("M+"))
                      {
                                 n1=display.getText();
                                 if(n1!=" ")
                                 {
                                            a=Double.parseDouble(n1.trim());
                                            memory=memory+a;
                                 }
                      }
                      else if (str.equals("tan"))
                      {
                                 n1=display.getText();
                                 a=Double.parseDouble(n1.trim());
                                 a=Math.tan(a);
                                 ts=" "+a;
                                 display.setText(ts);
                                 //op='*';
                      }
else if (str.equals("off"))
                      {
                                 setVisible(false);
                                 System.exit(0);
                      }

                      else if (str.equals("="))
                      {
                                 String n3=" ";
                                 n2=display.getText();
                                 b=Double.parseDouble(n2.trim());

                                 switch(op)
                                 {
                                            case '+':c=a+b;break;
                                            case '-':c=a-b;break;
                                            case '/':c=a/b;break;
                                            case '*':c=a*b;break;
                                            default:c=b;
                                 }
                          n3=n3+c;
                                 op='C';
                                 display.setText(n3);
                      }
           }
           public static void main(String args[])
           {
                      calc c = new calc();
           };
          
           class w extends WindowAdapter
           {
                      public void windowClosing(WindowEvent e)
                      {
                                 setVisible(false);
                                 System.exit(0);
                      }
           }
}

Output:
               


Code:
            

import java.io.*;
import java.util.*;
class Phonebook {
public static void main(String args[])
throws IOException
{
Properties ht = new Properties();
BufferedReader br =

new BufferedReader(new InputStreamReader(System.in));
String name, number;
FileInputStream fin = null;
boolean changed = false;

try {
fin = new FileInputStream("phonebook.dat");
} catch(FileNotFoundException e) {

}

try {
if(fin != null) {
ht.load(fin);
fin.close();
}
} catch(IOException e) {
System.out.println("Error reading file.");
}

do {
System.out.println("Enter new name" +
" ('quit' to stop): ");
name = br.readLine();
if(name.equals("quit")) continue;
System.out.println("Enter number: ");
number = br.readLine();
ht.put(name, number);
changed = true;
} while(!name.equals("quit"));

if(changed) {
FileOutputStream fout = new FileOutputStream("phonebook.dat");
ht.store(fout, "Telephone Book");

fout.close();
}

do {
System.out.println("Enter name to find" +
" ('quit' to quit): ");
name = br.readLine();
if(name.equals("quit")) continue;
number = (String) ht.get(name);
System.out.println(number);
} while(!name.equals("quit"));
}
}

3)Animation using applet
Code:
            
import java.awt.*;
import java.applet.*;

public class Animation extends Applet
{           public void paint(Graphics g)
            {           int x[]={150,100,900,850};//X coordinates for roof
                        int y[]={50,110,110,50};//Y coordinates for roof
                        g.setColor(Color.RED);//Set the roof color equal to red
                        g.fillPolygon(x,y,4);//Draw the roof using X[] and Y[]
                        g.setColor(Color.ORANGE);//Set the wall color equal to orange
                        g.fillRect(120,110,760,400);//Draw the wall
                        g.setColor(Color.WHITE);//Set the  color equal to WHITE
                        g.fillRect(424,210,152,300);//create the door
                        g.setColor(Color.BLACK);//Set the  color equal to BLACK
                        g.drawRect(424,210,152,300);//draw outline for door
                        g.setColor(Color.BLUE);//Set the  color equal to BLUE
                        g.fillRect(197,210,150,150);//fill the window
                        g.fillRect(653,210,150,150);//fill the window
                        g.setColor(Color.YELLOW);//Set the  color equal to YELLOW
                        g.fillRect(267,210,10,150);//fill middle rod of window
                        g.fillRect(723,210,10,150);//fill middle rod of window
                        g.fillRect(424,210,152,300);//draw complete door
                        g.setColor(Color.BLACK);//Set the  color equal to BLACK
                        g.drawRect(424,210,152,300);//draw outline for door
                        g.drawRect(197,210,150,150);//draw outline for the window
                        g.drawRect(653,210,150,150);//draw outline for the window
                        g.drawRect(267,210,10,150);//draw outline for middle rod of window
g.drawRect(723,210,10,150);//draw outline for middle rod of windowg.setColor(Color.magenta
                        g.drawLine(500,210,500,510);//draw middle line between doors
{
                        g.setFont(new Font("Times New Roman",Font.BOLD,72));//set the font
                        g.setColor(Color.GREEN);//set the color to green
                        g.drawString("HOME SWEET HOME",130,175);//draw the string
                        }
                        g.setColor(Color.WHITE);//Set the  color equal to WHITE
                        for(int i=0;i<=35;i++)//this loop will open the doors
                        {try
                                    {
                                    Thread.sleep(100);
                                    g.drawLine(424,211,500-i,211+i);
                                    g.drawLine(424,509,500-i,509-i);
                                    g.drawLine(576,211,500+i,211+i);
                                    g.drawLine(576,509,500+i,509-i);
                                    g.drawLine(500-i,211,500-i,509);
                                    g.drawLine(500+i,211,500+i,509);
                                    }catch(Exception e)
                                    {}
                        }g.setColor(Color.WHITE);//Set the  color equal to WHITE
                        for(int i=0;i<=68;i++)//this loop will open the windows
                        {           try
                                    {
                                    Thread.sleep(100);
                                    g.drawLine(346-i,211,346-i,359);
                                    g.drawLine(654+i,211,654+i,359);
                                    }catch(Exception e)
                                    {}
                        }
                        g.setColor(Color.GREEN);//Set the  color equal to green
                        for(int i=0;i<=200;i++)//this loop will print the green road
                        {
                                    try        {
                                    Thread.sleep(25);
                                    g.drawLine(424-i,511+i,576-i,511+i);
                                    }catch(Exception e)
                                    {}
                        }Color c=new Color(128,0,0);//create new object of color class with color brown
                        g.setColor(c);//Set the  color equal to brown
                        for(int i=0;i<=605;i++)//this loop will print the brown carpet
                        {
                                    try
                                    {
                                    Thread.sleep(25);
                                    g.drawLine(576+i,510,376+i,711);
                                    g.drawLine(424-i,510,224-i,711);
                                   
                                    }
                                    catch(Exception e)
                                    {}
                        }}}
OUTPUT:-

4) Chart Generator (Bar & Line Chart)

Code:
            import java.awt.*;
           import java.applet.*;
           public class Charter extends Applet
           {
                      TextField fycm,sycm,tycm;
                      public void init()
{
fycm=new TextField(2);
sycm=new TextField(2);
tycm=new TextField(2);
add(fycm);
add(sycm);
add(tycm);
}
public void paint(Graphics g)
{
g.drawString("FYCM",440,40);
g.drawString("SYCM",485,40);
g.drawString("TYCM",525,40);
g.drawString("Enter the Percentage of Students Passed in respected Year",90,20);
int fy=Integer.parseInt(fycm.getText());
int sy=Integer.parseInt(sycm.getText());
int ty=Integer.parseInt(tycm.getText());
//Bar Chart
g.setFont(new Font("Times New Roman",Font.BOLD,24));
g.drawLine(70,375,70,175);
g.drawLine(70,375,300,375);
g.drawString("X",70,170);
g.drawString("Y",300,375);
g.drawString("Bar Chart",100,180);
g.drawString("FY",20,230);
g.drawString("SY",20,280);
g.drawString("TY",20,330);
g.setColor(Color.RED);
g.fillRect(70,210,fy*2,40);
g.setColor(Color.YELLOW);
g.fillRect(70,260,sy*2,40);
g.setColor(Color.BLUE);
g.fillRect(70,310,ty*2,40);
//Line chart
g.setColor(Color.BLACK);
g.drawString("Line Chart",750,140);
g.drawLine(700,360,700,160);
g.drawLine(700,360,900,360);
g.drawString("X",700,155);
g.drawString("Y",900,360);
g.drawString("FY",740,380);
g.drawString("SY",790,380);
g.drawString("TY",840,380);
g.setColor(Color.RED);
g.drawLine(700,360,750,360-(fy*2));
g.drawLine(750,360-(fy*2),800,360-(sy*2));
g.drawLine(800,360-(sy*2),850,360-(ty*2));
g.setColor(Color.YELLOW);
g.drawLine(750,360-(fy*2),750,360);
g.drawLine(800,360-(sy*2),800,360);
g.drawLine(850,360-(ty*2),850,360);
}
}

Output:

5) To generate register from given values and calculate it’s Resistance.



Code:
           
import java.awt.*;
import java.applet.*;
public class Colorcodeapplet1 extends Applet
{
           TextField text1,text2,text3,text4;
           public void init()
{
text1=new TextField(2);
text2=new TextField(2);
text3=new TextField(2);
text4=new TextField(2);
add(text1);
add(text2);
add(text3);
add(text4);
}
public void paint(Graphics g)
{
int i=4;
double result;
g.drawString("\n Black=0\n Brown=1\n Red=2\n Orange=3\n Yellow=4\n Green=5\n Blue=6\n Violet=7\n Gray=8\n White=9\n Silver=10\n Gold=11\n No_color=12",10*i,10*i);
int cc1=Integer.parseInt(text1.getText());
int cc2=Integer.parseInt(text2.getText());
int cc3=Integer.parseInt(text3.getText());
int cc4=Integer.parseInt(text4.getText());
result=cc1;
result=result+(cc2*10);
switch(cc3)
{
case 0:
result*=1;
break;
case 1:
result*=10;
break;
case 2:
result*=100;
break;
case 3:
result*=1000;
break;
case 4:
result*=(10^4);
break;
case 5:
result*=(10^5);
break;
case 6:
result*=(10^6);
break;
case 7:
result*=(10^7);
break;
case 8:
result*=(10^8);
break;
case 9:
result*=(10^9);
break;
}
switch(cc4)
{
case 10:
String s1="Result="+(result)+"ohms="+(result/1000)+"kiloohms - 10%\n\n";
g.drawString(s1,30*i,20*i);
break;
case 11:
s1="Result="+(result)+"ohms="+(result/1000)+"kiloohms - 5%\n\n";
g.drawString(s1,30*i,20*i);
break;
case 12:
s1="Result="+(result)+"ohms="+(result/1000)+"kiloohms - 20%\n\n";
g.drawString(s1,30*i,20*i);
break;
}
g.drawRect(25*i,25*i,5*i,15*i);

switch(cc1)
{
case 0:
g.setColor(Color.BLACK);
break;
case 1:
Color c=new Color(128,0,0);
g.setColor(c);
case 2:
g.setColor(Color.RED);
break;
case 3:
g.setColor(Color.ORANGE);
break;
case 4:
g.setColor(Color.YELLOW);
break;
case 5:
g.setColor(Color.GREEN);
break;
case 6:
g.setColor(Color.BLUE);
break;
case 7:
Color c1=new Color(0,64,128);
g.setColor(c1);
case 8:
g.setColor(Color.GRAY);
break;
case 9:
g.setColor(Color.WHITE);
break;
case 10:
Color c4=new Color(192,192,192);
g.setColor(c4);
case 11:
Color c2=new Color(250,220,10);
g.setColor(c2);
case 12:
g.setColor(Color.WHITE);
break;
}
g.fillRect(20*i,40*i,15*i,20*i);
g.setColor(Color.BLACK);
g.drawRect(20*i,60*i,15*i,5*i);

switch(cc2)
{
case 0:
g.setColor(Color.BLACK);
break;
case 1:
Color c=new Color(128,0,0);
g.setColor(c);
break;
case 2:
g.setColor(Color.RED);
break;
case 3:
g.setColor(Color.ORANGE);
break;
case 4:
g.setColor(Color.YELLOW);
break;
case 5:
g.setColor(Color.GREEN);
break;
case 6:
g.setColor(Color.BLUE);
break;
case 7:
Color c1=new Color(0,64,128);
g.setColor(c1);
break;
case 8:
g.setColor(Color.GRAY);
break;
case 9:
g.setColor(Color.WHITE);
break;
case 10:
Color c4=new Color(192,192,192);
g.setColor(c4);
break;
case 11:
Color c2=new Color(250,220,10);
g.setColor(c2);
break;
case 12:
g.setColor(Color.WHITE);
break;
}

g.fillRect(20*i,65*i,15*i,20*i);
                                 g.setColor(Color.BLACK);
                                 g.drawRect(20*i,85*i,15*i,5*i);
                                
                      switch(cc3)
{
case 0:
g.setColor(Color.BLACK);
break;
case 1:Color c=new Color(128,0,0);
g.setColor(c);
break;
case 2:
g.setColor(Color.RED);
break;
case 3:
g.setColor(Color.ORANGE);
break;
case 4:
g.setColor(Color.YELLOW);
break;
case 5:
g.setColor(Color.GREEN);
break;
case 6:
g.setColor(Color.BLUE);
break;
case 7:
Color c1=new Color(0,64,128);
g.setColor(c1);
break;
case 8:
g.setColor(Color.GRAY);
break;
case 9:
g.setColor(Color.WHITE);
break;
case 10:
Color c4=new Color(192,192,192);
g.setColor(c4);
break;
case 11:
Color c2=new Color(250,220,10);
g.setColor(c2);
break;
case 12:
g.setColor(Color.WHITE);
break;
}
g.fillRect(20*i,90*i,15*i,20*i);
g.setColor(Color.BLACK);
g.drawRect(20*i,110*i,15*i,5*i);

switch(cc4)
{
case 0:
g.setColor(Color.BLACK);
break;
case 1:
Color c=new Color(128,0,0);
g.setColor(c);
break;
case 2:
g.setColor(Color.RED);
break;
case 3:
g.setColor(Color.ORANGE);
break;
case 4:
g.setColor(Color.YELLOW);
break;
case 5:
g.setColor(Color.GREEN);
break;
case 6:
g.setColor(Color.BLUE);
break;
case 7:
Color c1=new Color(0,64,128);
g.setColor(c1);
break;
case 8:
g.setColor(Color.GRAY);
break;
case 9:
g.setColor(Color.WHITE);
break;
case 10:
Color c4=new Color(192,192,192);
g.setColor(c4);
break;
case 11:
Color c2=new Color(250,220,10);
g.setColor(c2);
break;
case 12:
g.setColor(Color.WHITE);
break;
}
g.fillRect(20*i,115*i,15*i,20*i);
g.setColor(Color.BLACK);
g.drawRect(25*i,135*i,5*i,15*i);
}
public boolean action(Event event,Object object)
{
repaint();
return true;
}

}

Output:



6)To generate program for implementing different sorting techniques.





Code:
           
import java.util.*;
import java.io.*;
class Sort
{
           BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
           Vector  v1=new  Vector(5,2);
           Vector  v2=new  Vector(5,2);
Vector  v3=new  Vector(5,2);
int n;
void input()throws IOException
{
System.out.println("Enter the number of elements to be sorted:");
String s=b.readLine();
n=Integer.parseInt(s);
System.out.println("Enter the list to be sorted");
for(int i=0;i
{
Integer  a=Integer.parseInt(b.readLine());
v1.addElement(a);
}
}
void inputm()throws IOException
{
System.out.println("Enter the number of elements to be sorted:");
String s=b.readLine();
n=Integer.parseInt(s);
System.out.println("Enter the 1st list to be sorted");
for(int i=0;i
{
Integer a=Integer.valueOf(b.readLine());
v2.addElement(a);
}
System.out.println("Enter the 2nd list to be sorted");
for(int i=0;i
{
Integer a=Integer.valueOf(b.readLine());
v3.addElement(a);
}
}
void selection()
{
int i,j;
for( i=0;i
{
for(j=i+1;j
{
Integer i1=(Integer)v1.elementAt(i);
Integer i2=(Integer)v1.elementAt(j);
int a=i1.compareTo(i2);
if(a==1)
{
Integer temp=(Integer)v1.elementAt(i);
v1.setElementAt(v1.elementAt(j),i);
v1.setElementAt(temp,j);
}
}
}
}
void bubble()throws IndexOutOfBoundsException
{
int i,j;
for( i=0;i
{
for(j=0;j<(n-(i+1));j++)
{
int x=j+1;
Integer i3=(Integer)v1.elementAt(j);
Integer i4=(Integer)v1.elementAt(j+1);
int a=i3.compareTo(i4);
if(a==1)
{
Integer temp=(Integer)v1.elementAt(j);
v1.setElementAt(v1.elementAt(j+1),j);
v1.setElementAt(temp,j+1);
}
}
}
}
void insertion()throws IndexOutOfBoundsException
{
int i,j,k;
for( i=1;i
{
for(j=0;j
{
Integer i3=(Integer)v1.elementAt(i);
Integer i4=(Integer)v1.elementAt(j);
int a=i4.compareTo(i3);
if(a==1)
{
Integer temp=(Integer)v1.elementAt(j);
v1.setElementAt(v1.elementAt(i),j);
for(k=i;k>j;k--)
v1.setElementAt(v1.elementAt(k-1),k);
v1.setElementAt(temp,k+1);
}
}
}
}
void merge()throws IOException
{
int i,j,k;
for( i=0;i
{
for(j=i+1;j
{
Integer  i3=(Integer)v2.elementAt(j);
Integer  i4=(Integer)v2.elementAt(j);
int a=i3.compareTo(i4);
if(a==1)
{
Integer temp1=(Integer)v2.elementAt(i);
v2.setElementAt(v2.elementAt(j),i);
v2.setElementAt(temp1,j);
}
Integer i5=(Integer)v3.elementAt(j);
Integer i6=(Integer)v3.elementAt(j);
int a1=i5.compareTo(i6);
if(a1==1)
{
Integer temp=(Integer)v3.elementAt(i);
v3.setElementAt(v1.elementAt(j),i);
v3.setElementAt(temp,j);
}
}
}
System.out.println(n);
for(i=j=k=0;i
{
Integer i7=(Integer)v2.elementAt(j);
Integer i8=(Integer)v3.elementAt(k);
int b=i7.compareTo(i8);
if(b==-1 || b==0)
{
v1.insertElementAt(v2.elementAt(j),i);
i++;
j++;
}
else
{
v1.insertElementAt(v3.elementAt(k),i);
k++;
i++;
}
if(j==n/2 || k==n/2)
break;
}
for(;j
{
v1.insertElementAt(v2.elementAt(j),i);
i++;
j++;
}
for(;k
{
v1.insertElementAt(v3.elementAt(k),i);
i++;
k++;
}
}
void display()
{
   System.out.println("The sorted elements are:");
Enumeration e=v1.elements();
while(e.hasMoreElements())
{
  System.out.println(e.nextElement());
}
v1.removeAllElements();
}
}

class SORTINGTECHNICS
{
           public static void main (String args[])throws Exception
           {
           BufferedReader b1=new BufferedReader(new InputStreamReader(System.in));
System.out.println("***************************WELCOME TO SORTING**************************");
Sort s1=new Sort();
char ch1;
do
{
System.out.println("ENTER YOUR CHOICE");
System.out.println("1.BUBBLE SORT");
System.out.println("2.SELECTON SORT");
System.out.println("3.INSERTION SORT");
System.out.println("4.MERGE SORT");
String c=b1.readLine();
int ch=Integer.parseInt(c);
switch(ch)
{
case 1:
s1.input();
s1.bubble();
s1.display();
break;
case 2:
s1.input();
s1.selection();
s1.display();
break;
case 3:
s1.input();
s1.insertion();
s1.display();
break;
case 4:
s1.inputm();
s1.merge();
s1.display();
break;
default:
System.out.println("PLEASE ENTER CORRECT CHOICE");
}
System.out.println("DO YOU WANT TO CONTINUE?(Y/N)");
ch1=b1.readLine().charAt(0);
}
while(ch1=='y' || ch1=='Y');
}
}

Output:

***************************WELCOME TO SORTING**************************
ENTER YOUR CHOICE
1.BUBBLE SORT
2.SELECTON SORT
3.INSERTION SORT
4.MERGE SORT
1
Enter the number of elements to be sorted:
5
Enter the list to be sorted
10
5
2
70
58
The sorted elements are:
2
5
10
58
70
DO YOU WANT TO CONTINUE?(Y/N)
y
ENTER YOUR CHOICE
1.BUBBLE SORT
2.SELECTON SORT
3.INSERTION SORT
4.MERGE SORT
2
Enter the number of elements to be sorted:
5
Enter the list to be sorted
7
6
32
52
5
The sorted elements are:
5
6
7
32
52
DO YOU WANT TO CONTINUE?(Y/N)
y
ENTER YOUR CHOICE
1.BUBBLE SORT
2.SELECTON SORT
3.INSERTION SORT
4.MERGE SORT
3
Enter the number of elements to be sorted:
5
Enter the list to be sorted
8
6
6
12
23
The sorted elements are:
6
6
8
12
23
DO YOU WANT TO CONTINUE?(Y/N)
n




No comments:

Post a Comment