Friday, November 18, 2011

Java Programming Fundamentals: Part 2

by eturo


Java Operators

Java provides a rich set of operators to manipulate variables. There are different types of operators. There are arithmetic operators, relational operators, logical operators and conditional operators. These operators follow a certain kind of precedence so that the compiler will know which operator to evaluate first in case multiple operators are used in one statement.


a  1. Arithmetic Operators
        are used in mathematical expressions in the same way that they are used in algebra.

a.1. Increment and Decrement Operators
·         Aside from the basic arithmetic operators, Java also includes a unary increment operator (++) and unary decrement operator (--). Increment and decrement operators increase and decrease a value stored in a number variable by 1.
·         For example, the expression,
count = count + 1; //increment the value of count by 1
·         is equivalent to:
count++;

·         The increment and decrement operators can be placed before or after an operand.
·         When used before an operand, it causes the variable to be incremented or decremented by 1, and then the new value is used in the expression in which it appears. For example:
int i = 10,
int j = 3;
int k = 0;
k = ++j + i; //will result to k = 4+10 = 14

·         When the increment and decrement operators are placed after the operand, the old value of the variable will be used in the expression where it appears. For example:
int i = 10,
int j = 3;
int k = 0;
k = j++ + i; //will result to k = 3+10 = 13

b  2. Relational Operators
o   compare two values and determines the relationship between those values
o   the output of evaluation are the boolean values true or false.
                                                      

c  3. Logical Operators
        have one or two boolean operands that yield a boolean result
        There are six logical operators: && (logical AND), & (boolean logical AND), || (logical OR), |(boolean logical inclusive OR), ^ (boolean logical exclusive OR), and ! (logical NOT).
        The basic expression for a logical operation is:
x1 op x2
        where x1, x2 can be boolean expressions, variables or constants, and op is either &&, &, ||, |or ^ operator

c.1. Logical AND (&&) and Boolean Logical AND(&)
 
c.2. Logical OR (||) and Boolean Logical OR (|)


c.3. Boolean Logical Exclusive OR(^) and Logical NOT (!)




d  4. Conditional Operator
        the conditional operator (?:) is a ternary operator
o   this means that it takes in three arguments that together form a conditional expression
        the structure of an expression using a conditional operator is,
exp1?exp2:exp3
        wherein exp1 is a boolean expression whose result must either be true or false
        if exp1 is true, exp2 is the value returned. If it is false, then exp3 is returned.
        For example, given the code:

public class ConditionalOperator
{
public static void main(String[] args){
String status = "";
int grade = 80;

//get status of the student
status = (grade >= 60)?"Passed":"Fail";

//print status
System.out.println(status);
}
}
        The output of this program will be:
Passed


        Here is another program that uses the ?: operator:
class ConditionalOperator
{
public static void main(String[] args){
int score = 0;
char answer = 'a';
score = (answer == 'a') ? 10 : 0;
System.out.println("Score = " + score );
}
}
        The output of the program is:
Score = 10
Operator Precedence
        it defines the compiler’s order of evaluation of operators so as to come up with an unambiguous result

        Given a complicated expression:
6%2*5+4/2+88-10
        we can re-write the expression and place some parenthesis base on operator precedence:
((6%2)*5)+(4/2)+88-10

TRY THIS: Sample Exercises


1  A. Declaring and printing variables
Given the table below, declare the following variables with the corresponding data types and initialization values. Output to the screen the variable names together with the values.




The following should be the expected screen output:
The number is 10.
My letter is ‘a’.
The result is true.
My string is “hello”.

2  B. Getting the average of three numbers
Create a program that outputs the average of three numbers. Let the values of the three numbers be, 10, 20 and 45. The expected screen output is,
number 1 = 10
number 2 = 20
number 3 = 45
Average is = 25

3. Given three numbers, write a program that outputs the number with the greatest value among the three. Use the conditional ?: operator that we have studied so far (HINT: You will need to use two sets of ?: to solve this). For example, given the numbers 10, 23 and 5, your program should output,
number 1 = 10
number 2 = 23
number 3 = 5
The highest number is = 23


Happy Coding guys! Happy Programming!

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...