Showing posts with label Temperature Conversion. Show all posts
Showing posts with label Temperature Conversion. Show all posts

Temperature Converter - Celsius-Fahrenheit - Java Swing - JSlider Example - NetBeans - Internet & Web Programming Lab


Java Swing - JSlider Example
Temperature Converter - (Celsius-Fahrenheit)
NetBeans - Internet & Web Programming Lab

Source code:

/*
 * sw5.java
 *
 * Created on 1 Oct, 2011, 10:04:07 PM
 */

package swingexamples;

/**
 *
 * @author gcecse
 */
public class sw5 extends javax.swing.JFrame {

    /** Creates new form sw5 */
    public sw5() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSlider1 = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jSlider2 = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSlider1.setMajorTickSpacing(5);
        jSlider1.setMinorTickSpacing(1);
        jSlider1.setOrientation(javax.swing.JSlider.VERTICAL);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setValue(0);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jSlider2.setMajorTickSpacing(5);
        jSlider2.setMaximum(250);
        jSlider2.setMinorTickSpacing(1);
        jSlider2.setOrientation(javax.swing.JSlider.VERTICAL);
        jSlider2.setPaintLabels(true);
        jSlider2.setPaintTicks(true);
        jSlider2.setValue(0);
        jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider2StateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(123, 123, 123)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSlider2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
                            .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
        // TODO add your handling code here:
       int c=0,f=0;
       if(!jSlider1.getValueIsAdjusting())
       {
         c=jSlider1.getValue();
         f=c*9/5+32;
         jLabel1.setText(Integer.toString(c)+" C");
         jSlider2.setValue(f);
         jLabel2.setText(Integer.toString(f)+" F");

       }    
      
    }                                     

    private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {                                      
        // TODO add your handling code here:
        int c=0,f=0;
       if(!jSlider2.getValueIsAdjusting())
       {
         f=jSlider2.getValue();
         c=(f-32)*5/9;
         jLabel2.setText(Integer.toString(f)+" F");
         jSlider1.setValue(c);
         jLabel1.setText(Integer.toString(c)+" C");

       }
    }                                     

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new sw5().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    // End of variables declaration

}

Output:

Temperature Converter - Checkbox- Java AWT - Internet & Web Programming Lab

Program:

import java.awt.*;
import java.awt.event.*;

class ex8 extends Frame implements ItemListener,ActionListener
{
 Label l1,l2;
 TextField t;
 Checkbox c1,c2;
 Button b;
 ex8()
 {
   super("Temperature Converter");
   setLayout(null);
   setSize(500,400);
   setVisible(true);
   l1=new Label();
   l2=new Label();
   t=new TextField();
   b=new Button("Clear");
   CheckboxGroup cg=new CheckboxGroup();
   c1=new Checkbox("Celsius to Fahrenheit",cg,true);
   c2=new Checkbox("Fahrenheit to Celsius",cg,false);
   l1.setBounds(50,100,200,20);
   add(l1);
   l2.setBounds(200,130,100,20);
   add(l2);
   t.setBounds(260,100,50,20);
   add(t);
   c1.setBounds(50,300,250,20);
   add(c1);
   c2.setBounds(50,330,250,20);
   add(c2);
   b.setBounds(200,250,50,20);
   add(b);
   c1.addItemListener(this);
   c2.addItemListener(this);
   b.addActionListener(this);
   addWindowListener(new WindowAdapter()
   {
    public void windowClosing(WindowEvent we)
    {
     System.exit(0);
    }
   });
 }

 public void itemStateChanged(ItemEvent ae)
 {
  float c,f;
  if(c1.getState())
  {
   l1.setText("Enter temperature in Celsius");
   if(t.getText()!=null)
   {
    c=Float.parseFloat(t.getText());
    f=(9/(float)5)*c+32;
    l2.setText(Float.toString(f)+"F");
   }
  }
   if(c2.getState())
   {
    l1.setText("Enter temperature in Fahrenheit");
    if(t.getText()!=null)
    {
     f=Float.parseFloat(t.getText());
     c=5/(float)9*(f-32);
     l2.setText(Float.toString(c)+"C");
    }
   } 
 }


