ComputerProgramming.grade11.Q3 w4 for Student
ComputerProgramming.grade11.Q3 w4 for Student
Republic Act 8293, section 176 states that: No copyright shall subsist in any
work of the Government of the Philippines. However, prior approval of the government
agency or office wherein the work is created shall be necessary for exploitation of such
work for profit. Such agency or office may, among other things, impose as a condition
the payment of royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this activity sheet are owned by their respective copyright
holders. Every effort has been exerted to locate and seek permission to use these
materials from their respective copyright owners. The publisher and authors do not
represent nor claim ownership over them.
Objectives: (TLE_ICTJAVA11-12POAD-IIf-i-29)
• Demonstrate working with java data types in accordance with Java Framework;
• Demonstrate using operator and decision constructs in accordance with Java
Framework; and
• Create program that applies conditional statement (if, if-else, ladderized if, switch
case) applying API/GUI in the programs created.
Materials:
• Computer
• NetBeans IDE
If you were to create a program that computes for the General Average of 5
subjects as shown above and will give Remarks such as “With Highest Honors”, “With
High Honors”, “With Honors”, “Passed”, or “Failed”, how will you do it?
7|Q3 W4
Let’s ponder:
o How to work with Java data types?
o How to use operator and decision constructs in Java?
o How to apply conditional statement (if, if-else, ladderized if, switch case)
using API/GUI in Java?
As explained in the previous lessons, a variable in Java must be a specified data type. You
must know what data types to use when you start coding.
Example:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D’; // Character
boolean myBool = true; // Boolean
String myText = "Hello”; // String
• Primitive data types - A primitive data type specifies the size and type of variable
values, and it has no additional methods. It
includes byte, short, int, long, float, double, boolean and char.
• Non-primitive data types - Non-primitive data types are called reference
types because they refer to objects. Examples of non-primitive types
are Strings, Arrays, Classes, Interface, etc. You will learn more about these in a later
lesson.
Java Decision Making
Let’s review about Java decision-making statements which allows you to make a decision,
based upon the result of a condition.
if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition
is true.
Syntax: if (condition) {
// block of code to be executed if the condition is
true
}
7|Q3 W4
Note: that if is in lowercase letters. Uppercase letters (If or IF) will generate a
JavaScript error.
else Statement
Use the else statement to specify a block of code to be executed if the condition is
false.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax:
if (condition1) {
// 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
}
switch Statement
Use the switch statement to select one of many code blocks to be executed.
Syntax:
Note:
switch(expression) { ✓ Java switch statements can take
case x: input only as integers or
// code block characters.
✓ The switch can only check for
break;
equality. This means that the
case y: other relational operators such
// code block as greater than are rendered
break; unusable in a case.
default: ✓ The break statement is used to
// code block stop current iteration of loop or
} end Switch-case block.
7|Q3 W4
Now do it!
ACTIVITY 3-4
Instructions: Given the project specifications below, let’s create a program design using
NetBeans IDE using (1) if-else statement AND (2) switch statement.
Sample Output
english = Double.parseDouble(txtEnglish.getText());
math = Double.parseDouble(txtMath.getText());
science = Double.parseDouble(txtScience.getText());
history = Double.parseDouble(txtHistory.getText());
TLE = Double.parseDouble(txtTLE.getText());
average = (english + math + science + history + TLE) / 5;
7|Q3 W4
switch sample code:
private void btnComputeActionPerformed(java.awt.event.ActionEvent evt) {
double english, math, science, history, TLE, average;
int value=0;
english = Double.parseDouble(txtEnglish.getText());
math = Double.parseDouble(txtMath.getText());
science = Double.parseDouble(txtScience.getText());
history = Double.parseDouble(txtHistory.getText());
TLE = Double.parseDouble(txtTLE.getText());
average = (english + math + science + history + TLE) / 5;
value = (int)average;
switch(value){
case(100):
case(99):
case(98):
txtRemarks.setText("With Highest Honors");
break;
case(97):
case(96):
case(95):
txtRemarks.setText("With High Honors");
break;
case(94):
case(93):
case(92):
case(91):
case(90):
txtRemarks.setText("With Honors");
break;
case(89):
case(88):
case(87):
case(86):
case(85):
case(84):
case(83):
case(82):
case(81):
case(80):
case(79):
case(78):
case(77):
case(76):
case(75):
txtRemarks.setText("Passed");
break;
default:
txtRemarks.setText("Failed");
break;
}
txtAverage.setText(Double.toString(average));
}
7|Q3 W4
Ace it!
ASSESSMENT
Name: ________________________________________________ Date: _______________
Grade and Section: _____________________________________ Score: ______________
II. PROGRAMMING
2) Print "1" if x is equal to y, print "2" if x is greater than y, otherwise print "3".
int x = 50;
int y = 50;
(x y) {
System.out.println("1");
} (x > y) {
System.out.println("2");
} {
System.out.println("3");
}
3) Create a switch statement that will alert "Hello" if fruits is "banana", and
"Welcome" if fruits is "apple".
(fruits) {
"Banana":
alert("Hello")
break;
"Apple":
alert("Welcome")
break;
}