Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Create a calculator with JCreator

  1. #1
    Junior Member
    Join Date
    May 2010
    Location
    Ratnapura,Sri Lanka
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Unhappy Create a calculator with JCreator

    Dear Java Friends;
    I have got a problem when coding a java program to create a calculator with JCreator. Please help me to write codes for decimal point.

    • Decimal point should not be set to the text more than 1 time until an operator is pressed.
    • How can I use a Boolean value to solve this problem.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Create a calculator with JCreator

    What code do you have so far?
    You would use a boolean variable to remember an event. It would be false until the event happened, then it would be set true.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Delmi (May 25th, 2010)

  4. #3
    Junior Member
    Join Date
    May 2010
    Location
    Ratnapura,Sri Lanka
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Re: Create a calculator with JCreator

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Calculator extends JFrame{
     
    	double firstNumber;
        boolean calculate = false;
        char operator;
     
    	JTextField txtDisplay;
    	JButton bt1;
    	JButton bt2;
    	JButton bt3;
    	JButton bt4;
    	JButton bt5;
    	JButton bt6;
    	JButton bt7;
    	JButton bt8;
    	JButton bt9;
    	JButton bt0;
    	JButton btAdd;
    	JButton btSub;
    	JButton btDiv;
    	JButton btMul;
    	JButton btEqual;
    	JButton btDec;
    	JButton btClear;
    	JButton btSqrt;
     
    	JPanel pane;
     
    	Calculator(){
    		setTitle("Calculator");
    		txtDisplay=new JTextField();
    		add(txtDisplay,"North");
     
    		pane=new JPanel();
    		pane.setLayout(new GridLayout(5,4,6,6));
    		bt1=new JButton("1");
    		bt1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("1");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "1");
    		        }
                }
     
            } );
     
    		bt2=new JButton("2");
    		bt2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("2");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "2");
    		        }
                }
     
            } );
    		bt3=new JButton("3");
    		bt3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("3");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "3");
    		        }
                }
     
            } );
    		bt4=new JButton("4");
    		bt4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("4");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "4");
    		        }
                }
     
            } );
    		bt5=new JButton("5");
    		bt5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("5");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "5");
    		        }
                }
     
            } );
    		bt6=new JButton("6");		
    		bt6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("6");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "6");
    		        }
                }
     
            } );
    		bt7=new JButton("7");
    		bt7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("7");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "7");
    		        }
                }
     
            } );
    		bt8=new JButton("8");
    		bt8.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("8");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "8");
    		        }
                }
     
            } );
    		bt9=new JButton("9");
    		bt9.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("9");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "9");
    		        }
                }
     
            } );
    		bt0=new JButton("0");
    		bt0.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        if (calculate) {
    		            txtDisplay.setText("0");
    		            calculate=false;
    		        } else {
    		            txtDisplay.setText(s + "0");
    		        }
                }
     
            } );
            	btClear=new JButton("C");
    		btClear.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {                   
    		            txtDisplay.setText(" ");
     
                }
     
            } );
    		btAdd=new JButton("+");
    		btAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   	String s = txtDisplay.getText();
    		        firstNumber = Double.parseDouble(s);
    		        calculate = true;
    		        operator='+';
     
        			}     
     
            });
    		btSub=new JButton("-");
    		btSub.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   	String s = txtDisplay.getText();
    		        firstNumber = Double.parseDouble(s);
    		        calculate = true;
    		        operator='-';
     
        			}     
     
            });
    		btDiv=new JButton("/");
    		btDiv.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   	String s = txtDisplay.getText();
    		        firstNumber = Double.parseDouble(s);
    		        calculate = true;
    		        operator='/';
     
        			}     
     
            });
    		btMul=new JButton("*");
    		btMul.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   	String s = txtDisplay.getText();
    		        firstNumber = Double.parseDouble(s);
    		        calculate = true;
    		        operator='*';
     
        			}     
     
            });
    		btEqual=new JButton("=");
    		btEqual.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                   	String s = txtDisplay.getText();
    		        double secondNumber=Double.parseDouble(s);
    		        double result=secondNumber;		         
    		        switch(operator){
    		            case '+':
    		             result=firstNumber+secondNumber;
    		             break;
    		            case '-':
    		             result=firstNumber-secondNumber;
    		             break;
    		            case '/':
    		             result=firstNumber/secondNumber;
    		             break;
    		            case '*':
    		             result=firstNumber*secondNumber;
    		             break;		
    		        }
     
    			   	txtDisplay.setText(""+result);		 
        		}          
            });
    		btDec=new JButton(".");
    		btDec.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
                //    int count=0;
     
    		        if (calculate) {
    		            txtDisplay.setText(".");
    		          	calculate=false;          		
     
    		        } else {
    		            txtDisplay.setText(s + ".");
    		           // calculate=true;
    		         }
     
     
                }
     
            } );
            btSqrt=new JButton("Sqrt");
    		btSqrt.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
                    String s = txtDisplay.getText();
    		        s=txtDisplay.getText();	
                 	double q=Math.sqrt(Double.parseDouble(s));
            	 	txtDisplay.setText(q+"");
     
    		        }           	
            } );
     
    		pane.add(bt1);
    		pane.add(bt2);
    		pane.add(bt3);
    		pane.add(btAdd);
    		pane.add(bt4);
    		pane.add(bt5);
    		pane.add(bt6);
    		pane.add(btSub);
    		pane.add(bt7);
    		pane.add(bt8);
    		pane.add(bt9);
    		pane.add(btDiv);
    		pane.add(bt0);
    		pane.add(btDec);
    		pane.add(btEqual);
    		pane.add(btMul);
    		pane.add(btClear);
    		pane.add(btSqrt);
     
    		add(pane);			
    	}
     
    }
    class DemoFrame{
    	public static void main(String args[]){
    		Calculator c1=new Calculator();
    		c1.pack();
    		c1.show();
    	}
    }
    This is the code that I have coded. Please help me to re-correct my program, as the decimal point is not working properly.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Create a calculator with JCreator

    the decimal point is not working properly
    Can your be more specific about the problem?
    Show what program does, describe why that is wrong and show what it should do.

  6. #5
    Junior Member
    Join Date
    May 2010
    Location
    Ratnapura,Sri Lanka
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Re: Create a calculator with JCreator

    Hello Java Friends;
    Sorry for the delay reply...
    The Decimal Point is not working properly.
    The program should not set the decimal point to a number more than one time; until an operator is pressed. I have given the code that I have coded. But I can't re-correct the error. Please help me to solve this problem.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Create a calculator with JCreator

    program should not set the decimal point to a number more than one time
    It would help if you could post an example of the program doing this.
    Use println() to output the wrong results with a comment about how its wrong.

    It sounds like you need a flag to remember if a decimal point has been set and to not allow another if one has been entered.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Delmi (June 1st, 2010)

Similar Threads

  1. calculator GUI needs to compute
    By javanovice in forum AWT / Java Swing
    Replies: 3
    Last Post: May 4th, 2010, 02:16 PM
  2. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  3. Help codiing calculator
    By FM010 in forum Exceptions
    Replies: 7
    Last Post: June 12th, 2009, 02:25 PM
  4. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM