Question

In: Computer Science

Consider a system which has to accept prefix arithmetic expressions consisting of ‘+’(binary addition) , ‘-‘(binary...

Consider a system which has to accept prefix arithmetic expressions consisting of ‘+’(binary addition) , ‘-‘(binary subtraction), ‘*’(multiplication), ‘~’(unary subtraction), single digit numbers and compute their value.

Examples:

  1. +12 → 3
  2. ++12 → fail
  3. +*123 → 5
  4. *+123 → 9
  5. +123 → fail

What parts would be required for such a system?

How do these parts relate to the concepts studied?

Could you write a program to implement such a system?

(Layout of the structure and the logic of the system, what are the components of such a system? Justification the possibility(or lack there of) of implementing such a system.)

Solutions

Expert Solution

Unary Operators:

The Unary Operators need a single operand. For instance, operations like incrementing/decrementing a value by one, negating an expression, or inverting the value of a Boolean.

Following are the Unary Operators supported in Java:

Operator

Description

+

Unary plus Operator; indicates a positive value

-

Unary minus Operator; negates an expression

++

Increment Operator; increments a value by 1

--

Decrement Operator; decrements a value by 1

!

Logical complement Operator; inverts the value of a Boolean

Java sample Program for Unary operations:

=====================================================

public class UnaryOperator

{

public static void main(String[] args) {

    int result_value = +10; // indicates positive value 10

    System.out.println(result_value);

    result_value --; // decrements the value of 10 by 1

    System.out.println(result_value);

    result_value ++; // increments the value of 9 by 1

    System.out.println(result_value);

    result_value = - result_value;// this minus operator negates an expression

    System.out.println(result_value);

    booleanisPass = false;

    System.out.println(isPass);

    System.out.println(!isPass);//o/p is inverted isPass value i.e. true

    }

}

Output:

Prefix and Postfix:

The increment/decrement operators are used before (prefix) or after (postfix) the operand. Even though, both the values will return the original value being incremented/decremented by one. The only difference is the prefix operator evaluates the operand to the incremented value, whereas the postfix version evaluates the operand to the original value.

Sample java program for prefix and postfix:

=======================================================

public class PreAndPost

{

        public static void main(String[] args)

{

        int a = 5;

            System.out.println(a++); // output is 5  

            System.out.println(a); // output is 6       

            System.out.println(++a); // output is 7

            System.out.println(a++); // output is 7

            System.out.println(a); // output is 8

        }

}

Output:

Operator

Name

Type

!

Logical NOT

Unary

&

Address-of

Unary

( )

Cast Operator

Unary

*

Pointer dereference

Unary

+

Unary Plus

Unary

++

Increment

Unary

Unary negation

Unary

––

Decrement 1

Unary

~

complement

Unary

,

Comma

Binary

!=

Inequality

Binary

%

Modulus

Binary

%=

Modulus assignment

Binary

&

Bitwise AND

Binary

&&

Logical AND

Binary

&=

Bitwise AND assignment

Binary

*

Multiplication

Binary

*=

Multiplication assignment

Binary

+

Addition

Binary

+=

Addition assignment

Binary

Subtraction

Binary

–=

Subtraction assignment

Binary

–>

Member selection

Binary

–>*

Pointer-to-member selection

Binary

/

Division

Binary

/=

Division assignment

Binary

<

Less than

Binary

<<

Left shift

Binary

<<=

Left shift assignment

Binary

<=

Less than or equal to

Binary

=

Assignment

Binary

==

Equality

Binary

>

Greater than

Binary

>=

Greater than or equal to

Binary

>>

Right shift

Binary

>>=

Right shift assignment

Binary

^

Exclusive OR

Binary

^=

Exclusive OR assignment

Binary

|

Bitwise inclusive OR

Binary

|=

Bitwise inclusive OR assignment

Binary

||

Logical OR

Binary


Related Solutions

Give regular expressions for (c) C indentifiers (d) Binary strings consisting of either an odd number...
Give regular expressions for (c) C indentifiers (d) Binary strings consisting of either an odd number of 1s or an odd number of 0s
Problem 2.2 Consider a production system consisting of three Bernoulli machines and a controller, which also...
Problem 2.2 Consider a production system consisting of three Bernoulli machines and a controller, which also obeys the Bernoulli reliability model. This production system is considered up if the controller and at least two machines are up. During each cycle, the controller is up with probability 0.8 and each machine is up with probability 0.9. The controller and the machines fail independently. (a) Calculate the probability that the production system is up. (b) Assume that a second controller, identical to...
The heart of a computer system is an ARITHMETIC AND LOGIC UNIT (ALU) which is responsible...
The heart of a computer system is an ARITHMETIC AND LOGIC UNIT (ALU) which is responsible for executing arithmetic (e.g., addition, subtraction, etc.) and logic (e.g., AND, OR, etc.) operations. In this project, you will design, simulate, and implement a simple 1-bit ALU. The truth table for ALU is given below: Operation Selection Inputs Operation Output(s) S0 S1 A B 0 0 A AND B AB 0 1 A NOR B (A+B)’ 1 0 A XNOR B (A⊕B)’ 1 1...
1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two...
1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two positive binary number in string and perform the addition with Lab05.2 function enhance in a way can accept binary string in addition to decimal string (use input parameter to control the base2 or base10 addition) and output the as binary string. (Optional: Demonstrate 8 bits and 16 bits in length as inputs.) // the example function prototype for addition function below where accepting two...
Consider a home theater system consisting of a television set, a receiver, a DVD player, and...
Consider a home theater system consisting of a television set, a receiver, a DVD player, and speakers. Draw a system diagram for this system. Include both components and links.What are the inputs to this system? What are the outputs? (Remember that the DVD player and receiver are both components within the system). Now draw a system diagram for the receiver subsystem. Include both its primary components and the links between them. What are the inputs and outputs for the receiver...
Consider a system consisting of a cylinder with a movable piston containing 106 gas molecules at...
Consider a system consisting of a cylinder with a movable piston containing 106 gas molecules at 298 K at a volume of 1 L. Consider the following descriptions of this system: A. Initial system as described above. B. Starting from the initial system, the volume of the container is changed to 2 L and the temperature to 395 K. C. Starting from the initial system, a combination reaction occurs at constant volume and temperature. D. Starting from the initial system,...
Consider a portfolio investment consisting of 40% which is 0,4 invested in MTN, 60% which is...
Consider a portfolio investment consisting of 40% which is 0,4 invested in MTN, 60% which is 0,6 invested in Multichoice Expected return calculated as MTN =-0,002 Multichoice= 0,0033 Expected Portfolio Return =0.00118 3.2 Calculate the covariance of the portfolio    3.3 Calculate the variance of the portfolio and standard deviation of the portfolio                                                  3.4 Given that the risk free rate is 0.0002. Calculate the Sharpe ratio for the portfolio                                           3.5 Interpret the Sharpe ratio calculated in 3.4                                                                                                              
Consider a buffer system consisting of Benzoic acid (pKa = 4.20) and sodium benzoate, a) Would...
Consider a buffer system consisting of Benzoic acid (pKa = 4.20) and sodium benzoate, a) Would this buffer system be an appropriate choice to maintain a pH of 8.5? ues or no b) To produce a buffer with a pH = 4.55, what mass of sodium benzoate (FM = 104.10) must be dissolved in 500 mL of 0.65 M benzoic acid? c) What is the resulting pH if 45-mL of 0.08-M HCl is added to 250 mL your buffer?
Consider a system consisting of 4 resources of the same type that are shared among 3 processes, each of which needs at most 2 resources.
Consider a system consisting of 4 resources of the same type that are shared among 3 processes, each of which needs at most 2 resources. Show that the system is deadlock-free. Hint: Show that the condition ((claim-allocated) <= available) can always be satisfied.
If a solar panel system has an increasing annual maintenance cost (an arithmetic gradient) of $500...
If a solar panel system has an increasing annual maintenance cost (an arithmetic gradient) of $500 per year over an 8 year period, then what is the present value based on 10% interest? If a solar panel system has an increasing annual maintenance cost (an arithmetic gradient) of $500 per year over an 8 year period THAT STARTS AT YEAR 4, then what is the present value based on 10% interest? To be clear: the total period is 12 years...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT