Hi there ,
I need a calculator that is flexible enough to perform
+,-,/,%,^and *.
can you help plz:?:

Recommended Answers

All 22 Replies

If you are suppossed to be writing one, then write one, and if you have a specific problem, check back with us. If you simply want to find one to use, check with Google, there are more than enough out there.

Member Avatar for iamthwee

Hi there ,
I need a calculator that is flexible enough to perform
+,-,/,%,^and *.
can you help plz:?:

So basically you want a calculator in java which emulates the one in windows.

There should be lots of examples out there, even some which handles more complex crap like RPN notation. I'm feeling bored (holidays) :sad: I might write a RPN calculator with a nice gui for fun.

tat easy.. u haf each button add a actionlistener..
and inside the action listener perform the required statements lor.
i did that in my school before

For u it might be.There is surely a difference between you and me but the aim of this forum is to exchange ideas.Not to overact and undermine others.
Any way the calculator will be using numbers as input from the buttons as well as from a text file.
That is to say the file will have random real numbers arranged randomly.You have to input thos numbers from the file.
For example if the file has "3,7.9,-9.3,100" then the user has to choose which numbers to take as operands.Its a kind of assigning the text into a string variable, splitting it into parts and manipulating with the elements of the array of the strings.

tat easy.. u haf each button add a actionlistener..
and inside the action listener perform the required statements lor.
i did that in my school before

The idea is to help others, not to enlist them to do your homework for you.
You showed no interest at all in doing anything of your own, just sitting back and waiting for a complete solution to be handed to you on a golden platter.

That's not going to fly, especially since you're not paying us consultancy fees (at $150 an hour per person for a minimum of 80 hours per person).

Thank you for your solutions
But I dont know java very well.
What I know is VB 6.0 and VB.NET
and also as to the content of your hard work you have developed a converter(to number from string).That is good but i need inputs to be selected randomly from the file.
The only thing I should ask everybody is how to select interest operands i.e any operands of interest from the text file.
Thank you.
Embeza

Member Avatar for iamthwee

and also as to the content of your hard work you have developed a converter(to number from string).That is good but i need inputs to be selected randomly from the file.
The only thing I should ask everybody is how to select interest operands i.e any operands of interest from the text file.

Hullo, can you please explain this in greated detail with an example.

Reading from a file is simple when you know how. But what does the file input look like?

So you helped the kid by doing his homework for him, now he can claim a decent grade for doing no work at all.
What does that do for the profession, except causing even more unqualified idiots to enter the marketplace?

Hi Jamttwee

I appreciate your ambition to help me.

What I want is that a calculator that can do calculations based on input from a specific file as well as directly from the numeric button of the interface. The former is more difficult than the latter for me.
So, I would prefer if you do on the former one.
For clarity, the file can be either a text file or a database file. In any case its format looks as below:
"0,2,3,4,5,6,7,8,9,+,-,/,^,%"
Then the system has to store this content into a string array and read from the array for calculation.
Let me knowif any ambiguity
Thanks

Hey,
Your replies are annoying me.
Annoying people is much simpler than building them.
This is not expected from a professional.
Plz try to be friendly.
Try to help me rather.
with regards

Hey,
Your replies are annoying me.
Annoying people is much simpler than building them.
This is not expected from a professional.
Plz try to be friendly.
Try to help me rather.
with regards:-|

Member Avatar for iamthwee

Hi Jamttwee

I appreciate your ambition to help me.

What I want is that a calculator that can do calculations based on input from a specific file as well as directly from the numeric button of the interface. The former is more difficult than the latter for me.
So, I would prefer if you do on the former one.
For clarity, the file can be either a text file or a database file. In any case its format looks as below:
"0,2,3,4,5,6,7,8,9,+,-,/,^,%"
Then the system has to store this content into a string array and read from the array for calculation.
Let me knowif any ambiguity
Thanks

Hullo there, I still don't understand the example you have given. Especially since your most recent example contradicts your previous one.

That is to say the file will have random real numbers arranged randomly.You have to input thos numbers from the file.
For example if the file has "3,7.9,-9.3,100" then the user has to choose which numbers to take as operands.Its a kind of assigning the text into a string variable, splitting it into parts and manipulating with the elements of the array of the strings.

Hey,
Your replies are annoying me.
Annoying people is much simpler than building them.
This is not expected from a professional.
Plz try to be friendly.
Try to help me rather.
with regards:-|

What's annoying is you can't do a simple calculator program and you want someone else to do it for you.

It's not annoying if someone has a problem; and the question doesn't seem like a homework request either.

If you're reading exressions from a file, you need a way to parse the contents of the file and recognise operands; java DOES have a simple evaluate function, so you can do Integer.decode(String : s) : int to automatically calculate very simple things (I think, +, - and *). Unfortunately there's no similar Float.decode() or Double.decode() functions.

For more complex things, you'll have to do a String recognitition on every character that COULD be an operand, while buffering values that are numbers; as you reach "lower levels" of the calculation, calculate the results, then keep moving upwards until you get the result of the whole calculation.

Can get very complicated if you want to offer an equivilant inline calculation spec as Java, if you always have 2 operators and an operand it's far easier, if I had the String "5 + 4" I could split it by "+" then add the contents of split[0] and split[2], if it didn't split, I could then try splitting by "*" and so on.

I don't know if Java supports regular expressions in the String.split() function, that would be much helpful; although I suppose you'd never know where your operators were before the split.

Java is quite different from VB; there are methodical similarilities but the structure is far from. How would you do it in VB? the same method will probably apply to Java, if missing functions are a problem, think about what the VB functions actually do, and all of them (except Windows API depents/calls) will translate easily into Java.

Matt

Hullo there, I still don't understand the example you have given. Especially since your most recent example contradicts your previous one.

I know it is contradicting that's because I have done the the way of using the buttons . But now i need inputs from files.
E.g eth.txt="0,2,4,1,7,8,9,6,+,-,/"
and you have prepared a form with controls in it then
your program will put the numbers and operators in appropriate buttons.For instance 0 will be the value to be displayed on the diplay textbox when the button with text "0" displayed on it is clicked. The operator addition will be executed if the add button is clicked. The only new thing is how to display a number n on the display box when the button representing that number is clicked- all inputs are from the text file.
thanks

you can bring the numbers from the text file easily enough, do you know how to do java file access?

getting operators from text in actual methods is more difficult, you'll either have to pre-write possible methods in your java app and chose one based on comparison, or make your app extensible/updatable and allow your user to write classes to handle their custom operations.

Matt

Member Avatar for iamthwee

In that link I gave you open up the CalcGUI class.

In there find the line:-

String buttonOrder = "789456123 0 ";

E.g eth.txt="0,2,4,1,7,8,9,6,+,-,/"

Change the line to this:-

String buttonOrder = "02417896";

You'll notice the interface has changed. So effectively all you should be doing is reading your stuff from the text file and assigning that line to the variable buttonOrder ?

With the exception of the operators it should be as simple as that?

Yes? No?

It's not annoying if someone has a problem; and the question doesn't seem like a homework request either.

If you're reading exressions from a file, you need a way to parse the contents of the file and recognise operands; java DOES have a simple evaluate function, so you can do Integer.decode(String : s) : int to automatically calculate very simple things (I think, +, - and *). Unfortunately there's no similar Float.decode() or Double.decode() functions.

For more complex things, you'll have to do a String recognitition on every character that COULD be an operand, while buffering values that are numbers; as you reach "lower levels" of the calculation, calculate the results, then keep moving upwards until you get the result of the whole calculation.

Can get very complicated if you want to offer an equivilant inline calculation spec as Java, if you always have 2 operators and an operand it's far easier, if I had the String "5 + 4" I could split it by "+" then add the contents of split[0] and split[2], if it didn't split, I could then try splitting by "*" and so on.

I don't know if Java supports regular expressions in the String.split() function, that would be much helpful; although I suppose you'd never know where your operators were before the split.

Java is quite different from VB; there are methodical similarilities but the structure is far from. How would you do it in VB? the same method will probably apply to Java, if missing functions are a problem, think about what the VB functions actually do, and all of them (except Windows API depents/calls) will translate easily into Java.

Matt

Oh thanks
But I would be more satisfied if you tell me some about how to code the necessary functions

Assuming you've read in the values from a files, and placed the button "values" onto the buttons as labels:

boolean gotOperator = false;
String operand1 = new String();
String operand2 = new String();
String operator = new String();
float result;
JLabel display = new JLabel();
public void actionPerformed(ActionEvent e) {
  String buttonLabel = ( (JButton) (e.getSource())).getText();
  if (!gotOperator) {
    try {
      //Assume user hasn't picked first operand. Using parseFloat() inside toString()
      //causes a NumberFormatException if the button caption isn't numeric.
      //this is like using an isNumeric() function; there is no such function
      //in java that I know of.
      operand1 = operand1 + Float.toString(Float.parseFloat(buttonLabel));
      display.setText(operand1);
    }
    catch (NumberFormatException ex) {
      //If button caption is the decimal point, it needs to be appended to the first
      //operand.
      if (buttonLabel.equals(".")) {
        operand1 = operand1 + ".";
        display.setText(operand1);        
      }else{
        //Otherwiste, assume user just picked an operator
        operator = buttonLabel;
        gotOperator = true;
      }
    }
  }
  else {
    try {
      //User has picked an operator.
      operand2 = operand2 + Float.toString(Float.parseFloat(buttonLabel));
      display.setText(operand2);
    }
    catch (NumberFormatException ex) {
      //if (buttonLabel.equals("=")) {
      //Assume user pressed equals, even if they didn't; still need to calculate
      //the result for 2 operands in order to do consequetive calculations.
      if (operator.equals("+")) {
        result = Float.parseFloat(operand1) + Float.parseFloat(operand2);
      }
      else if (operator.equals("-")) {
        result = Float.parseFloat(operand1) - Float.parseFloat(operand2);
      }
      else if (operator.equals("*")) {
        result = Float.parseFloat(operand1) * Float.parseFloat(operand2);
      }
      else if (operator.equals("/")) {
        result = Float.parseFloat(operand1) / Float.parseFloat(operand2);
      }
      //Set the first operand (of the next calculation) to the result of this one.
      //Clear second operand, clear gotOperator flag.
      operand1 = Float.toString(result);
      operand2 = "";
      gotOperator = false;
      display.setText(Float.toString(result));
    }
    //}
  }
}

Any problems with the code, ask me. Any problems with integrating that code into your application, read a java book.

Matt

Assuming you've read in the values from a files, and placed the button "values" onto the buttons as labels:

boolean gotOperator = false;
String operand1 = new String();
String operand2 = new String();
String operator = new String();
float result;
JLabel display = new JLabel();
public void actionPerformed(ActionEvent e) {
  String buttonLabel = ( (JButton) (e.getSource())).getText();
  if (!gotOperator) {
    try {
      //Assume user hasn't picked first operand. Using parseFloat() inside toString()
      //causes a NumberFormatException if the button caption isn't numeric.
      //this is like using an isNumeric() function; there is no such function
      //in java that I know of.
      operand1 = operand1 + Float.toString(Float.parseFloat(buttonLabel));
      display.setText(operand1);
    }
    catch (NumberFormatException ex) {
      //If button caption is the decimal point, it needs to be appended to the first
      //operand.
      if (buttonLabel.equals(".")) {
        operand1 = operand1 + ".";
        display.setText(operand1);        
      }else{
        //Otherwiste, assume user just picked an operator
        operator = buttonLabel;
        gotOperator = true;
      }
    }
  }
  else {
    try {
      //User has picked an operator.
      operand2 = operand2 + Float.toString(Float.parseFloat(buttonLabel));
      display.setText(operand2);
    }
    catch (NumberFormatException ex) {
      //if (buttonLabel.equals("=")) {
      //Assume user pressed equals, even if they didn't; still need to calculate
      //the result for 2 operands in order to do consequetive calculations.
      if (operator.equals("+")) {
        result = Float.parseFloat(operand1) + Float.parseFloat(operand2);
      }
      else if (operator.equals("-")) {
        result = Float.parseFloat(operand1) - Float.parseFloat(operand2);
      }
      else if (operator.equals("*")) {
        result = Float.parseFloat(operand1) * Float.parseFloat(operand2);
      }
      else if (operator.equals("/")) {
        result = Float.parseFloat(operand1) / Float.parseFloat(operand2);
      }
      //Set the first operand (of the next calculation) to the result of this one.
      //Clear second operand, clear gotOperator flag.
      operand1 = Float.toString(result);
      operand2 = "";
      gotOperator = false;
      display.setText(Float.toString(result));
    }
    //}
  }
}

Any problems with the code, ask me. Any problems with integrating that code into your application, read a java book.

Matt

Hi baddy,
Thanks for help.
But I can't test it since I don't have a java compiler.
Sorry but can do it in VB 6 because I am a beginner of Java.
I have not well understood all.
with regards

A java compiler is an often overlooked prerequisite for developing java applications. o_O

Download Borland's free JBuilder2005 personal edition, you'll probably need the java software development kit aswell. (Available from Sun's website) although methinks JBuilder 2005 installs an SDK.

This kind of app is quite well suited to VB to be honest; but Java is actually easier to learn, and definately easier to work with/in than VB.

Matt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.