473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Calc Question

176 100+
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:
Expand|Select|Wrap|Line Numbers
  1. /******************************
  2.  ** Program Name: Calculator **
  3.  ** Date: June 3rd, 2008     **
  4.  ** Author: Edward Sanger    **
  5. ******************************/
  6.  
  7. import javax.swing.*;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. abstract public class calculator implements ActionListener
  12. {    
  13.         public static void main(String[] args)
  14.         {
  15.  
  16.                 //declare variables                                                                                                                                                                              
  17.                 final JFrame frame;
  18.                 final JTextField num;
  19.                 final JButton add, subtract, multiply, divide;
  20.                 final JButton zero, one, two, three, four, five, six, seven, eight, nine;
  21.                 final JButton decimal, clear;
  22.                 Container contentPane;
  23.  
  24.                 //create a new JFrame and set its title to "Calculator"                                                                                                                                          
  25.                 frame = new JFrame();
  26.                 frame.setTitle("Calculator");                                                                                                                                                                                                                                                                                                                                      
  27.  
  28.                 //set the frames layout
  29.                 frame.setLayout(null);
  30.  
  31.                 //create a new container
  32.                 contentPane = frame.getContentPane();
  33.  
  34.                 //create components                                                                                                                                                                              
  35.                 num = new JTextField("", 20);
  36.  
  37.                 add = new JButton("+");
  38.                 subtract = new JButton("-");
  39.                 multiply = new JButton("*");
  40.                 divide = new JButton("/");
  41.  
  42.  
  43.                 zero = new JButton("0");
  44.                 one = new JButton("1");
  45.                 two = new JButton("2");
  46.                 three = new JButton("3");
  47.                 four = new JButton("4");
  48.                 five = new JButton("5");
  49.                 six = new JButton("6");
  50.                 seven = new JButton("7");
  51.                 eight = new JButton("8");
  52.                 nine = new JButton("9");
  53.  
  54.                 decimal = new JButton(".");
  55.                 clear = new JButton("c");
  56.  
  57.                 //make num uneditable
  58.                 num.setEditable(false);
  59.  
  60.                 //apply the components                                                                                                                                                                           
  61.                 contentPane.add(num);
  62.  
  63.                 contentPane.add(one);
  64.                 contentPane.add(two);
  65.                 contentPane.add(three);
  66.                 contentPane.add(four);
  67.                 contentPane.add(five);
  68.                 contentPane.add(six);
  69.                 contentPane.add(seven);
  70.                 contentPane.add(eight);
  71.                 contentPane.add(nine);
  72.                 contentPane.add(zero);
  73.  
  74.                 contentPane.add(add);
  75.                 contentPane.add(subtract);
  76.                 contentPane.add(multiply);
  77.                 contentPane.add(divide);
  78.  
  79.                 contentPane.add(decimal);
  80.                 contentPane.add(clear);
  81.  
  82.                 //set the bounds for all the components
  83.                 num.setBounds(20, 20, 225, 50);
  84.  
  85.                 one.setBounds(20, 200, 50, 50);
  86.                 two.setBounds(80, 200, 50, 50);
  87.                 three.setBounds(140, 200, 50, 50);
  88.                 four.setBounds(20, 140, 50, 50);
  89.                 five.setBounds(80, 140, 50, 50);
  90.                 six.setBounds(140, 140, 50, 50);
  91.                 seven.setBounds(20, 80, 50, 50);
  92.                 eight.setBounds(80, 80, 50, 50);
  93.                 nine.setBounds(140, 80, 50, 50);
  94.                 zero.setBounds(20, 260, 50, 50);
  95.  
  96.                 decimal.setBounds(80, 260, 50, 50);
  97.                 clear.setBounds(140, 260, 50, 50);
  98.  
  99.                 add.setBounds(200, 260, 50, 50);
  100.                 subtract.setBounds(200, 200, 50, 50);
  101.                 multiply.setBounds(200, 140, 50, 50);
  102.                 divide.setBounds(200, 80, 50, 50);
  103.  
  104.                 //set the events to happen for the buttons                                                                                                                                                       
  105.                 one.addActionListener(new ActionListener()
  106.                     {
  107.                         public void actionPerformed(ActionEvent one)
  108.                         {
  109.                             String number;
  110.                             number = num.getText();
  111.                             num.setText("" + number + 1);
  112.                         }
  113.                     });
  114.                 two.addActionListener(new ActionListener()
  115.                 {
  116.                         public void actionPerformed(ActionEvent two)
  117.                         {
  118.                             String number;
  119.                             number = num.getText();
  120.                             num.setText("" + number + 2);
  121.                         }
  122.                     });
  123.                 three.addActionListener(new ActionListener()
  124.                     {
  125.                         public void actionPerformed(ActionEvent three)
  126.                         {
  127.                             String number;
  128.                             number = num.getText();
  129.                             num.setText("" + number + 3);
  130.                         }
  131.                     });
  132.                 four.addActionListener(new ActionListener()
  133.                     {
  134.                         public void actionPerformed(ActionEvent four)
  135.                         {
  136.                             String number;
  137.                             number = num.getText();
  138.                             num.setText("" + number + 4);
  139.                         }
  140.                     });
  141.                 five.addActionListener(new ActionListener()
  142.                     {
  143.                         public void actionPerformed(ActionEvent five)
  144.                         {
  145.                             String number;
  146.                             number = num.getText();
  147.                             num.setText("" + number + 5);
  148.                         }
  149.                     });
  150.                 six.addActionListener(new ActionListener()
  151.                 {
  152.                     public void actionPerformed(ActionEvent seven)
  153.                     {
  154.                         String number;
  155.                         number = num.getText();
  156.                         num.setText("" + number + 6);
  157.                     }
  158.                 });
  159.                 seven.addActionListener(new ActionListener()
  160.                 {
  161.                     public void actionPerformed(ActionEvent seven)
  162.                     {
  163.                         String number;
  164.                         number = num.getText();
  165.                         num.setText("" + number + 7);
  166.                     }
  167.                 });
  168.                     eight.addActionListener(new ActionListener()
  169.                     {
  170.                         public void actionPerformed(ActionEvent eight)
  171.                         {
  172.                             String number;
  173.                             number = num.getText();
  174.                             num.setText("" + number + 8);
  175.                         }
  176.                     });
  177.                     nine.addActionListener(new ActionListener()
  178.                     {
  179.                         public void actionPerformed(ActionEvent nine)
  180.                         {
  181.                             String number;
  182.                             number = num.getText();
  183.                             num.setText("" + number + 9);
  184.                         }
  185.                     });
  186.                     zero.addActionListener(new ActionListener()
  187.                     {
  188.                         public void actionPerformed(ActionEvent zero)
  189.                         {
  190.                             String number;
  191.                             number = num.getText();
  192.                             num.setText("" + number + 0);
  193.                         }
  194.                     });
  195.                     decimal.addActionListener(new ActionListener()
  196.                     {
  197.                         public void actionPerformed(ActionEvent decimal)
  198.                         {
  199.                             String number;
  200.                             number = num.getText();
  201.                             num.setText("" + number + ".");
  202.                         }
  203.                     });
  204.                     clear.addActionListener(new ActionListener()
  205.                     {
  206.                         public void actionPerformed(ActionEvent clear) 
  207.                         {
  208.                             num.setText("");
  209.                         }
  210.                     });
  211.                 add.addActionListener(new ActionListener()
  212.                 {
  213.             public void actionPerformed(ActionEvent add)
  214.             {        
  215.             }
  216.                 });
  217.  
  218.                 //frame settings                                                                                                                                                                                 
  219.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  220.                 frame.pack();
  221.                 frame.setSize(300, 375);
  222.                 frame.setVisible(true);
  223.         }
  224. }
  225.  
And also hi everyone because I haven't seen you in a long time.
Jul 2 '08 #1
7 1672
JosAH
11,448 Expert 8TB
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
Jul 2 '08 #2
r035198x
13,262 8TB
... 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".
Jul 2 '08 #3
Kid Programmer
176 100+
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.
Jul 2 '08 #4
JosAH
11,448 Expert 8TB
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
Jul 2 '08 #5
Kid Programmer
176 100+
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'."
Jul 2 '08 #6
JosAH
11,448 Expert 8TB
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
Jul 2 '08 #7
Kid Programmer
176 100+
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.
Jul 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Astra | last post by:
Hi All Wonder if you can help me with the following calc, which I want to create to check whether or not a user has entered a valid credit card expiry date: 1) I have retrieve a value from 2 x...
12
by: Susan Cranford | last post by:
Please forgive, I have looked at so much info I can't figure out how to put it together even though I know it must be fairly simple. I have an array of input text boxes (txtDOBn) where n is...
16
by: PeteCresswell | last post by:
I was happily coding along, putting some calculation results (rolling annualized rates of return - too compute-intensive to calculate on-the-fly - had tb staged beforehand via a batch job) into...
2
by: Sven K?hne | last post by:
Hello *, Our new compiler has some problems with this line: pBuf = Calc(pBuf, i); The Assembler-Code generated uses the increases "i" as a Paramter in Calc. Our old Compiler did not do...
0
by: MLH | last post by:
I was wondering what the differences in resource requirements between calc.exe and notepad.exe might be and if such differences might explain why Access 97 locks up when I run...
6
by: vj | last post by:
I have a program which generates xml files for excel but these files are not recognized by open office calc. I looked at the OO uno library, but it seems like an over kill. In my experience, for...
21
by: mm | last post by:
(Yes, I konw whats an object is...) BTW. I did a translation of a pi callculation programm in C to Python. (Do it by your own... ;-) -------- Calc PI for 800 digs(?). (german: Stellen) ------...
2
by: babakandme | last post by:
How can I Run( Execute ) another application( Calc.exe ) from another application in VC++? I mean, is there any API or Function to do it? Appreciate...
5
by: Manogna | last post by:
hi! i got the following error while using the Date::Calc module in perl. plz help me.. the file is ex.pl contains code like this: use strict; use Date::Calc();
7
by: sparks | last post by:
I am working on a database that has a lot of calculated values on the forms. These were never put into the tables. But were tied to unbound fields on the forms. Now 8000 records later they want...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.