Welcome to Dream.In.Code
Become a Java Expert!

Join 149,780 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,669 people online right now. Registration is fast and FREE... Join Now!




applet that calculates the square root

 
Reply to this topicStart new topic

applet that calculates the square root, Applet

PennyBoki
8 Feb, 2007 - 02:24 PM
Post #1

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi all, I was writing an applet that calculates the square root
it is not finished yet cuz i have some(read MANY) problems.
At this moment I want the values of X1 and X2 to be displayed
in a messageDialog Box. Also I get
possible loss of precision warning when I compile.
help

CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Kvadratna extends java.applet.Applet implements ActionListener {
    
    
    
     Label aa = new Label("a:");
     Label bb = new Label("b:");
     Label cc = new Label("c:");
    
     TextField A = new TextField();
     TextField B = new TextField();
     TextField C = new TextField();
    
     Button otkaz = new Button("Reset");
     Button calc = new Button("Calculate");
    
     float X1, X2, a, b, c;
          
    public void init() {
        
        setLayout(new GridLayout(4,2));
        add(aa);
        add(A);
        add(bb);
        add(B);
        add(cc);
        add(C);
        add(otkaz);
        add(calc);
        
        a = Double.parseDouble(aa.getText());  //when I compile get here possible loss of precision
        b = Double.parseDouble(bb.getText());  //possible loss of precision
        c = Double.parseDouble(cc.getText());    //possible loss of precision
        
        X1 = ((-b)-Math.sqrt((b*b-4*a*c)))/(2*a);     //possible loss of precision
        X2 = ((-b)+Math.sqrt((b*b-4*a*c)))/(2*a);    //possible loss of precision
        
           calc.addActionListener(this);
    
       
        }
    public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog(this, "the numbers are: "+X1+" , "+X2);
        }
    

}


EDIT: modified title ~ jayman9
User is offlineProfile CardPM
+Quote Post

ericode
RE: Applet That Calculates The Square Root
8 Feb, 2007 - 04:17 PM
Post #2

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 89


Dream Kudos: 75
My Contributions
Your line: float X1, X2, a, b, c;

Should be changed to: double X1, X2, a, b, c;


Then there will be no loss of precision when you store double values in X1, X2, etc.


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet That Calculates The Square Root
10 Feb, 2007 - 04:34 AM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hell(o)
I did what ericode told me it helped about that issue.Thx.
But now I get different kind of problem(s).
JVM says:


QUOTE
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Kv.init(Kv.java:35)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-Kv.class" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)



this is the code:

CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Kv extends java.applet.Applet implements ActionListener {
    
    
    
     Label aa = new Label("a:");
     Label bb = new Label("b:");
     Label cc = new Label("c:");
    
     TextField A = new TextField();
     TextField B = new TextField();
     TextField C = new TextField();
    
     Button otkaz = new Button("Reset");
     Button calc = new Button("Calculate");
    
    double X1, X2, a, b, c;
          
    public void init() {
        
        setLayout(new GridLayout(4,2));
        add(aa);
        add(A);
        add(bb);
        add(B);
        add(cc);
        add(C);
        add(otkaz);
        add(calc);
        
        a = Double.parseDouble(A.getText());  //when I compile get here possible loss of precision
        b = Double.parseDouble(B.getText());  //possible loss of precision
        c = Double.parseDouble(C.getText());    //possible loss of precision
        
        X1 = ((-b)-Math.sqrt((b*b-4*a*c)))/(2*a);     //possible loss of precision
        X2 = ((-b)+Math.sqrt((b*b-4*a*c)))/(2*a);    //possible loss of precision
        
           calc.addActionListener(this);
    
       
        }
    public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog(this, "the numbers are: "+X1+" , "+X2);
        }
    

}


help.
User is offlineProfile CardPM
+Quote Post

ericode
RE: Applet That Calculates The Square Root
10 Feb, 2007 - 09:07 AM
Post #4

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 89


Dream Kudos: 75
My Contributions
The problem is that your text fields don't have any data in them yet when you try to calculate the roots. To make sure data is in them, move your root calculating portion of code into the actionPerformed method.

