0% found this document useful (0 votes)
22 views

Week3 CM MDL CP1212

The document discusses operators and conditional statements in Java. It covers: 1) Different types of operators in Java including arithmetic, relational, bitwise, logical, and assignment operators. 2) Examples of using various operators with integer variables. 3) Conditional statements such as if, if-else, nested if-else, switch and break statements in Java.

Uploaded by

aumanjerome45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Week3 CM MDL CP1212

The document discusses operators and conditional statements in Java. It covers: 1) Different types of operators in Java including arithmetic, relational, bitwise, logical, and assignment operators. 2) Examples of using various operators with integer variables. 3) Conditional statements such as if, if-else, nested if-else, switch and break statements in Java.

Uploaded by

aumanjerome45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

MODULE 1: APPLY BASIC OF JAVA LANGUAGE

Week 3
UNIT 1: Demonstrate using Operators and Decision Construct in Accordance with Java
Framework
Most Essential Learning Competencies: At the end of the course, you must be able to:
1. Discuss the different types of operators in Java
2. Demonstrate Java conditional statements such as If, If-Else, Nested If-Else, Switch and
Break Statements and its syntax

Introduction
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into
the following groups:

 Arithmetic Operators
 Relational Operators
 Bitwise Operators
 Logical Operators
 Assignment Operators
 Misc Operators

The Arithmetic Operators


Arithmetic operators are used in mathematical expressions in the same way that they are used in
algebra. The following table lists the arithmetic operators.
Table 3.1
Assume integer variable A holds 10 and variable B holds 20, then −
Operator Description Example

+ (Addition) Adds values on either side of the operator. A + B will give 30

Subtracts right-hand operand from left-hand


- (Subtraction) A - B will give -10
operand.

* Multiplies values on either side of the operator.


A * B will give 200
(Multiplication)

/ (Division) Divides left-hand operand by right-hand operand. B / A will give 2

% (Modulus) Divides left-hand operand by right-hand operand and B % A will give 0

AICS – SHS Department/TVL-ICT Academic Department Page 1 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
returns remainder.

++ (Increment) Increases the value of operand by 1. B++ gives 21

-- (Decrement) Decreases the value of operand by 1. B-- gives 19

The Relational Operators


There are following relational operators supported by Java language.
Table 3.2
Assume variable A holds 10 and variable B holds 20, then
Operator Description Example

Checks if the values of two operands are equal or not, if (A == B) is


== (equal to)
yes then condition becomes true. not true.

Checks if the values of two operands are equal or not, if (A != B) is


!= (not equal to)
values are not equal then condition becomes true. true.

Checks if the value of left operand is greater than the


(A > B) is not
> (greater than) value of right operand, if yes then condition becomes
true.
true.

Checks if the value of left operand is less than the value of (A < B) is
< (less than)
right operand, if yes then condition becomes true. true.

Checks if the value of left operand is greater than or equal


>= (greater than (A >= B) is
to the value of right operand, if yes then condition
or equal to) not true.
becomes true.

Checks if the value of left operand is less than or equal to


<= (less than or (A <= B) is
the value of right operand, if yes then condition becomes
equal to) true.
true.

The Bitwise Operators


Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char,
and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b =
13; now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------

AICS – SHS Department/TVL-ICT Academic Department Page 2 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators −
Assume integer variable A holds 60 and variable B holds 13 then −
Table 3.3
Assume integer variable A holds 60 and variable B holds 13 then −
Operator Description Example

& (bitwise Binary AND Operator copies a bit to the (A & B) will give 12 which is
and) result if it exists in both operands. 0000 1100

Binary OR Operator copies a bit if it exists in (A | B) will give 61 which is


| (bitwise or)
either operand. 0011 1101

^ (bitwise Binary XOR Operator copies the bit if it is (A ^ B) will give 49 which is
XOR) set in one operand but not both. 0011 0001

(~A ) will give -61 which is


~ (bitwise Binary Ones Complement Operator is unary 1100 0011 in 2's
compliment) and has the effect of 'flipping' bits. complement form due to a
signed binary number.

Binary Left Shift Operator. The left


A << 2 will give 240 which is
<< (left shift) operands value is moved left by the number
1111 0000
of bits specified by the right operand.

Binary Right Shift Operator. The left


operands value is moved right by the A >> 2 will give 15 which is
>> (right shift)
number of bits specified by the right 1111
operand.

Shift right zero fill operator. The left


operands value is moved right by the
>>> (zero fill number of bits specified by the right A >>>2 will give 15 which is
right shift) operand and shifted values are filled up 0000 1111
with zeros.

AICS – SHS Department/TVL-ICT Academic Department Page 3 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
The Logical Operators
The following table lists the logical operators −
Table 3.4
Assume Boolean variables A holds true and variable B holds false, then −
Operator Description Example

Called Logical AND operator. If both the


&& (logical and) operands are non-zero, then the condition (A && B) is false
becomes true.

Called Logical OR Operator. If any of the two


|| (logical or) operands are non-zero, then the condition (A || B) is true
becomes true.

Called Logical NOT Operator. Use to reverses


! (logical not) the logical state of its operand. If a condition is !(A && B) is true
true then Logical NOT operator will make false.

The Assignment Operators


Table 3.5
Following are the assignment operators supported by Java language –
Operator Description Example

Simple assignment operator. Assigns values from right side C=A+B


operands to left side operand. will assign
=
value of A
+ B into C

Add AND assignment operator. It adds right operand to the left C += A is


operand and assign the result to left operand. equivalent
+=
to C = C +
A

Subtract AND assignment operator. It subtracts right operand from C -= A is


the left operand and assign the result to left operand. equivalent
-=
to C = C –
A

Multiply AND assignment operator. It multiplies right operand with C *= A is


the left operand and assign the result to left operand. equivalent
*=
to C = C *
A

AICS – SHS Department/TVL-ICT Academic Department Page 4 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
/= Divide AND assignment operator. It divides left operand with the C /= A is
right operand and assign the result to left operand. equivalent
to C = C /
A

Modulus AND assignment operator. It takes modulus using two C %= A is


operands and assign the result to left operand. equivalent
%=
to C = C %
A

C <<= 2 is
<<= Left shift AND assignment operator. same as C
= C << 2

C >>= 2 is
>>= Right shift AND assignment operator. same as C
= C >> 2

C &= 2 is
&= Bitwise AND assignment operator. same as C
=C&2

bitwise exclusive OR and assignment operator. C ^= 2 is


^= same as C
=C^2

bitwise inclusive OR and assignment operator.


C |= 2 is
|= same as C
=C|2

Miscellaneous Operators
There are few other operators supported by Java Language.

Conditional Operator (?: )

Conditional operator is also known as the ternary operator. This operator consists of three operands
and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should
be assigned to the variable. The operator is written as −
variable x = (expression) ? value if true : value if false
Example

Public class Test{

AICS – SHS Department/TVL-ICT Academic Department Page 5 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
public static void main(String args[]){
int a, b;
a =10;
b =(a ==1)?20:30;
System.out.println("Value of b is : "+ b );

b =(a ==10)?20:30;
System.out.println("Value of b is : "+ b );
}
}
This will produce the following result −
Output
Value of b is : 30
Value of b is : 20

instanceof Operator

This operator is used only for object reference variables. The operator checks whether the object is of a
particular type (class type or interface type). Instance of operator is written as −
( Object reference variable ) instanceof (class/interface type)
If the object referred by the variable on the left side of the operator passes the IS-A check for the
class/interface type on the right side, then the result will be true. Following is an example −
Example

public classTest{

public static void main(String args[]){

String name ="James";

// following will return true since name is type of String


boolean result = name instanceof String;
System.out.println( result);
}
}
This will produce the following result −
Output
true
This operator will still return true, if the object being compared is the assignment compatible with the
type on the right. Following is one more example −

AICS – SHS Department/TVL-ICT Academic Department Page 6 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
Example

class Vehicle{}

public class Car extends Vehicle{

public static void main(String args[]){

Vehicle a = newCar();
boolean result = a instanceof Car;
System.out.println( result);
}
}

This will produce the following result −


Output
true

Precedence of Java Operators


Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator −
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than
+, so it first gets multiplied with 3 * 2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Table 3.6
Category Operator Associativity

Postfix expression++ expression-- Left to right

Unary ++expression –-expression +expression –expression ~ ! Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift <<>>>>> Left to right

Relational <><= >= instanceof Left to right

Equality == != Left to right

