Calc Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    Calc Question

    Hey guys. Once again I am making a calculator like I do so much. I ran into an error with this program though. I just couldn't figure out how to make the operators work. I want this calculator to be like a standard calculator on http://calculator.com/ So how do I do this? Here is my source code:
    Code:
    /******************************
     ** Program Name: Calculator **
     ** Date: June 3rd, 2008     **
     ** Author: Edward Sanger    **
    ******************************/
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    abstract public class calculator implements ActionListener
    {	
            public static void main(String[] args)
            {
            	
                    //declare variables                                                                                                                                                                              
                    final JFrame frame;
                    final JTextField num;
                    final JButton add, subtract, multiply, divide;
                    final JButton zero, one, two, three, four, five, six, seven, eight, nine;
                    final JButton decimal, clear;
                    Container contentPane;
                    
                    //create a new JFrame and set its title to "Calculator"                                                                                                                                          
                    frame = new JFrame();
                    frame.setTitle("Calculator");                                                                                                                                                                                                                                                                                                                                      
                   
                    //set the frames layout
                    frame.setLayout(null);
                    
                    //create a new container
                    contentPane = frame.getContentPane();
                    
                    //create components                                                                                                                                                                              
                    num = new JTextField("", 20);
    
                    add = new JButton("+");
                    subtract = new JButton("-");
                    multiply = new JButton("*");
                    divide = new JButton("/");
    
    
                    zero = new JButton("0");
                    one = new JButton("1");
                    two = new JButton("2");
                    three = new JButton("3");
                    four = new JButton("4");
                    five = new JButton("5");
                    six = new JButton("6");
                    seven = new JButton("7");
                    eight = new JButton("8");
                    nine = new JButton("9");
                    
                    decimal = new JButton(".");
                    clear = new JButton("c");
    
                    //make num uneditable
                    num.setEditable(false);
                    
                    //apply the components                                                                                                                                                                           
                    contentPane.add(num);
    
                    contentPane.add(one);
                    contentPane.add(two);
                    contentPane.add(three);
                    contentPane.add(four);
                    contentPane.add(five);
                    contentPane.add(six);
                    contentPane.add(seven);
                    contentPane.add(eight);
                    contentPane.add(nine);
                    contentPane.add(zero);
    
                    contentPane.add(add);
                    contentPane.add(subtract);
                    contentPane.add(multiply);
                    contentPane.add(divide);
                    
                    contentPane.add(decimal);
                    contentPane.add(clear);
    
                    //set the bounds for all the components
                    num.setBounds(20, 20, 225, 50);
                    
                    one.setBounds(20, 200, 50, 50);
                    two.setBounds(80, 200, 50, 50);
                    three.setBounds(140, 200, 50, 50);
                    four.setBounds(20, 140, 50, 50);
                    five.setBounds(80, 140, 50, 50);
                    six.setBounds(140, 140, 50, 50);
                    seven.setBounds(20, 80, 50, 50);
                    eight.setBounds(80, 80, 50, 50);
                    nine.setBounds(140, 80, 50, 50);
                    zero.setBounds(20, 260, 50, 50);
                    
                    decimal.setBounds(80, 260, 50, 50);
                    clear.setBounds(140, 260, 50, 50);
                    
                    add.setBounds(200, 260, 50, 50);
                    subtract.setBounds(200, 200, 50, 50);
                    multiply.setBounds(200, 140, 50, 50);
                    divide.setBounds(200, 80, 50, 50);
                    
                    //set the events to happen for the buttons                                                                                                                                                       
                    one.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent one)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 1);
                            }
                        });
                    two.addActionListener(new ActionListener()
                    {
                            public void actionPerformed(ActionEvent two)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 2);
                            }
                        });
                    three.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent three)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 3);
                            }
                        });
                    four.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent four)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 4);
                            }
                        });
                    five.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent five)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 5);
                            }
                        });
                    six.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent seven)
                        {
                            String number;
                            number = num.getText();
                            num.setText("" + number + 6);
                        }
                    });
                    seven.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent seven)
                        {
                            String number;
                            number = num.getText();
                            num.setText("" + number + 7);
                        }
                    });
                        eight.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent eight)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 8);
                            }
                        });
                        nine.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent nine)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 9);
                            }
                        });
                        zero.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent zero)
                            {
                                String number;
                                number = num.getText();
                                num.setText("" + number + 0);
                            }
                        });
                        decimal.addActionListener(new ActionListener()
                        {
                        	public void actionPerformed(ActionEvent decimal)
                        	{
                        		String number;
                        		number = num.getText();
                        		num.setText("" + number + ".");
                        	}
                        });
                        clear.addActionListener(new ActionListener()
                        {
                        	public void actionPerformed(ActionEvent clear) 
                        	{
                        		num.setText("");
                        	}
                        });
    	            add.addActionListener(new ActionListener()
    	            {
    			public void actionPerformed(ActionEvent add)
    			{		
    			}
    	            });
    
                    //frame settings                                                                                                                                                                                 
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setSize(300, 375);
                    frame.setVisible(true);
            }
    }
    And also hi everyone because I haven't seen you in a long time.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Kid Programmer
    Hey guys. Once again I am making a calculator like I do so much. I ran into an error with this program though. I just couldn't figure out how to make the operators work. I want this calculator to be like a standard calculator on http://calculator.com/ So how do I do this? Here is my source code:[ snip ]
    No hard feelings but you can't just dump your code here, throw your hands up in
    the air and say "this doesn't work". It just doesn't work that way.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by JosAH
      ... It just doesn't work that way.

      kind regards,

      Jos
      What might work is if you explain further what you mean by "it doesn't work".

      Comment

      • Kid Programmer
        New Member
        • Mar 2008
        • 176

        #4
        Originally posted by JosAH
        No hard feelings but you can't just dump your code here, throw your hands up in
        the air and say "this doesn't work". It just doesn't work that way.

        kind regards,

        Jos
        I wasn't dumping my code. And look at the post below yours.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Kid Programmer
          I wasn't dumping my code. And look at the post below yours.
          You were dumping your code here and what about r035198x's post? Note that
          in e.g. Sun's Java forum you could've been hanged for your attitude: what exactly
          doesn't work the way you want it to work and what have you tried yourself. I see
          nothing at all in all that code of yours that even attempts to add two numbers
          properly. We can always talk about the other operators later but please remind:
          we are not going to do your work for you; you want a calculator so you have to
          program one. All we can do is give you a bit of help when you're stuck. And what
          about your previous calculator programs? Didn't they work either?

          kind regards,

          Jos

          Comment

          • Kid Programmer
            New Member
            • Mar 2008
            • 176

            #6
            Originally posted by JosAH
            You were dumping your code here and what about r035198x's post? Note that
            in e.g. Sun's Java forum you could've been hanged for your attitude: what exactly
            doesn't work the way you want it to work and what have you tried yourself. I see
            nothing at all in all that code of yours that even attempts to add two numbers
            properly. We can always talk about the other operators later but please remind:
            we are not going to do your work for you; you want a calculator so you have to
            program one. All we can do is give you a bit of help when you're stuck. And what
            about your previous calculator programs? Didn't they work either?

            kind regards,

            Jos
            Angry aren't we. But anyways I was asking how to get it to add the numbers. And I don't want it to add two, but as many as you would like. Like what the standard calculator at calculator.com (which I said in my first post in the this thread) can do. And my previous calculators weren't as good. They were text based which can have a lot of errors if for example you asked the user to enter an operator and they typed "aksdfhasdf ". Other ones were in GUIs but they could only add two numbers. I just wanted help on how I should get it to work. I tried getting it to work but I got errors and things so I deleted that code. That's why in the code I showed you I didn't try to add numbers. And maybe you could do what r035198x's said which was "What might work is if you explain further what you mean by 'it doesn't work'."

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Kid Programmer
              Angry aren't we.
              No I'm not angry; read Henry Weinbergs' "egoless programming" to understand
              what I'm talking about. Just learn and learn how to program.

              Jos

              Comment

              • Kid Programmer
                New Member
                • Mar 2008
                • 176

                #8
                Originally posted by JosAH
                No I'm not angry; read Henry Weinbergs' "egoless programming" to understand
                what I'm talking about. Just learn and learn how to program.

                Jos
                I have been learning. But I ecountered a problem because I couldn't find what to do. I will learn and learn how to program more if you tell me what to do. I'm not asking for the actual code. I'm asking for help on what to do. Now could you please help me.

                Comment

                Working...