Results 1 to 8 of 8
  1. #1
    isamu01 is offline Member
    Join Date
    May 2012
    Posts
    9
    Rep Power
    0

    Red face java bisection gui

    Yo there, Im trying to do a bisection method with a gui , Ive done the program for the comand line but Im unable to parse correctly the function
    I try to get the function to work from a JTextfield and pass it to the bisection loop but, javac says it does not recognize the symbol of fb(x), fb(a) and likewise I try usinf a clss fb based on a f_x separated class;
    As far as I know using the statement

    fb=Float.parseFloat(yf.getText());
    would do the trick but It seems not to work , thanks in advance
    ====================================
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.lang.*;
    
    class fb extends f_x
    { public double func (double x)
    	{ return x*x;
    	}
      }	
    
    
    public class SimpleFrame extends JFrame
    {
    private JButton button = new JButton("Process!");
    private JLabel label0 = new JLabel(" A");
    private JLabel label1 = new JLabel(" B");
    private JLabel label2 = new JLabel("Tolerance");
    private JLabel label3 = new JLabel("Function");
    JTextField ya = new JTextField(15);
    JTextField yb = new JTextField(15);
    JTextField ytol = new JTextField(15);
    JTextField yf = new JTextField(15);
    
    private JPanel background = new JPanel();
    
    
    public SimpleFrame()
    {
    	super("Método de bisección");
    	button.addActionListener( new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	   double a, b, x=0, tol, fb;
    	   a =Float.parseFloat(ya.getText());	
       	   b =Float.parseFloat(yb.getText());	
    	   tol =Float.parseFloat(ytol.getText());
    	   fb=Float.parseFloat(yf.getText());
    	   double dx = b-a;
    	   int k = 0;
    		while (Math.abs(dx) > tol && k<10 && fb(x)!=0 ) {
    			x = ((a+b)/2);
    		if ((fb(a)*fb(x)) < 0) {
    					b = x;
    					dx = b-a;
    				     }
    		else {
    			a = x;
    			dx = b-a;
    		      }
    		k++;
    		}
    	}	
    
    		
    	});
    
    	background.add(label0);
    	background.add(ya);	
    	background.add(label1);
    	background.add(yb);
    	background.add(label2);
    	background.add(ytol);
    	background.add(label3);
    	background.add(yf);
    	background.add(button);
    	getContentPane().add(background);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	pack();
    }
    }
    ===================
    Thanks again!!!!
    Last edited by isamu01; 05-07-2012 at 09:09 PM.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: java bisection gui

    In the future use code tags: BB Code List - Java Programming Forum

    fb(x), fb(a) and fb(b) refer to a function. Where is that function defined?

    Also, I think you should use a different name since fb is also a class.

  3. #3
    isamu01 is offline Member
    Join Date
    May 2012
    Posts
    9
    Rep Power
    0

    Default Re: java bisection gui

    Java Code:
    class fb extends f_x
    { public double func (double x)
    { return x*x;
    }
    }
    because I dont know how to pass it to the bisection loop I declare the function right here and explicit define it
    As I know p

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: java bisection gui

    Sorry for getting back so late - I was busy.

    You need to create an object of type fb in SimpleFrame, i.e. private fb fb1 = new fb();. Then, you can us it to access the function func(..), i.e. fb1.func(a).

    You'll want to put the class fb in a separate file name fb.java or make it an inner class. Also, you'll want to give it an access modifier, i.e. public.

    Is the class f_x defined?

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: java bisection gui

    Correction to last post. The fb class doesn't need to be put in a separate file, but only one of the classes in the file can be public (see Can a java file have more than one class? - Stack Overflow).

  6. #6
    isamu01 is offline Member
    Join Date
    May 2012
    Posts
    9
    Rep Power
    0

    Default Re: java bisection gui

    Thanks shall , sorry for the late reply im updating the code and posting rigth now!!!

  7. #7
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: java bisection gui

    Quote Originally Posted by isamu01 View Post
    Thanks shall , sorry for the late reply im updating the code and posting rigth now!!!
    No code was posted. Are you sure you posted the code?

  8. #8
    isamu01 is offline Member
    Join Date
    May 2012
    Posts
    9
    Rep Power
    0

    Default Re: java bisection gui

    Sorry not yet today , Friday late Ill be posting this stuff,

Similar Threads

  1. Bisection Method to find Square and Cube Roots
    By tecnicamente in forum New To Java
    Replies: 2
    Last Post: 05-07-2011, 02:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •