Results 1 to 15 of 15
  1. #1
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Unhappy actions dont work properly, calculator

    please help me how i could make my calcu work.

    this is my code.


    package calculator;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class MyActions implements ActionListener {
    private GUI a;
    public MyActions( GUI b)
    {
    this.a = b;
    }
    public void actionPerformed(ActionEvent e) {
    int operation = checkOp(a.getOperation());
    String invalid = "";
    double num1;
    double num2;
    double result = 0;

    try
    {

    num1 = a.getnum1();
    num2 = a.getnum2();
    if (e.getActionCommand().equals("Calculate"))

    {
    switch(operation)

    {
    case 0:
    result = num1+num2;

    break;
    case 1:
    result = num1-num2;

    break;
    case 2:
    result = num1*num2;

    break;
    case 3:
    result = num1/num2;

    break;

    default:
    invalid = "invalid op";

    break;
    }
    a.setresult(""+invalid+result);

    }
    else if(e.getActionCommand().equals("Clear"))
    {
    a.setresult("");
    a.setnum1("");
    a.setnum2("");
    a.setOperation();

    }
    }
    catch(Exception ex)
    {
    a.setresult("ERROR");
    }
    }
    public int checkOp(String op)

    {
    if(op.equals("+"))
    {
    return 0;

    }
    else if(op.equals("-"))
    {
    return 1;

    }
    else if(op.equals("*"))
    {
    return 2;

    }
    else if(op.equals("/"))
    {
    return 3;

    }
    return 4;



    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    i put screenshots.

    operations are defined:

    0 = +
    1 = -
    2 = *
    3 = /

    but i dont know whats wrong with this.
    Attached Thumbnails Attached Thumbnails actions dont work properly, calculator-calcu1.png   actions dont work properly, calculator-calcu2.jpg  

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Your checkOp method is expecting the op to be + - * or / but you entered 0 which is invalid. Your code is working pefectly. PEBKAC

  5. #5
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    please bare with me. i really need help on this.
    i need to replace operators + , -, * and / to 0 , 1 , 2 and 3 respectively.
    but this code wont let me.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What???

    You just need to input the correct value. Imagine someone wants to use your program and they want to perform an addition. Do you really think it is a good idea to make them remember that addition is 0 and type that into the Operator field or will it be 1000000000 times easier if they just enter +? Since your code already handles + * - and / as the operators then just enter a correct operator and test your program.

  7. #7
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    you know it sir. =)

    anyway, how can i make this code right. i replaced it already with operators + - * / and still wrong output. please help.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    ????????

    How can I say this so that you can understand? THERE IS NOTHING WRONG WITH YOUR CODE.

    Do this:

    Enter 3 in Num1 field
    Enter + in Operator field
    Enter 7 in Num2 field
    Click Calculate

    If 10 appears in the Result field then you're done as your program works. If it doesn't then come back and describe what happened.
    Last edited by Junky; 02-03-2011 at 05:10 AM.

  9. #9
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    here's what happend =(
    Attached Thumbnails Attached Thumbnails actions dont work properly, calculator-calc3.png  
    Last edited by lordjb; 02-03-2011 at 05:20 AM. Reason: added screenshot

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Quote Originally Posted by lordjb
    sir can u take a look at my thread please. much thanks.

    actions dont work properly, calculator
    a private message from op. I dont reply on this thread because I can see that Junky and fubarable can help here.

    Post your lastest code so we can see what are the changes.

  11. #11
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    PHP Code:
    package calculator;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MyActions implements ActionListener {
    	private GUI a;
    	public MyActions( GUI b)
    	{ 
    		this.a = b;
    		}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		int operation = checkOp(a.getOperation());
    		String invalid = "";
    		double num1;
    		double num2;
    		double result = 0;
    		
    try
    		{
    			
    			num1 = a.getnum1();
    			num2 = a.getnum2();
    			if (e.getActionCommand().equals("Calculate"))
    			
    		{
    			switch(operation)
    			
    			{
    			case 0:
    				result = num1+num2;
    				
    				break;
    			case 1:
    				result = num1-num2;
    				
    				break;
    			case 2:
    				result = num1*num2;
    				
    				break;
    			case 3:
    				result = num1/num2;
    			
    				break;
    				
    				default:
    					invalid = "invalid op";
    					
    					break;
    			}
    			a.setresult(""+invalid+result);
    			
    	}
    			else if(e.getActionCommand().equals("Clear"))
    			{
    				a.setresult("");
    				a.setnum1("");
    				a.setnum2("");
    				a.setOperation();
    				
    			}	
    			}
    catch(Exception ex)
    {
    	a.setresult("ERROR");
    }
    }
    public int checkOp(String op)
    
    {
    	if(op.equals("+"))
    	{
    		return 0;
    		
    	}
    	else if(op.equals("-"))
    	{
    		return 1;
    		
    	}
    	else if(op.equals("*"))
    	{
    		return 2;
    		
    	}
    	else if(op.equals("/"))
    	{
    		return 3;
    		
    }
    	return 4;
    	
    	
    
    }
    }
    PHP Code:
    package calculator;
    
    import java.awt.GridLayout;
    
    import javax.swing.*;
    
    public class GUI {
    	
    	private JLabel label1,label2,label3, label4;
    	private JButton btns1, btns2;
    	private JTextField txtfld1, txtfld2, txtfld3, txtfld4;
    	private JFrame f;
    	private MyActions a;
    
    	public GUI(){
    		a = new MyActions (this);
    		f = new JFrame ("Calculator");
    		txtfld1 = new JTextField(20);
    		txtfld2 = new JTextField(20);
    		txtfld3 = new JTextField(20);
    		txtfld4 = new JTextField(20);
    		label1 = new JLabel("Num1: ");
    		label2 = new JLabel("Operator");
    		label3 = new JLabel("Num2: ");
    		label4 = new JLabel("Result: ");
    		btns1 = new JButton("Calculate");
    		btns2 = new JButton("Clear");
    		btns1.addActionListener(a);
    		btns2.addActionListener(a);
    	}
    	public void launch() {
    		JFrame f = new JFrame();
    		f.setLayout(new GridLayout(5,2));
    		
    		
    		f.add(label1);
    		f.add(txtfld1);
    		f.add(label2);
    		f.add(txtfld2);
    		f.add(label3);
    		f.add(txtfld3);
    		f.add(label4);
    		f.add(txtfld4);
    		f.add(btns1);
    		f.add(btns2);
    		f.setSize(250,200);
    		f.setVisible(true);
    		}
    	public double getnum1(){
    		String num1 = txtfld1.getText();
    		double number1 = Double.parseDouble(num1);
    		return 1;
    	}
    	public double getnum2(){
    		String num2 = txtfld3.getText();
    		double number2 = Double.parseDouble(num2);
    		return 2;
    	}
    	public String getOperation() {
    		String operation = txtfld2.getText();
    		return operation;
    	}
    	public void setOperation() {
    		txtfld2.setText("");
    		
    	}
    	public void setnum1 (String num1) {
    		txtfld1.setText(num1);
    		
    	}
    	public void setnum2 (String num2) {
    		txtfld3.setText(num2);
    	}
    	public void setresult (String result) {
    		txtfld4.setText(result);
    }
    }
    PHP Code:
    package calculator;
    
    public class Launcher{
    	public static void main(String [] args) {
    		
    		GUI g = new GUI ();
    		g.launch();
    			
    		}
    }
    these are all of it. and the result is similar to the last screenshot.

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    On your GUI class notice your getNum1() and getNum2() method, you are returning FIX value which is 1 and 2.
    So whatever you enter in txtfld1 getNum1() will ALWAYS return 1, same as getNum2().

    Correct this by returning the parsed number.

    Goodluck!

  13. #13
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    i tried changing that part but still not working. problem is still the same.

    what should be the return value?

  14. #14
    lordjb is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    yehey. i got it already. thanks for the help =)

  15. #15
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Quote Originally Posted by lordjb View Post
    i tried changing that part but still not working. problem is still the same.

    what should be the return value?
    post the GUI class or at least the changed method (getNum1() and getNum2()) so we could see the problem

Similar Threads

  1. Replies: 0
    Last Post: 01-03-2011, 03:24 PM
  2. this programme dont work as I want Help me!!
    By pinar in forum New To Java
    Replies: 2
    Last Post: 10-26-2010, 11:27 PM
  3. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 01:39 AM
  4. Replies: 3
    Last Post: 05-06-2009, 12:11 AM
  5. Eclipse Documentation does not work properly.
    By krawetko in forum Eclipse
    Replies: 0
    Last Post: 10-05-2008, 10:06 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
  •