# Calculate a Circle's Area, Circumference and Diameter

Page 1 of 1

## 2 Replies - 22382 Views - Last Post: 01 June 2008 - 03:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=53526&amp;s=9aa873cb129ad8b0429885c97cb20935&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 taliskerskye

Reputation: 0
• 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;

/** 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();
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);

public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

jButton1.setText("Submit");

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

jButton3.setText("Exit");
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);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
);
layout.setVerticalGroup(
);

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

// Display the circle's diameter, circumference, and area for the user
//Parse degrees Fahrenheit as a double and convert to Celsius.

fltCircumference = (2* PI * tempRadius);
jLabel2.setText(fltCircumference  + "Circumference is:                    ");

jLabel3.setText (fltDiameter + "Diameter is:                              ");

jLabel4.setText (fltArea + "Area is:                                      ");
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Clears the input field
}

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;
// End of variables declaration

}

```

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

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

Reputation: 10
• 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");
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

```

``` private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// 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)));
}

```

Also remove any other code related to the radiusTextField actionListener.

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

### #3 pbl

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

Reputation: 8378
• Posts: 31,956
• 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