Calculate a Circle's Area, Circumference and Diameter

Calculate a Circle's Area, Circumference and Diameter

Page 1 of 1

2 Replies - 18824 Views - Last Post: 01 June 2008 - 03:29 PM Rate Topic: -----

#1 taliskerskye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-June 08

Calculate a Circle's Area, Circumference and Diameter

Posted 01 June 2008 - 09:56 AM

I am having issues with the get text portion. I've used this before in other calculators but don't understand why this is such a problem. I need to receive the user's input for a radius, validate it then display the results for area, circumference and diameter. Any help would be good. Thank you!

/*
 * circles.java
 *
 * Created on May 31, 2008, 10:11 PM
 */

package circles;

/**
 *
 * @author  Charissa
 */
public class circles extends javax.swing.JFrame {
    
        float PI = (float) 3.14159;
        float fltDiameter;
        float fltArea;
        float fltCircumference;
        int tempRadius;
        
          
    /** Creates new form circles */
    public circles() {
        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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        radiusTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Radius");

        radiusTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiusTextFieldActionPerformed(evt);
            }
        });

        jButton1.setText("Submit");

        jButton2.setText("Clear");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Exit");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("Circumference");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Area");

        jLabel4.setText("Diameter");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Circle Calculator");

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Please Enter the Radius");

        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(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(75, 75, 75)
                        .addComponent(radiusTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(18, 18, 18)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jButton3))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(radiusTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(64, 64, 64))
        );

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

    private void radiusTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
        // Display the circle's diameter, circumference, and area for the user
        //Parse degrees Fahrenheit as a double and convert to Celsius.
        
        int tempRadius = (int)((double.parseDouble)(radiusTextField.getText());      
        
        fltCircumference = (2* PI * tempRadius);
        jLabel2.setText(fltCircumference  + "Circumference is:                    ");
        
        fltDiameter = (2 * tempRadius); 
        jLabel3.setText (fltDiameter + "Diameter is:                              ");
        
        fltArea = (PI * (tempRadius * tempRadius));
        jLabel4.setText (fltArea + "Area is:                                      ");     
}

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // Clears the input field
         radiusTextField.setText("0");
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        // Exits the Program
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new circles().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField radiusTextField;
    // End of variables declaration
    
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 01 June 2008 - 10:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate a Circle's Area, Circumference and Diameter

#2 cutegrrl  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 77
  • Joined: 12-May 08

Re: Calculate a Circle's Area, Circumference and Diameter

Posted 01 June 2008 - 10:59 AM

Add this to your initComponents() method (you forgot to set up an actionListener for your submit button):

         jButton1.setText("Submit");
         jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });       



And replace your radiusTextFieldActionPerformed() method with

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        tempRadius = (int)Double.parseDouble(radiusTextField.getText()); 
        
        // Truncate decimals
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        
        jLabel2.setText("Circumference:    " + df.format((2* Math.PI * tempRadius)));
        jLabel3.setText("Diameter is:           " + df.format((2 * tempRadius)));
        jLabel4.setText("Area is:                    " + df.format((Math.PI * (tempRadius * tempRadius))));
    }



Also remove any other code related to the radiusTextField actionListener.

This post has been edited by cutegrrl: 01 June 2008 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculate a Circle's Area, Circumference and Diameter

Posted 01 June 2008 - 03:29 PM

Use Math.PI you'll have the better precison the system can provide you with
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1