sqrtのタグがつけられたコード一覧

Java 平方根(Math.sqrtを使わない)

/**
 *
 */
package net.hinekure.test;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JTextField;

/**
 * @author ****
 *
 */
public class SampleProj_20120617 extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel_1 = null;
	private JLabel jLabel_2 = null;
	private JLabel jLabel_3 = null;
	private JButton jButton_1 = null;
	private JTextField jTextField_1 = null;
	private JTextField jTextField_2 = null;
	private JTextField jTextField_3 = null;

	/**
	 * This method initializes jButton_1
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton_1() {
		if (jButton_1 == null) {
			jButton_1 = new JButton();
			jButton_1.setBounds(new Rectangle(226, 119, 191, 90));
			jButton_1.setText("計算開始");
			jButton_1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					double a = Double.parseDouble(jTextField_1.getText());
					double epslion = Double.parseDouble(jTextField_2.getText());
					keisan_sqrt(a, epslion);
				}
			});
		}
		return jButton_1;
	}

	/**
	 * This method initializes jTextField_1
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_1() {
		if (jTextField_1 == null) {
			jTextField_1 = new JTextField();
			jTextField_1.setBounds(new Rectangle(14, 57, 192, 48));
		}
		return jTextField_1;
	}

	/**
	 * This method initializes jTextField_2
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_2() {
		if (jTextField_2 == null) {
			jTextField_2 = new JTextField();
			jTextField_2.setBounds(new Rectangle(225, 59, 193, 46));
		}
		return jTextField_2;
	}

	/**
	 * This method initializes jTextField_3
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_3() {
		if (jTextField_3 == null) {
			jTextField_3 = new JTextField();
			jTextField_3.setBounds(new Rectangle(14, 165, 188, 46));
		}
		return jTextField_3;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SampleProj_20120617 thisClass = new SampleProj_20120617();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public SampleProj_20120617() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setSize(460, 282);
		this.setContentPane(getJContentPane());
		this.setTitle("平方根の計算");
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel_3 = new JLabel();
			jLabel_3.setBounds(new Rectangle(16, 120, 189, 27));
			jLabel_3.setText("score root of a = ");
			jLabel_2 = new JLabel();
			jLabel_2.setBounds(new Rectangle(225, 16, 195, 29));
			jLabel_2.setText("epslion = ");
			jLabel_1 = new JLabel();
			jLabel_1.setBounds(new Rectangle(15, 16, 192, 29));
			jLabel_1.setText("a = ");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel_1, null);
			jContentPane.add(jLabel_2, null);
			jContentPane.add(jLabel_3, null);
			jContentPane.add(getJButton_1(), null);
			jContentPane.add(getJTextField_1(), null);
			jContentPane.add(getJTextField_2(), null);
			jContentPane.add(getJTextField_3(), null);
		}
		return jContentPane;
	}
	public double keisan_sqrt(double aa, double eepsilon){
		boolean flg = false;
		double x0 = aa;
		double x1 = (x0+(aa/x0))/2;
		double error = Math.abs((x1-x0)/x0);
		if (aa <= 0){
			if (aa < 0){
				flg = true;
			}else{
				jTextField_3.setText(Double.toString(aa));
			}
		}else{
			while (error >= eepsilon){
				x0 = x1;
				if (Math.abs(x0) <= 0){
					flg = true;
					break;
				}
				x1 = (x0+(aa/x0))/2;
				error = Math.abs((x1-x0)/0);
			}
		}
		if (flg){
			jTextField_3.setText("平方根がありません");
		}else{
			jTextField_3.setText(Double.toString(x0));
		}
		return x0;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"

よく投稿されているコード

タグ

最近投稿されたコード