Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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");
   

}}