In: Computer Science
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:
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.)
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 |