Here is your code, I just cut and pasted the numerical calculation part into the actionPerformed method:

CODE

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Kv extends java.applet.Applet implements ActionListener {



     Label aa = new Label("a:");
     Label bb = new Label("b:");
     Label cc = new Label("c:");

     TextField A = new TextField();
     TextField B = new TextField();
     TextField C = new TextField();

     Button otkaz = new Button("Reset");
     Button calc = new Button("Calculate");

    double X1, X2, a, b, c;

    public void init() {

        setLayout(new GridLayout(4,2));
        add(aa);
        add(A);
        add(bb);
        add(B);
        add(cc);
        add(C);
        add(otkaz);
        add(calc);


           calc.addActionListener(this);


        }
    public void actionPerformed(ActionEvent e)
        {
            a = Double.parseDouble(A.getText());  //when I compile get here possible loss of precision
            b = Double.parseDouble(B.getText());  //possible loss of precision
            c = Double.parseDouble(C.getText());    //possible loss of precision

            X1 = ((-b)-Math.sqrt((b*b-4*a*c)))/(2*a);     //possible loss of precision
            X2 = ((-b)+Math.sqrt((b*b-4*a*c)))/(2*a);    //possible loss of precision



            JOptionPane.showMessageDialog(this, "the numbers are: "+X1+" , "+X2);
        }


}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet That Calculates The Square Root
10 Feb, 2007 - 10:11 AM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
It works just fine now.
ericode for(;;) Thanks;
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet That Calculates The Square Root
10 Feb, 2007 - 12:28 PM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
SO, I did some Exception handling after reading
the tutorials by William_Wilson and got stuck.

I want to throw an Exception if any of the TextFields
are emty and notify the user to enter a number.

When I compile I get:

QUOTE
actionPerformed(java.awt.event.ActionEvent) in Kv cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; overridden method does not throw Kv.Isklucok


the code is:

CODE

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;
import java.lang.String;

public class Kv extends java.applet.Applet implements ActionListener {



    public class Isklucok extends Exception
        {
            public Isklucok()
            {
                super("Enter a number.");
            }
        }


     Label aa = new Label("a:");
     Label bb = new Label("b:");
     Label cc = new Label("c:");

     TextField A = new TextField();
     TextField B = new TextField();
     TextField C = new TextField();

     Button otkaz = new Button("Reset");
     Button calc = new Button("Calculate");

    double X1, X2, a, b, c, D;

    public void init() {

        setLayout(new GridLayout(4,2));
        add(aa);
        add(A);
        add(bb);
        add(B);
        add(cc);
        add(C);
        add(otkaz);
        add(calc);


           calc.addActionListener(this);


        }
    public void actionPerformed(ActionEvent e) throws Isklucok
        {
            
            if (((A.getText())=="")||((B.getText())=="")||((C.getText())=="")) throw new Isklucok();
            
            
            
            a = Double.parseDouble(A.getText());  
            b = Double.parseDouble(B.getText());  
            c = Double.parseDouble(C.getText());    
            
        
            
            D = (b*b-4*a*c);

            X1 = ((-b)-Math.sqrt(D))/(2*a);    
            X2 = ((-b)+Math.sqrt(D))/(2*a);    
            
            if(D>0)
            JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2);
            else if(D<0)
                JOptionPane.showMessageDialog(this, "the numbers are komplex");
            else
                JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2);
        }
        
        


}


Anybody?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet That Calculates The Square Root
10 Feb, 2007 - 04:05 PM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
OK, I modified the code but problems will be problems.
I don't have compiling issues but I still have issues with
Exception handling and with the RESET Button. The thing is that
RESET does the same thing as CALCULATE.
I know it has to do with that they use the same actionPerformed()
method but I don't know other way.


CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;
import java.lang.String;

public class Kv extends java.applet.Applet implements ActionListener {



    public class Isklucok extends Exception
        {
            public Isklucok()
            {
                super("Enter a number.");
            }
        }


     Label aa = new Label("a:");
     Label bb = new Label("b:");
     Label cc = new Label("c:");

