Write a program that evaluates an expression.assume the operands are integers and
operators are +, −, *, /.

Please help me.

Recommended Answers

All 5 Replies

Hey still learning myself but the four basic methods you need are

public int add(int num1, int num2)
{
    return num1 + num2;
}

public int subtract(int num1, int num2)
{
    return num1 - num2;
}

public int multiply(int num1, int num2)
{
    return num1 * num2;
}

public int divide(int num1, int num2)
{
    return num1 / num2;
}

ok...But expression will be in string like 12+15/3+1*7...
there may arise some problem

now, actually, that's not as much a problem.
there are several ways you can do this.
the first is to use + - * / = as separators, and use the indexOf and substring method of string objects to get the numerical values.

an other way is to, char by char, extract the contents of the string and make a check eacht time whether or not the char is one of the separators.

what have you done yourself so far? it's better to try and fail, than to copy paste.

Very much thanks.....
I also believe better to try and fail than to copy paste but actually
am beginner in java(specially in such type of problems )..So please sir elaborate little bit more so that i could try and apply the concept myself..

Stutulske is right: your first problem is to work out what the component pieces of the expression are.

please elaborate little bit more

Let's start simple. Suppose you had a String, like
"asddf234fsdf"
and you wanted to extract and print the integer part of that, so the output would be
234

- how would you do that? You don't have to write the program if you can describe how it would work.

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.