AICS – SHS Department/TVL-ICT Academic Department Page 7 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %= ^= |= <<= >>= >>>= Right to left

Java Conditions and If Statements


Java supports the usual logical conditions from mathematics:
 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
 Use if to specify a block of code to be executed, if a specified condition is true
 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

The if Statement
Use if statement to specify a block of Java code to be executed if a condition is true.

Figure 3.1

AICS – SHS Department/TVL-ICT Academic Department Page 8 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
Syntax

if(condition){

// block of code to be executed if the condition is true

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true,
print some text:

Example

If (20>18){

System.out.println("20 is greater than 18");

Java Code

public class MyClass{


public static void main(String[]args){
if (20>18){
System.out.println("20 is greater than 18");// obviously
}
}
}
Output:
20 is greater than 18

We can also test variables:

Example
int x =20;
int y =18;
if (x > y){
System.out.println("x is greater than y");
}

AICS – SHS Department/TVL-ICT Academic Department Page 9 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
Java Code:

public class MyClass{


public static void main(String[]args){
int x =20;
int y =18;
if (x > y){
System.out.println("x is greater than y");
}
}
}
Output:
x is greater than y

Example Explained

In the example above we use two variables, x and y, to test whether x is greater than y (using
the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen
that "x is greater than y".

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Figure 3.2

Syntax
if(condition){
// block of code to be executed if the condition is true

AICS – SHS Department/TVL-ICT Academic Department Page 10 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

}else{
// block of code to be executed if the condition is false
}

Example
int time =20;
if (time <18){
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."

Java Code
public class MyClass{
public static void main(String[]args){
int time =20;
if (time <18){
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}

Output:

Good evening.

Example Explained

In the example above, time (20) is greater than 18, so the condition is false. Because of this, we move on
to the else condition and print to the screen "Good evening". If the time was less than 18, the program
would print "Good day".

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if(condition1){

AICS – SHS Department/TVL-ICT Academic Department Page 11 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
// block of code to be executed if condition1 is true

}else if(condition2){

// block of code to be executed if the condition1 is false and condition2 is true

}else{

// block of code to be executed if the condition1 is false and condition2 is false

Example
int time =22;
if (time <10){
System.out.println("Good morning.");
} else if (time <20){
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."

Java Code:
public class MyClass{
public static void main(String[]args){
int time =22;
if (time <10){
System.out.println("Good morning.");
} else if (time <20){
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}

Output:

Good evening

AICS – SHS Department/TVL-ICT Academic Department Page 12 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
Example Explained

In the example above, time (22) is greater than 10, so the first condition is false. The next condition, in
the else if statement, is also false, so we move on to the else condition
since condition1 and condition2 is both false - and print to the screen "Good evening".

However, if the time was 14, our program would print "Good day."

Short Hand If...Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three
operands. It can be used to replace multiple lines of code with a single line. It is often used to replace
simple if else statements:

Syntax

variable=(condition)?expressionTrue:expressionFalse;

Instead of writing:

Example
int time =20;
if (time <18){
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}

Java Code:
public class MyClass{
public static void main(String[]args){
int time =20;
if (time <18){
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}

Output:

Good evening

AICS – SHS Department/TVL-ICT Academic Department Page 13 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
You can simply write:

Example
int time =20;
String result =(time <18)?"Good day.":"Good evening.";
System.out.println(result);

Java Code:
public class MyClass{
public static void main(String[]args){
int time =20;
String result;
result =(time <18)?"Good day.":"Good evening.";
System.out.println(result);
}
}

Output:

Good evening.

Java Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax
switch(expression){
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 The break and default keywords are optional, and will be described later in this chapter

AICS – SHS Department/TVL-ICT Academic Department Page 14 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
The example below uses the weekday number to calculate the weekday name:

Example
int day =4;
switch (day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Outputs "Thursday" (day 4)

Java Code:
public class MyClass{
public static void main(String[]args){
int day =4;
switch (day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");

AICS – SHS Department/TVL-ICT Academic Department Page 15 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}

Output:

Thursday

The break Keyword

 When Java reaches a break keyword, it breaks out of the switch block.
 This will stop the execution of more code and case testing inside the block.
 When a match is found, and the job is done, it's time for a break. There is no need for more
testing.
 A break can save a lot of execution time because it "ignores" the execution of all the rest of the
code in the switch block.

The default Keyword


The default keyword specifies some code to run if there is no case match:

Example
int day = 4;
switch (day){
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");

AICS – SHS Department/TVL-ICT Academic Department Page 16 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
}
// Outputs "Looking forward to the Weekend"

Java Code:
public class MyClass{
public static void main(String[]args){
int day = 4;
switch (day){
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
}
}

Output:

Looking forward to the Weekend

Note that if the default statement is used as the last statement in a switch block, it does not need a
break.

AICS – SHS Department/TVL-ICT Academic Department Page 17 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

1. JAVA Basic Data Types (PDF)

2. JAVA Variable Types (PDF)

1. If Statement
https://www.youtube.com/watch?v=7qLGhGPth3I

2. If-Else Statement
https://www.youtube.com/watch?v=cM-SuD1Hr9o

3. If…Else...If…Else Statement
https://www.youtube.com/watch?v=HlEomyY4KqI

4. Java Switch Statement


https://www.youtube.com/watch?v=ep4ontFhZ4k

5. Java Break Statement


https://www.youtube.com/watch?v=6Bq0ttoCKeM

AICS – SHS Department/TVL-ICT Academic Department Page 18 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

Quiz 3.1

Instructions: Write your answer on the Answer Sheet (AS) provided in this module.

A. Identification. (1-point each)


1. Operators are used to perform operations on variables and values.
2. Arithmetic operators are used to perform common mathematical operations.
3. Operator works on bits and performs bit-by-bit operation
4. && and || and !, in what group of Java Operators
5. What operator symbol that it adds right operand to the left operand and assign the result to left
operand.
6. Write the syntax of if statement.
7. Use the statement to specify a block of code to be executed if the condition is false.
8. Write the syntax of if else statement.
9. Use the statement to specify a new condition if the first condition is false.
10. Write the syntax of else if statement.
11. Use the statement to select one of many code blocks to be executed.
12. Write the syntax of Switch Statement.
13. Can save a lot of execution time because it "ignores" the execution of all the rest of the code in
the switch block.
14. The keyword specifies some code to run if there is no case match.
15. In switch, the value of the expression is compared with the values of each ____.

AICS – SHS Department/TVL-ICT Academic Department Page 19 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

Activity 3.1

Java Conditional Statement: Activity 3.1 with Solution

Problem: Write a Java program to test a number is positive or negative.

Test Data
Input number: 35

Pictorial Presentation:

Figure 3.3
Solution:

Java Code:
import java.util.Scanner;
public class Activity1{
public static void main(String[]args)
{
Scanner in = newScanner(System.in);
System.out.print("Input number: ");
int input =in.nextInt();
if (input >0)
{
System.out.println("Number is positive");

AICS – SHS Department/TVL-ICT Academic Department Page 20 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
}
else if(input <0)
{
System.out.println("Number is negative");
}
else
{
System.out.println("Number is zero");
}
}
}

Output:

Input number: 35
Number is positive

Activity 3.2

Java Nested if..else Statement

In Java, it is also possible to if..else statements inside a if..else statement. It's called

nested if...else statement.

Here's a program to find largest of 3 numbers:

Nested if...else Statement

Class Number{
public static void main(String[] args){

// declaring double type variables


Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;

// checks if n1 is greater than or equal to n2


if (n1 >= n2) {

AICS – SHS Department/TVL-ICT Academic Department Page 21 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

// if...else statement inside the if block


// checks if n1 is greater than or equal to n3
if (n1 >= n3) {
largestNumber = n1;
}

else {
largestNumber = n3;
}
}
else {

// if..else statement inside else block


// checks if n2 is greater than or equal to n3
if (n2 >= n3) {
largestNumber = n2;
}

else {
largestNumber = n3;
}
}

System.out.println("The largest number is " + largestNumber);


}
}

Output:

The largest number is 4.5

Activity 3.3

AICS – SHS Department/TVL-ICT Academic Department Page 22 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

Making Calculator using the switch statement


The program below takes three inputs from the user: one operator and 2 numbers. Based on the
operator provided by the user, it performs the calculation on the numbers. Then the result is displayed
on the screen.

Before you go through the program, make sure you know about Java Scanner to take input from the
user.

import java.util.Scanner;

class Main{
public static void main(String[] args){

char operator;
Double number1, number2, result;

// create an object of Scanner class


Scanner scanner = new Scanner(System.in);
System.out.print("Enter operator (either +, -, * or /): ");

// ask user to enter operator


operator = scanner.next().charAt(0);
System.out.print("Enter number1 and number2 respectively: ");

// ask user to enter numbers


number1 = scanner.nextDouble();
number2 = scanner.nextDouble();

switch (operator) {

// performs addition between numbers


case'+':
result = number1 + number2;
System.out.print(number1 + "+" + number2 + " = " + result);
break;

// performs subtraction between numbers


case'-':
result = number1 - number2;
System.out.print(number1 + "-" + number2 + " = " + result);

AICS – SHS Department/TVL-ICT Academic Department Page 23 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

break;

// performs multiplication between numbers


case'*':
result = number1 * number2;
System.out.print(number1 + "*" + number2 + " = " + result);
break;
// performs division between numbers
case'/':
result = number1 / number2;
System.out.print(number1 + "/" + number2 + " = " + result);
break;

default:
System.out.println("Invalid operator!");
break;
}
}
}

Output:

Enter operator (either +, -, * or /): *


Enter number1 and number2 respectively: 1.4
-5.3
1.4*-5.3 = -7.419999999999999

Activity 3.4

A sequence of six tests all scored out of 100, are to be given different weightings in determining a final
mark. Write a program fragment that computes the appropriate weighted score for one test. The
fragment should first read values of Test Number and Score. Using a switch statement it should compute
and print the appropriate value of Weighted Score using the weightings given in the following table.

Table 3.7

AICS – SHS Department/TVL-ICT Academic Department Page 24 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3
Test Number Weight
1 10%
2 20%
3 20%
4 15%
5 15%
6 29%

For example, input of 3 and 27 should produce the following output: A score of 27 on test 3 gives a
weighted score of 5.4

Activity 3.5

Use If – Else Statement


Problem: Write Java running program to allow the user to input his/her age. Then the program will show
if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years
old.

Sample Output:

Enter your age: 18

You are eligible to vote.

1. Put the running codes on the Activity Sheet provided in this module together with the screen
shots or pictures from your monitor of the output. Save your file with the following format:
<lastname_coursecode_section_Weekno_quarter_ActivitySheetNo.docx>
Example: delacruz_CP1212_ICT2MA_Week3_1stQuarter_ASNo3.docx
* Activity Sheet No. and Week No. refers to the week you are working on
2. Email the file to your teacher.

AICS – SHS Department/TVL-ICT Academic Department Page 25 of 26 /etj


MODULE 1: APPLY BASIC OF JAVA LANGUAGE
Week 3

1. Java Operators
http://www1.fs.cvut.cz/cz/u12110/prt/java/j5.pdf

2. Logical Operator
https://www.youtube.com/watch?v=PAaqgTr7Cx4

3. Java Basic Operators


https://people.cs.clemson.edu/~yfeaste/cpsc215/cpsc2150F15/cpsc215Fall215/Notes/Tutorials
point_webpages/8java_basic_operators.pdf

4. If – Else Statement in Java


https://www.tutorialspoint.com/java/pdf/if_else_statement_in_java.pdf

5. Conditional Statement
https://www.inf.unibz.it/~calvanese/teaching/04-05-ip/lecture-notes/uni05.pdf

6. Java Switch
https://www.programiz.com/java-programming/switch-statement

7. Decisions in Java – Switch Statement


https://stevesweeney.pbworks.com/f/Java+05d+Decisions+in+Java+-+Switch+Statements.pdf

8. If Statement
https://www.youtube.com/watch?v=7qLGhGPth3I
https://www.youtube.com/watch?v=iMeaovDbgkQ

9. If-Else Statement
https://www.youtube.com/watch?v=cM-SuD1Hr9o

10. If…Else…If…Else Statement


https://www.youtube.com/watch?v=HlEomyY4KqI

11. Java Switch Statement


https://www.youtube.com/watch?v=ep4ontFhZ4k

12. Java Break Statement


https://www.youtube.com/watch?v=6Bq0ttoCKeM

13. Java Operators


https://www.youtube.com/watch?v=8CX4Tdttbqk

AICS – SHS Department/TVL-ICT Academic Department Page 26 of 26 /etj

You might also like