8 Replies - 6537 Views - Last Post: 13 February 2007 - 05:51 AM Rate Topic: -----

#1 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

applet that calculates the square root

Posted 08 February 2007 - 03:24 PM

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

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

Is This A Good Question/Topic? 0
  • +

Replies To: applet that calculates the square root

#2 ericode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 112
  • Joined: 09-December 06

Re: applet that calculates the square root

Posted 08 February 2007 - 05:17 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: applet that calculates the square root

Posted 10 February 2007 - 05:34 AM

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:

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.
Was This Post Helpful? 0
  • +
  • -

#4 ericode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 112
  • Joined: 09-December 06

Re: applet that calculates the square root

Posted 10 February 2007 - 10:07 AM

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:

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);
		}


}


Was This Post Helpful? 0
  • +
  • -

#5 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: applet that calculates the square root

Posted 10 February 2007 - 11:11 AM

It works just fine now.
ericode for(;;) Thanks;
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: applet that calculates the square root

Posted 10 February 2007 - 01:28 PM

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:

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?
Was This Post Helpful? 0
  • +
  • -

#7 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: applet that calculates the square root

Posted 10 February 2007 - 05:05 PM

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.


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?
calc.addActionListener(this); 
		otkaz.addActionListener(this);


And here it simply does nothing
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 February 2007 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: applet that calculates the square root

Posted 11 February 2007 - 08:21 AM

Again anybody? I just can't figure this one out, Just a hint that's all???
Was This Post Helpful? 0
  • +
  • -

#9 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: applet that calculates the square root

Posted 13 February 2007 - 05:51 AM

THIS WORKS TOO :P

FINAL 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:

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1