     TextField A = new TextField();
     TextField B = new TextField();
     TextField C = new TextField();

     Button otkaz = new Button("Reset");
     Button calc = new Button("Calculate");

    double X1, X2, a, b, c, D;

    public void init() {

        setLayout(new GridLayout(4,2));
        add(aa);
        add(A);
        add(bb);
        add(B);
        add(cc);
        add(C);
        add(otkaz);
        add(calc);


        calc.addActionListener(this);
        otkaz.addActionListener(this);


        }
    public void actionPerformed(ActionEvent e)
        {
            
            try{
                if (((A.getText())=="")||((B.getText())=="")||((C.getText())=="")) throw new Isklucok();
            }
            catch(Exception ex)
            {JOptionPane.showMessageDialog(this,"Enter number.");}
            
            
            a = Double.parseDouble(A.getText());  
            b = Double.parseDouble(B.getText());  
            c = Double.parseDouble(C.getText());    
            
        
            
            D = (b*b-4*a*c);

            X1 = ((-b)-Math.sqrt(D))/(2*a);    
            X2 = ((-b)+Math.sqrt(D))/(2*a);    
            
            if(D>0)
            JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2);
            else if(D<0)
                JOptionPane.showMessageDialog(this, "the numbers are komplex");
            else
                JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2);
        }
}


I know this can't be right, but what other way?
CODE
calc.addActionListener(this);
        otkaz.addActionListener(this);


And here it simply does nothing
CODE
try{
                if (((A.getText())=="")||((B.getText())=="")||((C.getText())=="")) throw new Isklucok();
            }
            catch(Exception ex)
            {JOptionPane.showMessageDialog(this,"Enter number.");}


This post has been edited by PennyBoki: 10 Feb, 2007 - 04:06 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet That Calculates The Square Root
11 Feb, 2007 - 07:21 AM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Again anybody? I just can't figure this one out, Just a hint that's all???
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet That Calculates The Square Root
13 Feb, 2007 - 04:51 AM
Post #9

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
THIS WORKS TOO tongue.gif

FINAL CODE:

CODE

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;
import java.lang.String;

public class Kv extends java.applet.Applet implements ActionListener {



    public class Isklucok extends NumberFormatException
        {
            public Isklucok()
            {
                super("Enter a number.");
            }
        }


     Label aa = new Label("a:");
     Label bb = new Label("b:");
     Label cc = new Label("c:");

     TextField A = new TextField();
     TextField B = new TextField();
     TextField C = new TextField();

     Button otkaz = new Button("Reset");
     Button calc = new Button("Calculate");

    double X1, X2, a, b, c, D;

    public void init() {

        setLayout(new GridLayout(4,2));
        add(aa);
        add(A);
        add(bb);
        add(B);
        add(cc);
        add(C);
        add(otkaz);
        add(calc);

        calc.addActionListener(this);
        otkaz.addActionListener(this);
        
        
        }
        
        
            public void actionPerformed(ActionEvent e)
            {
                
                
                if(e.getSource()==calc)
                {
                    D = (b*b-4*a*c);

                X1 = ((-b)-Math.sqrt(D))/(2*a);    
                X2 = ((-b)+Math.sqrt(D))/(2*a);    
            
                if(D>0)
                JOptionPane.showMessageDialog(this, "the numbers are real: "+X1+" , "+X2);
                else if(D<0)
                JOptionPane.showMessageDialog(this, "the numbers are komplex");
                else
                JOptionPane.showMessageDialog(this, "the numbers are real and equal. "+X1+" , "+X2);
                }
                
                if(e.getSource()==otkaz)
                {    
                    X1=0.0;
                    X2=0.0;
                    A.setText("");
                    B.setText("");
                    C.setText("");
                }              
                       
       }
      
}


HTML CODE:

CODE
<html>
    <head>
    </head>
    <body bgcolor="000000">
        <center>
            <applet
                code    = "Kv.class"
                width    = "250"
                height    = "125"
                >
            </applet>
        </center>
    </body>
</html>

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month