Question

CREATE AND TEST A FLOWCHART IN RAPTOR All our operands will be (float) numbers and for...

CREATE AND TEST A FLOWCHART IN RAPTOR

All our operands will be (float) numbers and for a start, we shall use only two operators:

+ (plus)

and

- (minus)

A sample run of the program would look like this.

Welcome to your friendly neighborhood accumulator! Please input

your expression, one token at a time, starting with an operand and

type in '=' when completed.

3.0

+

2.0

-

-5.0

=

10.0

Thank-you for using your friendly neighborhood accumulator!

The problem is formally described as follows:

Input:

An alternating sequence of operands and operators, given one at a time, starting and

ending with an operand. The sequence is followed by an equals sign, to indicate termination. Each

operand is a oat value and each operator is a plus or a minus.

Output:

The value of the expression defined by the sequence of operands and operators.

We shall assume for now that the user will not make mistakes when using the accumulator, i.e., there is no bad input

(e.g., two consecutive tokens being both operands or both operators, other unexpected characters etc.) Note that the

equals sign acts like a sentinel.

The Strategy.

To initialize the process, the first operand (a number) is read and stored as the

total. Next, an operator (a character) is read and we decide what action to take. The action can

be one of the following:

(i) In case of termination (equals sign) the process is exited and the total is printed;

(ii) otherwise another operand is read and either added to or subtracted from the total depending

on the operator.

Write the sequence of steps that would be carried out when evaluating an expression

using the above strategy. Your answer will look something like this:

read num;

store num in total;

read operator;

...

(extend this following our strategy until you see repetitions)

From the answer to the first question, identify a block of repeating steps. The exit test

should be performed at the start of the block. Create a Flowchart for this on Raptor, and verify

your design. (Note that Raptor does not have the char datatype. Operators are treated as strings:

("+", "-", "=".)

Homework Answers

Answer #1

class ArithmeticOperators
{
   public static void main(String[] args)
   {
       System.out.println(10 + 20 * 2 + 40 / 20 % 2 * 30 - 10);
       /*
       => System.out.println(10 + (20 * 2) + 40 / 20 % 2 * 30 - 10);
       => System.out.println(10 + 40 + (40 / 20) % 2 * 30 - 10);
       => System.out.println(10 + 40 + 2 % 2 * 30 - 10);
       => System.out.println(10 + 40 + (2 % 2) * 30 - 10);
       => System.out.println(10 + 40 + 0 * 30 - 10);
       => System.out.println(10 + 40 + (0 * 30) - 10);
       => System.out.println(10 + 40 + 0 - 10);
       => System.out.println((10 + 40) + 0 - 10);
       => System.out.println((50 + 0) - 10);
       => System.out.println(50 - 10);
       => System.out.println(40);
       */
       //1. we cannot divide integer by ZERO it leads to exception java.lang.ArithmeticException: /by Zero
      
       //System.out.println(10/0); RE: java.lang.ArithmeticException: /by Zero
       //System.out.println(0/0); RE: java.lang.ArithmeticException: /by Zero
       System.out.println(0/10); //0
  
       //but we can divide integer number with 0.0, because internally that integer number will be converted into double type
       System.out.println(10/0.0); //=>       System.out.println(10.0/0.0); Infinity
      
       //2. we can divide floating point numbers (float and double) by zero, output is Infinity or -Infinity
       //3. if we divide 0.0 by 0 output is NaN (Not a Number)

       System.out.println(10.0/0); //Inifinity
       System.out.println(0.0/0); //NaN
       System.out.println(0.0/10); //0.0

       System.out.println(-10.0/0); //-Inifinity
       System.out.println(-0.0/0); //NaN
       System.out.println(-0.0/10); //-0.0

   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions