-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
java code #554
-
hai anyone please help me, I can't think anymore
Question 2: [40 marks]
CalculatorMain is provided to you, however you need to create the other class, namely Calculator
class yourself. For this exercise you will create a Calculator class and add some basic functionality
to it. The Basic Calculator is able to evaluate simple arithmetical expressions involving integer
operands and the operators: * , / , + , -.
The functionality of the basic calculator is as follows.
- Evaluating simple arithmetical expressions
- The calculator can evaluate expressions of the following form: operand[space]operator[space]operand
For example, 3 + 4, 2 * 10, 10 / 5, 39 - 47. - Thus, the operands are any valid integer and the operator is one of * , / , + or -. There is a
space character between each operand and the operator. - For the Basic Calculator, if the user enters an expression (as a String) that is not precisely of
the form shown above then the program should return a boolean value. - If the user enters an expression involving a ‘divide-by-zero’ error then the program should
also output a boolean value in that case too.
Calculator class: [40 marks]
(a) You must create a class called Calculator;
(b) The mandatory attributes are operator (char), operand1 and operand2 (int) and expression
(String);
(c) The constructor:public Calculator() is the default Constructor;
(d) The getter() methods: getter() for each mandatory attribute;
(e) The setExpression() method: pass the expression, i.e. the arithmetic expression to be
evaluated) to the method, and assign it to the class attribute;
(f) The verify() method: the header is as follows - public boolean verify()
i. This method determines whether an expression is a valid expression or not and
returns either true or false;
ii. You may call another method from this method or you may find you don’t need to;
iii. You need to test whether there is a valid operator, a space either side of the operator,
that both operands are valid integer numbers (only integers are used for this
application), and that there is no division by zero;
iv. Only if all tests pass can a true be returned, otherwise the expression cannot be
evaluated as it is not a valid expression;
v. Your operator as well as both operands MUST be assigned / determined as part of
this method;
The evaluate() method: the header is as follows - public int evaluate()
(a) This method evaluates the expression and returns the result as a int;
Beta Was this translation helpful? Give feedback.