Sunday 15 May 2016

Java code to check whether a number is power of two


import java.util.Scanner;

public class CheckPowerTwo {
   
     
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a,i;
    System.out.println("Enter an integer");
    a=ob.nextInt();
   
    for( i=0;i<a;i++)
    {
    if(a==Math.pow(2,i))
    { System.out.println("The number is power of two");break;
    }
   
    }
    if(i==a)
        System.out.println("The number is not power of two");
   
   
    }
   
}

Reading a file using FileInputStream in java

import java.io.FileInputStream;

public class ReadFile {
   
   
   
    public static void main(String args[])
        
           
    {  FileInputStream fin ;
    try{
  
    fin= new FileInputStream("C:\\Users\\user\\Desktop\\ash.txt");
    int c;
    while((c=fin.read())!=-1)
    {
       
       
        System.out.print((char)c);
    }
   
    }
    catch(Exception e){}
   
    } 
   
}

Student database GUI using java which adds the records to a text file


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class StudentDatabase extends JFrame{
   static FileOutputStream fou;

  
   
    class Handler   implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
           String s0 = "\r\n";
             byte h0[]=s0.getBytes();
           String s="    ";
           byte h[]=s.getBytes();
            String s1= t1.getText();
            byte h1[]=s1.getBytes();
            String s2= t2.getText();
            byte h2[]=s2.getBytes();
            String s3= t3.getText();
            byte h3[]=s3.getBytes();
            String s4= t4.getText();
            byte h4[]=s4.getBytes();
            String s5= t5.getText();
            byte h5[]=s5.getBytes();

   try{
fou.write(h1);
fou.write(h);
fou.write(h2);
fou.write(h);
fou.write(h3);
fou.write(h);
fou.write(h4);
fou.write(h);
fou.write(h5);
fou.write(h0);
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
JOptionPane j= new JOptionPane();
     j.showMessageDialog(rootPane, "RECORD ADDED");

   }
   catch(Exception ee)
   {}
         
        }
       }
     JTextField t1;
   JTextField t2;
    JTextField t3;
     JTextField t4;
      JTextField t5;
      JButton b;
    StudentDatabase()
    {b= new JButton("ADD");
    Handler k = new Handler();
    b.setBounds(50, 250, 70, 30);
    b.addActionListener(k);
          JLabel j1= new JLabel("STUDENT DATABASE");
    j1.setBounds(90, 30,140, 20);
    t1= new JTextField();
    t1.setBounds(200,50,150,20);
    JLabel j2= new JLabel("STUDENT NAME");
    j2.setBounds(50, 50,150, 20);
    t2= new JTextField();
    t2.setBounds(200,80,150,20);
       JLabel j3= new JLabel("STUDENT ROLL. NO.");
    j3.setBounds(50, 80,150, 20);
     JLabel j4= new JLabel("STUDENT BRANCH");
    j4.setBounds(50, 110,150, 20);
    t3= new JTextField();
    t3.setBounds(200,110,150,20);
     JLabel j5= new JLabel("DOB");
    j5.setBounds(50, 140,150, 20);
    t4= new JTextField();
    t4.setBounds(200,140,150,20);
        JLabel j6= new JLabel("STUDENT SECTION");
    j6.setBounds(50, 170,150, 20);
    t5= new JTextField();
    t5.setBounds(200,170,150,20);
    add(j1);
    add(t1);
    add(j2);
    add(t2);
    add (b);
add(t3);
add(t4);
add(t5);
    add(j3);
       add(j4);
          add(j5);
             add(j6);
            
        setSize(400, 400);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setVisible(true);
   
    }
      public static void main(String args[])
         {  new   StudentDatabase();
 
    try{ fou= new FileOutputStream("C:\\Users\\user\\Desktop\\aa.txt");}
    catch(Exception ee)
    {}
   
}
}

-------------------------------------------------------------

import java.io.FileInputStream;

public class ReadFile {
   
   
   
    public static void main(String args[])
        
           
    {  FileInputStream fin ;
    try{
  
    fin= new FileInputStream("C:\\Users\\user\\Desktop\\ash.txt");
    int c;
    while((c=fin.read())!=-1)
    {
       
       
        System.out.print((char)c);
    }
   
    }
    catch(Exception e){}
   
    } 
   
}

Saturday 14 May 2016

