Results 1 to 17 of 17
  1. #1
    jvpk4 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Please someone help me with this calculator...due tomorrow..

    This is my code.. everything is working fine.. i just need the code to this calculator can ADD subtract more than 2 digits.. and this is all new to me. and this assignment is due tomorrow.. plzzz help.. i would appreciate it very much...:--


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    //This is the accumulator program that adds and subtracts numbers
    //The input comes through the keypad
    //This program is written by ME
    
    public class Adding_Machine extends JApplet
    {
    	JTextField jtfInput = new JTextField(50);
    
    	Font f1 = new Font("Forte",Font.BOLD,20);
    
    	JButton jbtClear = new JButton("Clear");
    	JButton jbt7 = new JButton("7");
    	JButton jbt8 = new JButton("8");
    	JButton jbt9 = new JButton("9");
    	JButton jbt4 = new JButton("4");
    	JButton jbt5 = new JButton("5");
    	JButton jbt6 = new JButton("6");
    	JButton jbt1 = new JButton("1");
    	JButton jbt2 = new JButton("2");
    	JButton jbt3 = new JButton("3");
    	JButton jbtMinus = new JButton("-");
    	JButton jbt0 = new JButton("0");
    	JButton jbtPlus = new JButton("+");
    
    	//implements constructor. creates the buttons textfield and panel
    	public void init()
    	{
    		JPanel p1 = new JPanel();
    		p1.setBackground(Color.GREEN);
    		p1.setLayout(new GridLayout(4,3,5,5));
    		p1.add(jbt7);
    		p1.add(jbt8);
    		p1.add(jbt9);
    		p1.add(jbt4);
    		p1.add(jbt5);
    		p1.add(jbt6);
    		p1.add(jbt1);
    		p1.add(jbt2);
    		p1.add(jbt3);
    		p1.add(jbtMinus);
    		p1.add(jbt0);
    		p1.add(jbtPlus);
    
    		jbt9.setFont(f1);
    		jbt9.setForeground(Color.BLUE);
    		jbt8.setFont(f1);
    		jbt8.setForeground(Color.BLUE);
    		jbt7.setFont(f1);
    		jbt7.setForeground(Color.BLUE);
    		jbt6.setFont(f1);
    		jbt6.setForeground(Color.BLUE);
    		jbt5.setFont(f1);
    		jbt5.setForeground(Color.BLUE);
    		jbt4.setFont(f1);
    		jbt4.setForeground(Color.BLUE);
    		jbt3.setFont(f1);
    		jbt3.setForeground(Color.BLUE);
    		jbt2.setFont(f1);
    		jbt2.setForeground(Color.BLUE);
    		jbt1.setFont(f1);
    		jbt1.setForeground(Color.BLUE);
    		jbt0.setFont(f1);
    		jbt0.setForeground(Color.BLUE);
    		jbtMinus.setForeground(Color.RED);
    		jbtMinus.setFont(f1);
    		jbtPlus.setForeground(Color.RED);
    		jbtPlus.setFont(f1);
    		jbtClear.setForeground(Color.RED);
    		jbtClear.setFont(f1);
    
    		getContentPane().add(jtfInput, BorderLayout.NORTH);
    		getContentPane().add(p1, BorderLayout.CENTER);
    		getContentPane().add(jbtClear, BorderLayout.SOUTH);
    	}
    
    	//main driver that sets off parameters of the frame
    	public static void main(String [] args)
    	{
    		Adding_Machine applet = new Adding_Machine();
    		applet.init();
    		JFrame frame = new JFrame();
    		frame.add(applet.getContentPane());
    		frame.setTitle("Adding Machine");
    		frame.setLocation(300,200);
    		frame.setSize(300,400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	}
    Last edited by Eranga; 06-24-2010 at 04:07 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Please use code tags when you are posting again in the forum. Unformated codes are really hard to read. If you don't know how to do it, check my forum signature.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Regarding your question, where did you do the calculation part. I cannot see that you've done related to that, only the UI.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I might be missing something here but the purpose of an assignment is to give you the opportunity to learn something. It's like going to the gym: the point is to get healthy.

    Other people could go to the gym for you. But they would get healthy, and you would miss out.

    I wouldn't want you to miss out so write your own code and, if you encounter a problem, post a question about that.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I agreed with you pbrockway2. And also this is an assignment, so OP must show his/her effort. I believe that this code is written by him, but then why he's not even give a try on the rest.

  6. #6
    jvpk4 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    yes u guys are right.. but i dont know how can i add

    the addition or subtraction code.. thats where i am really confused.. i dont know how and where to add the addition and subtraction code..

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

    Default

    Quote Originally Posted by jvpk4 View Post
    yes u guys are right.. but i dont know how can i add

    the addition or subtraction code.. thats where i am really confused.. i dont know how and where to add the addition and subtraction code..
    You've got a bunch of JButtons, so adding action listeners to the JButtons would seem like a great place to start. A method that does this is the addActionListener(ActionListener al) method of JButton (actually AbstractButton which JButton extends). To learn how to do this, go to the Sun Swing button tutorial: How to use Buttons

    Luck!
    Last edited by Fubarable; 06-24-2010 at 05:18 AM.

  8. #8
    jvpk4 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks, it making sense... kinda.. lol

  9. #9
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Your project doesn't contain a BL so I think you have to develope it first or to find a code and add it for your GUI :)
    I recommend reading this Writing Calculator program in Swing
    and this Java Swing Calculator Source Code
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  10. #10
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    you should delare your JButtons etc as an array, and then u can manipulate then all with a loop. makes code alot more cleaner
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by jvpk4 View Post
    yes u guys are right.. but i dont know how can i add

    the addition or subtraction code.. thats where i am really confused.. i dont know how and where to add the addition and subtraction code..
    Okay, to start up please write down your logic, that's the way you are going to done this. Then try to identify where you should implement it. Fubarable says something about action listeners, where your control events invoke.

    Did you do anything on that?

  12. #12
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    1) To create a calculator, you need some kind of parse tree. I would strongly recommend an Abstract Syntax Tree.

    2) Recursion can be used to create a tree, by looping operators in order of precedence (eg: addition, subtraction, multiplication then division)

    3) When you have built your syntax tree, you can start from the root node, and begin a post-tree-recursion to find the lowest node. From there, you can begin using the operators. For example:

    4 + 1 becomes
    Java Code:
           +
         /   \
        /     \
       4      1
    from there, we can use the plus to know that 4 adds with 1.

    This can be extended to create very very complex structures, involving lots more mathematical operators, but this is enough to get you started.

    HTH,

    Jvl

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm sure that use of abstract syntax tree is the most easiest as well as most suitable way to workout this application. But I don't know how much easier to understand and implement it within our OPs' code. He's still struggling with the basic implementation.

  14. #14
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Quote Originally Posted by JvlShz View Post
    1) To create a calculator, you need some kind of parse tree. I would strongly recommend an Abstract Syntax Tree.

    2) Recursion can be used to create a tree, by looping operators in order of precedence (eg: addition, subtraction, multiplication then division)

    3) When you have built your syntax tree, you can start from the root node, and begin a post-tree-recursion to find the lowest node. From there, you can begin using the operators. For example:

    4 + 1 becomes
    Java Code:
           +
         /   \
        /     \
       4      1
    from there, we can use the plus to know that 4 adds with 1.

    This can be extended to create very very complex structures, involving lots more mathematical operators, but this is enough to get you started.

    HTH,

    Jvl
    Inorder tree traversal, not postorder.
    Last edited by Lil_Aziz1; 06-27-2010 at 02:03 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Not interest at all, the idea is how far our OP could implement it. I don't think at the moment it's not suitable to use, isn't it?

  16. #16
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Oh. Yea that's reasonable. Sorry about that.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Oh. Yea that's reasonable. Sorry about that.
    Hey lol. I'm not saying that what you've said is wrong. Honestly I've never recognize that there is any error, until you pointed it here.

    What I want to point is that looking at the OPs' code I'm sure that he/she relly confuse with this. ;)

Similar Threads

  1. Ordered Array plz help....due tomorrow!!
    By canyonist in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 09:57 AM
  2. need help with my calculator
    By semoche in forum AWT / Swing
    Replies: 6
    Last Post: 12-04-2009, 10:16 PM
  3. Replies: 10
    Last Post: 02-11-2009, 07:38 AM
  4. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM
  5. Need help~~~ Midterm tomorrow!!!
    By str4kt in forum New To Java
    Replies: 23
    Last Post: 10-10-2008, 09:12 AM

Posting Permissions

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