Results 1 to 17 of 17
- 06-24-2010, 03:33 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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
- 06-24-2010, 04:08 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-24-2010, 04:12 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Regarding your question, where did you do the calculation part. I cannot see that you've done related to that, only the UI.
- 06-24-2010, 04:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.
- 06-24-2010, 04:20 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-24-2010, 04:57 AM #6
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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.
- 06-24-2010, 05:52 AM #8
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
thanks, it making sense... kinda.. lol
- 06-24-2010, 06:29 AM #9
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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 CodeIf my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
- 06-24-2010, 07:30 AM #10
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
- 06-24-2010, 07:14 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-25-2010, 05:47 AM #12
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
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
from there, we can use the plus to know that 4 adds with 1.Java Code:+ / \ / \ 4 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
- 06-26-2010, 02:23 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-27-2010, 02:01 AM #14
- 06-27-2010, 03:50 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 06-27-2010, 04:08 AM #16
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)
- 06-27-2010, 04:16 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Ordered Array plz help....due tomorrow!!
By canyonist in forum New To JavaReplies: 6Last Post: 05-23-2010, 09:57 AM -
need help with my calculator
By semoche in forum AWT / SwingReplies: 6Last Post: 12-04-2009, 10:16 PM -
PLEASE HELP! i need some code by tomorrow for my exam :-(
By madouva in forum New To JavaReplies: 10Last Post: 02-11-2009, 07:38 AM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM -
Need help~~~ Midterm tomorrow!!!
By str4kt in forum New To JavaReplies: 23Last Post: 10-10-2008, 09:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks