Write a static method called evaluate that takes as a parameter a Scanner containing a series of tokens that represent a
Posted: Fri Jun 10, 2022 11:55 am
Write a static method called evaluate that takes as a parameter
a Scanner containing a series of tokens that represent a numeric
expression involving addition and subtraction and that returns the
value of the expression. For example, if a Scanner called data
contains the following tokens:
and we make the following call:
the method should evaluate the result as (4.2+3.4-4.1) =
(7.6-4.1) = 3.5 and should return this value as its result. Every
expression will begin with a real number and then will have a
series of operator/number pairs that follow. The operators will be
either "+" (addition) or "-" (subtraction). As in the example
above, there will be spaces separating numbers and operators. You
may assume the expression is legal.
Your program should evaluate operators sequentially from left to
right. For example, for this expression:
your method should evaluate the operators as follows:
The Scanner might contain just a number, in which case your
method should return that number as its result.
If you could break down your thought process for solving this
that would be great!
a Scanner containing a series of tokens that represent a numeric
expression involving addition and subtraction and that returns the
value of the expression. For example, if a Scanner called data
contains the following tokens:
and we make the following call:
the method should evaluate the result as (4.2+3.4-4.1) =
(7.6-4.1) = 3.5 and should return this value as its result. Every
expression will begin with a real number and then will have a
series of operator/number pairs that follow. The operators will be
either "+" (addition) or "-" (subtraction). As in the example
above, there will be spaces separating numbers and operators. You
may assume the expression is legal.
Your program should evaluate operators sequentially from left to
right. For example, for this expression:
your method should evaluate the operators as follows:
The Scanner might contain just a number, in which case your
method should return that number as its result.
If you could break down your thought process for solving this
that would be great!