 public void actionPerformed(ActionEvent ae)
 {
  t.setText(null);
  l2.setText(null);
  t.requestFocus();
 }

 public static void main(String s[])
 {
  ex8 ob=new ex8();
 }
}

Output:

nn@linuxmint ~/Desktop/java 7 $ javac ex8.java
nn@linuxmint ~/Desktop/java 7 $ java ex8
nn@linuxmint ~/Desktop/java 7 $


Sorting Using Template - C++

Program:

Download Runnable Code

#include<iostream>
using namespace std;
template<class t>
void sort(t a[],int n)
{
    int i,j;t tem;
    for(i=0;i<n;i++)
        for(j=0;j<n-i-1;j++)
            if(a[j]<a[j+1])
            {
                tem=a[j];
                a[j]=a[j+1];
                a[j+1]=tem;
            }
    cout<<"\n\t--------After Sorting---------\n\n";
    for(i=0;i<n;i++) 
        cout<<" "<<a[i];
}
               
int main()
{
    int i,n,a[25],ch;float b[25];bool exit=false;   
    cout<<"\n\t------SORTING USING TEMPLATE-----------\n\nEnter the number of elements: ";
    cin>>n;

    do{
        cout<<"\n\t--------MENU---------\n\n\t1.Integer Sorting\n\t2.Floating point Sorting\n\t3.Exit\n\nEnter your choice:";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\n\t------INT TYPE-----------\n\n";
                cout<<"\nEnter elements:\n";
                for(i=0;i<n;i++)
                    cin>>a[i];
                sort(a,n);
                break;
           
            case 2:
                cout<<"\n\t------FLOAT TYPE-----------\n\n";
                cout<<"\nEnter elements:\n";
                for(i=0;i<n;i++)
                    cin>>b[i];
                sort(b,n);
                break;
            case 3:
               
            default:
                cout<<"Exiting..... \n\t\t\t\t\t\t...www.2k8618.blogspot.com\n";
                exit=true;
        }
    }while(!exit);
return 0;   
}

Output:

nn@ubuntu:~$ ./a.out

    ------SORTING USING TEMPLATE-----------

Enter the number of elements: 5

    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:1

    ------INT TYPE-----------


Enter elements:
5
4
3
2
1

    --------After Sorting---------

 5 4 3 2 1
    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:2

    ------FLOAT TYPE-----------


Enter elements:
1.9
1.2
1.0
1.4
1.7

    --------After Sorting---------

 1.9 1.7 1.4 1.2 1
    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:3
Exiting.....
                                                                       ...www.2k8618.blogspot.com

Download

Temperature Conversion C to F & F to C - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int d,e=248;float a,b,c;
    //clrscr();
    printf("\n\t\t***Temperature conversion program***\n");
    n:
    printf("\n\tWhat u want to do?\n\t\t1:Convert %cC to %cF\n\t\t2:convert %cF to %cC\n\t\t",e,e,e,e);
    scanf("%d",&d);
    if(d==1)
    {
        printf("Enter the temp in %cc\n",e);
        scanf("%f",&a);
        b=(1.8*a)+32;
        printf("The temperature in %cF is:%f",e,b);
    }
    else if(d==2)
    {
        printf("Enter the temperature in %cF:\n",e);
        scanf("%f",&a);
        b=((0.5555555555555556)*(a-32));
        printf("The temperature in %cC is:%f",e,b);
    }
    else
    {
        printf("\n\tWrong choice!\n\tenter either 1 or 2\n\n\n");
        goto n;
    }
    //getch();
}

Output:
nn@linuxmint ~ $ gcc cc1.c
nn@linuxmint ~ $ ./a.out

        ***Temperature conversion program***

    What u want to do?
        1:Convert �C to �F
        2:convert �F to �C
        1
Enter the temp in �c
100
The temperature in �F is:212.000000nn@linuxmint ~ $ ./a.out

        ***Temperature conversion program***

    What u want to do?
        1:Convert �C to �F
        2:convert �F to �C
        3

    Wrong choice!
    enter either 1 or 2



    What u want to do?
        1:Convert �C to �F
        2:convert �F to �C
        2
Enter the temperature in �F:
212
The temperature in �C is:100.000000
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...