square root code

package eanalysis;

 

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

 

/**

 * headline:   Analysis

 * Beschreibung:

 * Copyright:     Copyright (c) 2002

 * Organisation:  HAW

 * @author Marc Heiligenstein

 * @version 1.0

 */

 

 

public class Eanalysis extends Applet implements ActionListener{

 

  // declaring the button

  Button button1;

 

  // the single-line text-components

  TextField topTextField, startTextField,sumTextField;

 

  // for displaying text

  Label l,l2,steps, error,result;

 

  // making some color for the font and the button

  Color blue = new Color(25,0,240),red = new Color(150,0,50),green = new Color(0,100,100);

 

  // the text windows for the result and the difference

  TextArea tsteps, errort;

 

  // for each result and difference

  String s1 = "";

  String s2 ="";

 

  // to store the strings of each step

  StringBuffer b1 = new StringBuffer(); //for the result

  StringBuffer b2 = new StringBuffer(); //for the difference

 

 

  /**initialise the applet, making the layout*/

  public void init() {

 

        // difining the layout

        GridBagLayout gridbag = new GridBagLayout();

        GridBagConstraints c = new GridBagConstraints();

        setFont(new Font( "Helvetica" , Font.PLAIN, 12));

        setLayout(gridbag);

 

        // label basic number

        c.weighty = 0.25; // giving some space vertically

        c.weightx = 1.0// giving some space horizontally

        l = new Label( "Basic number : " );

        gridbag.setConstraints(l, c);

        add(l);

 

        // text line basic

        c.weighty =0.25; // giving some space vertically

        c.gridwidth = GridBagConstraints.REMAINDER; // the last component in the row

        topTextField = new TextField( "25" ,10); // initialising the text field with a value

        topTextField.setFont(new Font( "Monospaced" ,Font.PLAIN,12));

        gridbag.setConstraints(topTextField, c);

        add(topTextField);

 

        // lable start value addition

        c.gridwidth = 1; // setting to the default value

        l2 = new Label( "Startvalue of the addition and 1st divisor: " ); // the text of the label

        gridbag.setConstraints(l2, c);

        add(l2);

 

        // text field for the start value of the addition

        startTextField = new TextField( "7", 10 ); // initialising the text field with a value

        startTextField.setFont(new Font( "Monospaced" ,Font.PLAIN,12));

        c.gridwidth = GridBagConstraints.REMAINDER; // the last component in the row

        gridbag.setConstraints(startTextField, c);

        add(startTextField);

 

        // setting the button

        button1 = new Button( "GO" ); // initialising the button with the text

        button1.setBackground(blue); // giving the color to the button

        c.anchor = GridBagConstraints.CENTER; //setting the button to the center of its grid cell

        gridbag.setConstraints(button1, c);

        add(button1);

        // when the button is pressed the method button1_actionPerformed(e) is called

        button1.addActionListener(new java.awt.event.ActionListener(){

        public void actionPerformed(ActionEvent e) {

        button1_actionPerformed(e);

          }

         });

 

        // lable result

        c.gridwidth = 1; // setting to the default value

        steps = new Label( "Result for every step : " );

        gridbag.setConstraints(steps, c);

        add(steps);

 

        // lable difference

        c.gridwidth = GridBagConstraints.REMAINDER; // the last component in the row

        error = new Label( "Difference to the Java square root funktion : " );

        gridbag.setConstraints(error, c);

        add(error);

 

        // text area for showing the result

        c.gridwidth = 1; // setting to the default value

        tsteps = new TextArea(s1,5,35,1);

        tsteps.setEditable(false);

        tsteps.setForeground(blue);

        gridbag.setConstraints(tsteps, c);

        add(tsteps);

 

        // text area for showing the difference

        c.gridwidth = GridBagConstraints.REMAINDER; // the last component in the row

        errort = new TextArea(s2,5,35,1);

        errort.setEditable(false);

        errort.setForeground(red); // setting the color of the font

        gridbag.setConstraints(errort, c);

        add(errort);

 

        // label square root

        c.gridwidth = GridBagConstraints.REMAINDER; // the last component in the row

        result = new Label( "Result of the Java square root function : " );

        gridbag.setConstraints(result, c);

        add(result);

 

        // text field showing the result of the Java square root function

        sumTextField = new TextField(20);

        sumTextField.setEditable(false);

        sumTextField.setForeground(green);

        gridbag.setConstraints(sumTextField, c);

        add(sumTextField);

 

  }

 

// is needed because the actionPerformed()-method is an inner class

public void actionPerformed(ActionEvent l){}

 

 

// all this happens when the button is pressed

 

   void button1_actionPerformed(ActionEvent e) {

 

 

    double stat = Double.parseDouble(topTextField.getText()); //searching the square root from this number*

    double temp = Double.parseDouble(startTextField.getText()); // Start value of the addition and divisor*

   double wurzel = Math.sqrt(stat); //getting the square root from the Java square root funktion

 

    if(stat >= 0 && temp > 0){      // the function only works if the basic number and the start value of the addition is greater or equal to 0

    double bug = 0;                 // difference

    double result = 999999998;      // result

    double previus = 999999999;     //the previus result of the actual calculation

 

    // the calculation happens as long as the previus result is greater than the actual result

    for (int i=1; previus > result && result > 0.0; i++){

    previus = result;  //giving the actual result to the previus

    result = (temp + stat / temp)/ 2// calculating the result

    temp = result; // the actual result is the start value of the addition for the next step

 

     // if the previus result is greater then the actual result the actual result is written in the string

     if(previus > result){

 

     // counting the right way

     if(i ==1){

     s1 = new String( "\n" + i + "st step: " + Double.toString(result));}

 

     else{

     if(i == 2){

     s1 = new String( "\n" + i + "nd step: " + Double.toString(result));}

 

       else{

       if(i == 3){

       s1 = new String("\n" + i + "rd step: " + Double.toString(result));}

 

         else{

         s1 = new String("\n" + i + "th step: " + Double.toString(result));

 

         }

       }

     }

 

     // calculating the difference of the result of every step to the result of the Java square root function

 

     if(wurzel > 0.0){  // only when the chosen number is positive and greater then 0

      bug = result - wurzel; // calculating the difference for every step

      s2 = new String( "\n\t" + Double.toString(bug));

      }

 

     // when choosing the 0 the result is the difference

 

     else{

         s2 = new String("\n\t" + "result = difference");

       }

 

       // storing the strings after every step

 

       b1.append(s1);

       b2.append(s2);

     }

    }

 

 

  // after finishing the function the results are written in the text windows

 

   tsteps.setText((b1.toString())); // result

   errort.setText((b2.toString())); // difference

 

  // showing the result of the square root function of Java

   sumTextField.setText(" " + Double.toString(wurzel));

 

  // delete for the next time

   b1.delete(0,b1.length());

   b2.delete(0,b2.length());

  }

  // this is showing when one of the chosen numbers is negative

    else{

 

   s1 = new String( "\n Only >= 0 for the basic number" );

   tsteps.setText(s1);

   s2 = new String( "\n Only > 0 \n for the startvalue and divisor" );

   errort.setText(s2);

   sumTextField.setText( " New numbers, please" );

   }

 

  }

 

}