Created
March 27, 2012 19:39
-
-
Save nitschmann/2219596 to your computer and use it in GitHub Desktop.
Heron-Verfahren
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| /* | |
| * Interface.java | |
| * | |
| * Created on 27.03.2012, 21:01:59 | |
| */ | |
| package heronverfahren; | |
| import java.math.*; | |
| /** | |
| * | |
| * @author Florian Nitschmann | |
| */ | |
| public class Interface extends javax.swing.JFrame { | |
| /** Creates new form Interface */ | |
| public Interface() { | |
| 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() { | |
| jLabel1 = new javax.swing.JLabel(); | |
| numInput = new javax.swing.JTextField(); | |
| jButton1 = new javax.swing.JButton(); | |
| jLabel2 = new javax.swing.JLabel(); | |
| resultField = new javax.swing.JTextField(); | |
| setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
| setTitle("Heron-Verfahren"); | |
| setResizable(false); | |
| jLabel1.setText("Zahl:"); | |
| numInput.addActionListener(new java.awt.event.ActionListener() { | |
| public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| numInputActionPerformed(evt); | |
| } | |
| }); | |
| jButton1.setText("Ausrechnen"); | |
| jButton1.addActionListener(new java.awt.event.ActionListener() { | |
| public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| jButton1ActionPerformed(evt); | |
| } | |
| }); | |
| jLabel2.setText("Ergebnis:"); | |
| resultField.setEditable(false); | |
| javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
| getContentPane().setLayout(layout); | |
| layout.setHorizontalGroup( | |
| layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGroup(layout.createSequentialGroup() | |
| .addContainerGap() | |
| .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) | |
| .addGroup(layout.createSequentialGroup() | |
| .addComponent(jLabel1) | |
| .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| .addComponent(numInput, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)) | |
| .addGroup(layout.createSequentialGroup() | |
| .addComponent(jLabel2) | |
| .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| .addComponent(resultField, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE))) | |
| .addContainerGap()) | |
| ); | |
| layout.setVerticalGroup( | |
| layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGroup(layout.createSequentialGroup() | |
| .addContainerGap() | |
| .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
| .addComponent(jLabel1) | |
| .addComponent(numInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
| .addGap(18, 18, 18) | |
| .addComponent(jButton1) | |
| .addGap(18, 18, 18) | |
| .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
| .addComponent(jLabel2) | |
| .addComponent(resultField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
| .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
| ); | |
| pack(); | |
| }// </editor-fold> | |
| private void numInputActionPerformed(java.awt.event.ActionEvent evt) { | |
| // TODO add your handling code here: | |
| } | |
| private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { | |
| //Variablen | |
| Double a = Double.valueOf(numInput.getText()); | |
| int sqrt = (int)Math.sqrt(a); | |
| Double x = (double)sqrt; | |
| //Heron-Verfahren | |
| for(int i = 1; i <= sqrt; i++){ | |
| //x an die reale Quadratwurzel anähern | |
| x = (x+(a/x))/2; | |
| } | |
| //Ergebnis im Feld ausgeben | |
| resultField.setText(String.valueOf(x)); | |
| } | |
| /** | |
| * @param args the command line arguments | |
| */ | |
| public static void main(String args[]) { | |
| /* Set the Nimbus look and feel */ | |
| //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
| /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
| * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
| */ | |
| try { | |
| for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
| if ("Nimbus".equals(info.getName())) { | |
| javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
| break; | |
| } | |
| } | |
| } catch (ClassNotFoundException ex) { | |
| java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } catch (InstantiationException ex) { | |
| java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } catch (IllegalAccessException ex) { | |
| java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
| java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } | |
| //</editor-fold> | |
| /* Create and display the form */ | |
| java.awt.EventQueue.invokeLater(new Runnable() { | |
| public void run() { | |
| new Interface().setVisible(true); | |
| } | |
| }); | |
| } | |
| // Variables declaration - do not modify | |
| private javax.swing.JButton jButton1; | |
| private javax.swing.JLabel jLabel1; | |
| private javax.swing.JLabel jLabel2; | |
| private javax.swing.JTextField numInput; | |
| private javax.swing.JTextField resultField; | |
| // End of variables declaration | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment