۱۳۸۹ مرداد ۱۳, چهارشنبه

محاسبه حجم

یک کلاس با دو متد برای محاسبه حجم مکعب و استوانه

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package chem10;

import javax.swing.JOptionPane;

/**
*
* @author kian
*/
public class Calculate {

public static void cubeVolumeCalculate() throws NumberFormatException,NullPointerException{
String t = JOptionPane.showInputDialog("طول را وارد کن", null);
String a = JOptionPane.showInputDialog("عرض را وارد کن", null);
String e = JOptionPane.showInputDialog("ارتفاع را وارد کن", null);
double T = Double.parseDouble(t);
double A = Double.parseDouble(a);
double E = Double.parseDouble(e);
double V = T * A * E;
JOptionPane.showMessageDialog(null, "حجم " + V);
}

public static void cilandrVolumeCalculate() throws NumberFormatException,NullPointerException{
String h = JOptionPane.showInputDialog("ارتفاع", null);
String g = JOptionPane.showInputDialog("قطر را وارد کن", null);

double H = Double.parseDouble(h);
double G = Double.parseDouble(g);

double V = 0.785 * G * G * H;
JOptionPane.showMessageDialog(null, "حجم " + V);

}
}


یک فریم با دو دکمه برای آن که دو متد کلاس بالا را صدا بزند

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* NewJFrame.java
*
* Created on Jun 28, 2010, 5:54:45 PM
*/
package chem10;

import javax.swing.JOptionPane;

/**
*
* @author kian
*/
public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
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")
// //GEN-BEGIN:initComponents
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("حجم مکعب");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("حجم استوانه");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(27, 27, 27))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(200, Short.MAX_VALUE))
);

pack();
}//
//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
jLabel1.setText("Cube Volume");
try {
Calculate.cubeVolumeCalculate();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Incorect value, try again with right data");
}catch (NullPointerException ex) {
JOptionPane.showMessageDialog(null, "Enter data");
}
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
Calculate.cilandrVolumeCalculate();
}//GEN-LAST:event_jButton2ActionPerformed

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

public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}

هیچ نظری موجود نیست: