Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write the Java code for an arbitrary expression evaluator (supporting _, -, *, /). The - and / operator only works on two operands, the others can have any number or operands. The / operator will additionally check that there is no 0 in the second operand. If it does, then it will throw a BadArithmeticException.
Write the code using the Composite pattern. Also write a client class that will create objects and calculate expressions to demonstrate the use of the composite pattern. The common method in the composite hierarchy is called eval. Here is the signature for eval.
public int eval() throws BadArithmeticException { ...
Not really sure where to begin here. Any help would be appreciated.
This isn't really a question but I can offer a suggestion at least. I would start by trying to write your exception so that you understand what you should be doing. You can find this in the Oracle docs here:
http://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html
If you know that you can't divide by 0, think about what kinds of expressions a user (or you) could input that would cause a BadArithmeticException.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently learning Java, and trying to parse a maths equation for valid inputs:
For example, the user has to input integers in the form:
Operand Operator Operand
in that specific order, and the program would then need to be able to tell if the inputs were in that form, and then work out the simple equation.
An example would be:
4 * 8 which the result would give as 32
the program would also reject something like 45.6 * 0.3, or 45 + 3 / 4
For this to work, do I have to use regular expression, or some other method of if loops?
You can use RegEx to on one hand, get rid of the invalid input, and on the other hand make use of the groups to extract the operands and operation and apply the math
/^(\\d+)\\s*([+\\-*\\/])\\s*(\\d+)$/
Edit:
As Rory Daulton pointed out, the signed integers are excluded from the above RegEx, so the below one should be used instead
/^([+\\-]?\\d+)\\s*([+\\-*\\/])\\s*([+\\-]?\\d+)$/
Take look that:
https://stackoverflow.com/a/11009403/8701820
^([-+/*]\d+(\.\d+)?)*
Also you can check this website: https://regex101.com
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Why doesn't java.lang.Short (or Float or Double) class have a reverse method similar to java.lang.Integer ?
They both do have reverseBytes method though.
Why isn't the API list consistent ?
short can hold 2 bytes and it would have made sense to have a reverse method as well.
Wouldn't it ?
Thanks
While I agree on the API criticism, it's pretty simple to emulate:
short input = ...;
short reversed = (short)(Integer.reverse(input) >> 16);
So maybe the answer is:
Not enough people felt it was necessary
It's easy enough to simulate
Someone wanted to show off with the implementation of Integer.reverse()
Every line of code needs to maintained. Less code == less bugs, lower cost, easier maintenance.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?
With 'casting to string method', I mean:
String.valueOf(integer);
As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.
Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()