GUI for weight conversion in java


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class WeightConv extends JFrame {
   
    class Handler implements ActionListener{

     
        public void actionPerformed(ActionEvent e) {
           
          String s= a1.getText();
          double d= Integer.parseInt(s);
              if(kg1.isSelected()&&kg2.isSelected())
              { a2.setText(s);
              }
       if(kg1.isSelected()&&pound2.isSelected())
       {   double pou= d*2.205;
         a2.setText( Double.toString(pou));
      
      
      
       }
            if(kg1.isSelected()&&ounce2.isSelected())
            { double ounce=d*35.274;
             a2.setText( Double.toString(ounce));}
           
            if(pound1.isSelected()&&kg2.isSelected())
            {  double kgs= d/2.205;
             a2.setText( Double.toString(kgs));}
              if(pound1.isSelected()&&pound2.isSelected())  
            a2.setText(s);
             
               if(pound1.isSelected()&&ounce2.isSelected())
               {double ounces=d/.0625;
                a2.setText( Double.toString(ounces));
               }
              if(ounce1.isSelected()&&kg2.isSelected())
              { double kgs2=d/35.274;
               a2.setText( Double.toString(kgs2));}
               if(ounce1.isSelected()&&pound2.isSelected())
             
               {double pounds2= d*.0625;
                a2.setText( Double.toString(pounds2));}
                if(ounce1.isSelected()&&ounce2.isSelected())
                 a2.setText(s);
           
        }
   
   
   
    }
   
   
   
JButton b;
JTextArea a2;
JTextField a1;
JCheckBox kg1;
  JCheckBox kg2;
    JCheckBox pound1;
     JCheckBox pound2;
     JCheckBox ounce1;
      JCheckBox ounce2;
    WeightConv()
    {Handler h= new Handler();
       
        JLabel l= new JLabel( "WEIGHT CONVERTER");
       l.setBounds(120, 0, 180, 20);
       add(l);
        JLabel l2= new JLabel( "INPUT UNIT");
       l2.setBounds(56, 65, 100, 10);
       add(l2);
       JLabel l3= new JLabel( "OUTPUT UNIT");
       l3.setBounds(230, 65, 100, 10);
       add(l3);
        b= new JButton("Convert");
      b.setBounds(120, 260, 100, 50);
      a1= new JTextField();
      a1.setBounds(120, 20, 100, 30);
      a2=new JTextArea();
      a2.setBounds(120, 310, 100, 40);
    kg1= new JCheckBox("KG");
    kg1.setBounds(50,110, 140, 20);
      kg2= new JCheckBox("KG");
       kg2.setBounds(230, 110, 140, 20);
        pound1= new JCheckBox("Pounds");
         pound1.setBounds(50, 160, 100,20);
          pound2= new JCheckBox("Pounds");
           pound2.setBounds(230, 160, 100,20);
            ounce1= new JCheckBox("Ounce");
            ounce1.setBounds(50, 210, 100,20);
              ounce2= new JCheckBox("Ounce");
                ounce2.setBounds(230, 210, 100,20);
              add(b);
              add(kg1);
              add(kg2);
              add(a1);
              add(a2);
              add(pound1);
              add(pound2);
              add(ounce1);
              add(ounce2);
              b.addActionListener(h);
             
             setSize(400, 400);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setVisible(true);
   
    }
   
   
   
   
    public static void main(String args[])
    { WeightConv obj= new WeightConv();}
   
}

Java code to check given number is even or odd


import java.util.Scanner;

public class CheckEven {
   
   
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a;
    System.out.println("Enter an integer");
    a=ob.nextInt();
    if(a%2==0)
        System.out.println("The Entered number is even");
    else
     System.out.println("The Entered number is odd");  
    }
   
   
}

Java code to check number is palindrome or not.

import java.util.Scanner;

public class CheckPalindrome {
   
      public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
   double k;int a,count=0;
   double r=0;
    System.out.println("Enter an integer");
    a=ob.nextInt();
    int b=a;
    while(b!=0)  //loop to count number of digits in a
    {b=b/10;
    count++;
    }
   b=a;
  for(int i=count;i>0;i--)
  {k=b%10;
  k*= Math.pow(10,i-1);
  r+=k;
  b=b/10;
  }
    
    if(a==r)
         System.out.println("The number is a palindrome");
    else
         System.out.println("The number is not a palindrome");
    }
   
   
}

Java code to check whether number is prime or composite

import java.util.Scanner;
public class CheckPrime {
   
 
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a,i;
    System.out.println("Enter an integer");
    a=ob.nextInt();
    if(a==1)
        System.out.println("Number is prime");
    for(i=2;i<a;i++)
    {
     if(a%i==0) 
     {System.out.println("Number is composite");
     break;}
   
    }
    if(i==a)
        System.out.println("Number is prime");
   

}}