Thursday, December 13, 2012

Java Swing - Finding diameter and circumfernce a circle


  
               The program calculates the diameter ( by multiplying the radius by 2), and then calculates the circumference (by multiplying the diameter by 3.14). The program prints both the diameter and the circumference.


Java code:


import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class I_C_2 
{
    public static void main (String args[])
    
    {
        // Declaring  variables
        String radius_strg;
        double radius,diameter, cirm;
        double pi = 3.14;
        DecimalFormat formatter = new DecimalFormat("#.00");
        
        // Prompt user to input by dialogbox
        radius_strg = JOptionPane.showInputDialog(null,"Enter Radius: ");
        
        // Convert string value into double
        radius = Double.parseDouble(radius_strg);
        
        // Compile diameter
        diameter = radius * 2;
        
        // Compile circumfernce
        cirm = diameter * pi;
        
        // Output : Displaying the result diameter and circumfernce in Message Dialogbox
        JOptionPane.showMessageDialog(null," The Diameter is " + formatter.format(diameter) + "\n\r" + "The Circumfence is " + formatter.format(cirm));
        
        } // end of the main clause
     } // end of the program















1 comment: