How do I implement a desktop calculator - java

I need to get some input from user in my application, and then use it in Java. But, it is quite more complicated than get some value from GUI and assign it to variable. The value should be processed according some rules.
For example:
input from user is string "2 + 3", then he clicks "RUN" button, and when the button is clicked I need to assign "2" to one variable, "3" to next variable, and then make SUM of it.

I suggest you use http://www.beanshell.org/ This tool is used in a number of IDEs with the debugger to evaluate expressions.

Use the ScriptEngine. E.G. here.
=

If all you needed is to make some simple math calculation like these, I would use 2 Stacks to maintain the syntax. You can tokenise the input Strings and then use one Stack as the operators Stack and the other as the value Stack. And then you know that for every one pop from the operator Stack, the next pop from the value Stack must be an integer. If it isn't, you know the rules is broken and you can throw an error to your user.

Here is some code for a four-function calculator.

Related

Within Android code, what suggests the characteristics a program is expecting from a variable?

I know if I run a program, it will likely express one way or another: what it is expecting from each variable. But I would like to determine on my own when I read over each page of Android code etc. e.g:
How could I determine what size or length an android program is expecting a string array to be?
Whether an integer or double, is expected to be positive or negative?
etc.
Help in this regard would be much appreciated.
You can set breakpoints in your code and examine all of the variables when the program pauses. This would give you a general idea of whether the integers were positive or negative, the length and content of the strings, etc. It could be useful if the code was poorly documented.
Assuming you are using Android Studio you can follow this guide:
https://developer.android.com/tools/debugging/debugging-studio.html

Multiple arithimetic operator store

I have been thinking to solve any problem like 1+2*4-5 with user entering it and program to solve it. I've read some questions on this site about storing arithmetic operator and the solution says to check by using switch which can't be applied here. I would be thankful if anybody could suggest any idea of how to make it.
I had a similar exercise not long ago, but in the question it was stated that the seperation is a space. So the user input would be 1 + 2 * 4 - 5, and i solved it that way. I will give you some tips but not paste the whole code.
-you read the input as a String
-you can use the String.split() method to devide the String into the pieces you need and they will be put in an array.(in this case: strArray[0]='1',strArray[1]='+', etc)
-you will need a for-loop to go trough every String in the array:
-the decimals will need to be converted to integers with the Integer.parseInt() method.
-The + - * / will need to be put in switch-statement.
(be careful how you construct your loop, think about how many times you want to go trough it and what you need in each loop)
I hope these tips helped.

Java - Determine event sequence - AB or BA

this one might sound trivial question but I am not able to arrive at a proper solution. Request for help.
Problem : In my code, I get 2 events (one after the other) in a sequence. The sequence could be "event A" first then "event B" or vice versa. If my event sequence is AB then I need to take some action and similarly if my sequence is BA, then I need to take another action.
However, I am finding it tough to determine the sequence and set a boolean value.
One solution I have thought of is to use stack. Both these events will be pushed to stack. I will check if my stack size is 2, then I will pop the last value and see which event it is. If it is A, then i will add logic for BA and if it is B, then I will add logic for AB. Once it is done I will clear my stack.
Is there a simpler way to do it? Please advice
If it is Synchronous and you only can have these to possibilities, you only need one boolean (hasOtherArrived) to do it:
When Event arrives, check if hasOtherArrived is true: if it is execute needed code (depending on which event arrived) and set hasOtherArrived to false. Otherwise set hasOtherArrived to true.
A little schema for AB (the BA case is similar)
use a counter . set its value =2;
1. in onreceiveEventA : decerement the counter
if(counter==0){
// you know that event sequence is BA
}
2. in onreceiveEventB :decrement the counter
if(counter==0){
// you know that event sequence is AB
}
Since, I do not know exactly how you are getting your input or your use-case, it is not that clear to me what is better for you. here is what I am thinking about:
You can use queue instead of stack. Since queue follow FIFO style First In First Out. So, no need to reverse the selection like what you are thinking about in Stack
If you are sure there is only two option then you can define two variable. for example choice1 and choice2 where you are storing the first input in choice1 and the second input in choice2.
Note: this solution will not work if there is more than one choice.

Handling an exception where more or less than 4 characters is entered

I'm writing a Mastermind program where it takes input for the guess, but I need to make sure that it only takes 4 characters of input. So if someone entered anything other than 4 characters, it would prompt for reentry. I know this isn't hard at all, I'm just drawing a blank and haven't been able to find an answer on here anywhere.
Lets try to do it, one step at a time.
Get user input in your program. If it's standard input, one way to pull it is with System.in, which is an InputStream.
Store the input in an intermediate variable. The type of this variable can be String.
If needed, cast the value to a type which is the most relevant to your application's requirement. Before that check input for bad values like null.
Perform the logic on the input, which in your case is finding out whether the length of the input is 4.
Prompt again for input if the current one doesn't meet the requirement. One way to do it is to put your relevant code in a loop which terminates only when you get the right input.
And if that doesn't work, you're most welcome to ask again including code that shows your effort.

While loop doesn't work in Android but works in a normal Java class?

Hi I am having a problem with my while loop in the getAmatch() function
It doesn't enter the while loop in android but enters in a normal Java class.
All I can say off the top of my head without a bit more information is watch your case sensitivity on both input and patterns, and your while loop is a good piece of evidence; your matcher.find() is simply not finding a match. As it states in the documentation:
Parameters:
start: The index in the input at which
the find operation is to begin. If
this is less than the start of the
region, it is automatically adjusted
to that value. If it is beyond the end
of the region, the method will fail.
Returns:
true if (and only if)** a match has been found.

Categories