Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

java code #554

tannie2906 started this conversation in General
Discussion options

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.

  1. Evaluating simple arithmetical expressions
  2. The calculator can evaluate expressions of the following form: operand[space]operator[space]operand
    For example, 3 + 4, 2 * 10, 10 / 5, 39 - 47.
  3. 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.
  4. 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.
  5. 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;

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /