Programming 12
Programming 12
Programming 12
Department of Education
N a t i o n a l C a pi t a l Re g i o n
Sc h o o l s D i v i s i o n O f f i c e o f La s Pi ñ a s C i t y
t OBJECTIVES
Directions: Read each item carefully and choose the correct answer.
2
4. Which of the following control expressions are valid for an if statement?
A. a Boolean expression C. either A or B
B. an integer expression D. neither A nor B
SELECTION/DECISION CONSTRUCTS
THE IF STATEMENT
if (condition)
statement;
3
Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
Expression Meaning
x>y Is x greater than y?
x<y Is x less than y?
x >= y Is x greater than or equal to y?
x <= y Is x less than or equal to y?
x == y Is x equal to y?
x != y Is x not equal to y?
The following program asks the user to enter grades of a student for two
semesters and calculates the average grade. If the grade is greater than or equal
to 90, the program displays “With Honors! Congratulations!”
4
// get the first sem grade.
System.out.print("Enter first sem grade: ");
grade1 = input.nextDouble();
Program Output 1:
Program Output 2:
5
Flowchart of an if statement
True
average
>= 90
Sample if statements
Statement Result
if (sales > 10000) if sales is more than 10000, the
commission = 1000; commission variable is assigned
with 1000.
The if-else statement is the if statement with an added else clause. It works
the same way as the if statement except that, when the condition is false, the
statement within the else clause executes. The syntax for the if-else statement
is:
6
if (condition)
statement;
else
statement;
Division y zero is undefined. The following program asks the user to enter
two numbers, dividend and divisor. The program will display an error message
“Division by zero is undefined!” if divisor is equal to zero else the program will
display the quotient.
7
System.out.print("The quotient of " + xnumber);
System.out.print(" and " + ynumber);
System.out.println(" is " + quotient);
}
}
}
Program Output 1:
Program Output 2:
True
divisor
!= 0
Display
“Division by
zero is
undefined!”
8
POST-TEST Multiple Choice (10 points)
Direction: Read each item carefully and choose the correct answer.
4. Which of the following you enclose the statements with, when creating a
block of statements?
A. angled brackets < > C. parentheses ( )
B. braces { } D. square brackets [ ]
9
NAME: _________________________________________ DATE: ________________
OBJECTIVES
Direction: Read each item carefully and choose the correct answer.
2. What decision structure works best if you have more than two possible
answers (multiple choice) to a question, and those answers can be
integer or character based?
A. an if statement C. a switch statement
B. an if-else statement D. nested-if statements
10
4. Which of these are selection statements in Java?
A. break C. for( )
B. continue D. if( )
NESTED IF STATEMENTS
True
False
residency Display “Residency should
>= 3 be at least 3 years to
qualify.”
True
11
A Java program that demonstrates a nested-if statement.
import java.util.Scanner;
/**
* This program demonstrates a nested if statement.
*/
12
}
else
{
System.out.println("Salary should be at least "
+ "15,000 a month to qualify.");
}
}
}
Program Output 1:
Program Output 2:
Program Output 3:
if(condition1)
{
//block of code to be executed if condition1 is true.
}
13
else if(condition2)
{
//block of code to be executed if condition1 is false and condition2
is true
}
else
{
//block of code to be executed if condition1 is false and condition2
is false
}
IF-ELSE-IF PROBLEM
average
YES
average “Distinction.”
>= 95
NO
YES
average “Merit.”
>= 90
NO
YES
average “Completer.”
>= 75
NO
“Incomplete.”
14
The problem demonstrates the if-else-if statement. The above algorithm asks
the user to enter final average grade of a student and displays the equivalent
worded grade.
import java.util.Scanner;
else
System.out.println("Incomplete.");
}
}
15
Program Output 1:
Program Output 2:
import java.util.Scanner;
if(number > 0)
{
System.out.println("Number is positive.");
}
else
{
System.out.println("Number is zero.");
}
}
}
16
Program Output 1:
Enter a number: -4
Number is negative.
Program Output 2:
Enter a number: 0
Number is zero.
Direction: Read each item carefully and choose the correct answer.
17
NAME: _________________________________________ DATE: ________________
OBJECTIVES
Direction: Read each item carefully and choose the correct answer.
18
4. Which results in true if both conditions are true?
A. AND C. OR
B. NOT D. NOR
LOGICAL OPERATORS
Logical Operators
Operator Meaning Effect
&& AND Combines two boolean expressions into one
compound expression. Both boolean expressions
must be true to have a true result.
|| OR Combines two boolean expressions. To have a true
result, any of the two boolean expressions must be
true.
! NOT The ! “not” reverses the truth of the boolean
expression.
The && Operator is called the logical AND. It combines two boolean
expressions and will only have a true result if both boolean expressions are
true. Here is an example of an if statement that uses && logical AND operator:
19
if (temperature < -10 && minutes > 15)
{
System.out.println("The temperature is in the danger zone!");
}
Sample Problem:
The program will process a salary loan application if the applicant satisfies
BOTH conditions: 1.) salary of an applicant is at least 15,000 per month; AND
2.) residency of the applicant is at least 3 years in the company.
import java.util.Scanner;
20
residency = input.nextDouble();
Program Output 1:
Program Output 2:
Program Output 3:
21
if (temperature < -10 || temperature > 45)
{
System.out.println("The temperature is in the danger zone!");
}
Sample Problem:
The program will process a salary loan application if the applicant satisfies
ANY of the two conditions: 1.) salary of an applicant is at least 15,000 per month;
OR 2.) residency of the applicant is at least 3 years in the company.
import java.util.Scanner;
22
// Determine whether the user qualifies for the loan.
if (salary >= 15000 || residency >= 3)
{
System.out.println("You qualify for a salary loan!");
}
else
{
System.out.println("You do not qualify for a salary loan!");
}
}
}
Program Output 1:
Program Output 2:
Program Output 3:
The ! Operator is called the logical NOT. The NOT reverses the truth of the
boolean expression. If the expression is true, the ! operator returns false. If the
expression is false, the ! operator returns true. It is a unary operator that
reverses the logical value of a boolean expression which is its operand. Here is
an example of an if statement that uses ! logical NOT operator:
23
if (!(temperature > 45))
System.out.println("This is below the highest temperature.");
24
POST-TEST MULTIPLE CHOICE (10 points)
Direction: Read each item carefully and choose the correct answer.
1. Which Java logical operator checks either values are true, but not both?
A. AND C. NOT
B. OR D. XOR
25
NAME: _________________________________________ DATE: ________________
OBJECTIVES
Direction: Read each item carefully and choose the correct answer.
26
5. What type of data a switch statement accepts as input?
A. byte C. short
B. int D. all of the above
The switch statement is like the if-else-if combination for processing one of a
set of alternatives. Use the switch statement to use one of many code blocks to
be executed.
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.
The default keyword specifies some code to run if there is no case match. It is
used at the last statement in a switch block.
switch (expression)
{
case x:
//code block
break;
case y:
//code block
break;
default:
//code block
}
27
A Java program that demonstrates how Switch Statement works.
Program Output 1:
Enter A, B or C: A
You entered A.
28
Program Output 2:
Enter A, B or C: E
That is not A, B, or C!
import java.util.Scanner;
Program Output 1:
Program Output 2:
import java.util.Scanner;
30
default:
System.out.println("Please enter one of these words:\n"
+ "friday, saturday, sunday, monday");
}
}
}
RANDOM NUMBERS
There are many applications that feature random numbers generation such
as Statistics applications that generates random numbers for sample data
analysis. Java provides ways to generate random numbers using some built-in
methods and classes such as: java.util.Random class; Math.random method is
used to return a pseudorandom double type number greater than or equal to 0.0
and less than 1.0. The default random number always generated between 0 and
1.
The class Random is part of the java.util package, so that any program that
uses it will need an import statement such as: import java.util.Random;
This declares a variable named randomNumbers from the Random class. The
new Random() creates an instance of the Random class and the equal sign
assigns the address of the Random class to the randomNumbers variable. After
the statement executes, the randomNumbers variable references a Random
object and may now invoke methods such as nextInt(), nextDouble(),
nextLong() using that instance.
31
// A Java program to demonstrate random number generation
// using java.util.Random;
import java.util.Random;
32
3. What is the return type of Math.random() method?
A. boolean C. integer
B. double D. String
5. D 5. A 5. C 5. C 5. C 5. B 5. C 5. A
4. D 4. D 4. A 4. B 4. D 4. C 4. A 4. B
3. A 3. B 3. D 3. A 3. C 3. C 3. A 3. C
2. B 2. A 2. A 2. D 2. C 2. D 2. C 2. A
1. C 1. C 1. C 1. B 1. A 1. D 1. A 1. B
Pretest Posttest Pretest Posttest Pretest Posttest Pretest Posttest
Week4 Week3 Week2 Week1
REFERENCES:
33