SlideShare a Scribd company logo
1 of 31
SWE-103T Object Oriented
Programming
Instructors:
Engr. Anila Saghir
Software Engineering Department
5/22/2022 SWE-103T OOP SED,SSUET 1
Lecture 05
5/22/2022 SWE-103T OOP SED,SSUET 2
Chapter #3
Selections
Chapter 1 introduces how to declare Boolean variables, and write Boolean
expressions using relational operators. How to implement selection control using
one-way if statements, two-way if-else statements, nested if and multi-way if
statements and switch statements and how to use logical operators.
5/22/2022 SWE-103T OOP SED,SSUET 3
Introduction
• Another way to make programs more user-interactive and flexible
is to make the program able to handle more than one case. This is
done by making use of selection statements.
• Selection Statements: Selection statements allow a program to
test several conditions, and execute instructions based on which
condition is true. That is why selection statements are also
referred to as conditional statements.
5/22/2022 SWE-103T OOP SED,SSUET 4
Selection Statements in JAVA
5/22/2022 SWE-103T OOP SED,SSUET 5
• Like all high-level programming languages, Java provides selection
statements. Selection statements use conditions that are Boolean
expressions.
• Boolean expression is an expression that evaluates to a Boolean value:
true or false.
• Boolean Data Type (Already discussed in previous lectures)
• The Boolean data type declares a variable with the value either true or false.
• true and false are literals, just like a number such as 10.
• Boolean Variable A variable that holds a Boolean value is known as a
Boolean variable.
• Example: boolean lightsOn = true;
Relational/ Comparison Operators
• Java provides six relational operators (also known as comparison
operators), shown in Table 3.1
5/22/2022 SWE-103T OOP SED,SSUET 6
Logical Operators
5/22/2022 SWE-103T OOP SED,SSUET 7
Practice task
• 3.18 Assuming that x is 1, show the result of the following
Boolean expressions.
i. !(x > 0) && (x > 0)
ii. (x > 0) || (x < 0)
iii. (x != 0) || (x == 0)
iv. (x >= 0) || (x < 0)
v. (x != 1) == !(x == 1)
5/22/2022 SWE-103T OOP SED,SSUET 8
Example Program1
public class BooleanTest {
public static void main(String[] args) {
int x=1;
int y=x-1;
boolean test= x>y;
System.out.println(" x>y is " + test);
}
}
5/22/2022 SWE-103T OOP SED,SSUET 9
Example Program2
public class BooleanTest {
public static void main(String[] args) {
int x=1;
int y=x-1;
boolean test= x<y;
System.out.println(" x<y is " + test);
}
}
5/22/2022 SWE-103T OOP SED,SSUET 10
• 3.2 Assuming that x is 1, show the result of the following Boolean expressions:
• (x > 0)
• (x < 0)
• (x != 0)
• (x >= 0)
• (x != 1)
• 3.3 Can the following conversions involving casting be allowed? Write a test program to
verify your answer.
boolean b = true;
i = (int)b;
int i = 1;
boolean b = (boolean)i;
Test Questions
5/22/2022 SWE-103T OOP SED,SSUET 11
If Statements
• Java has several types of selection statements: one-way if
statements, two-way if-else statements, nested if statements,
multi-way if-else statements, switch statements, and conditional
expressions.
5/22/2022 SWE-103T OOP SED,SSUET 12
One-Way if Statement
• A one-way if statement executes an action if and
only if the condition is true. The syntax for a one-
way if statement is:
if (boolean-expression) {
statement(s);
}
Example
• Write a Java program That takes an integer as input and test it, if
the input is positive, print that input is positive. Otherwise quite
the program.
5/22/2022 SWE-103T OOP SED,SSUET 13
Example 2
• Calculate the area of circle, if and only if the radius is greater than
or equal to 0.
5/22/2022 SWE-103T OOP SED,SSUET 14
Example 3 SimpleIfDemo.java
• Gives a program that prompts the user to enter an integer. If the number is a
multiple of 5, the program displays HiFive. If the number is divisible by 2, it
displays HiEven.
5/22/2022 SWE-103T OOP SED,SSUET 15
Practice tasks
• 3.4 Write an if statement that assigns 1 to x if y is greater than 0.
• 3.5 Write an if statement that increases pay by 3% if score is
greater than 90.
5/22/2022 SWE-103T OOP SED,SSUET 16
Two-Way if-else Statements
• A one-way if statement performs an action if the specified
condition is true. If the condition is false, nothing is done. But by
using a two-way if-else statement we can work with false
statements as well. Following is the syntax for a two-way if-else
statement:
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
5/22/2022 SWE-103T OOP SED,SSUET 17
Example 1
• Write a Java program that takes an integer as input and test it, if
the input is positive, print that “the input is positive”, otherwise
print that “the input is negative”.
5/22/2022 SWE-103T OOP SED,SSUET 18
Example 2
• Write a program to check whether the input is even or odd.
5/22/2022 SWE-103T OOP SED,SSUET 19
Practice Task
• 1. Write a program to check the attendance of an student. If the
attendance is greater than 75% print a message that you are
allowed other wise print that you are not eligible for exam.
• 2. Write an if statement that increases pay by 3% if KPI score is
greater than 90, otherwise increases pay by 1%.
5/22/2022 SWE-103T OOP SED,SSUET 20
Nested if and Multi-Way if-else Statements
• An if statement can be inside another if statement to form a nested
if statement.
• The statement in an if or if-else statement can be any legal Java
statement, including another if or if-else statement.
• The inner if statement is said to be nested inside the outer if
statement. The inner if statement can contain another if
statement;
• In fact, there is no limit to the depth of the nesting.
5/22/2022 SWE-103T OOP SED,SSUET 21
Example 1
• IMF passed a resolution to charge income tax if the salary is
greater than 50000. Write a program to calculate the tax amount
as per the given slabs
5/22/2022 SWE-103T OOP SED,SSUET 22
Salary Income Tax Ratio
50000-62000 5%
62001-79000 10%
79001-104000 20%
104001-1000000 30%
5/22/2022 SWE-103T OOP SED,SSUET 23
Example 2
• Write a java program to grant the scholarship to an student.
Student will be granted scholarship in fulfilling the following
conditions. Otherwise he/she wont be eligible for the scholarship.
i. If he/she had 75% or more attendance during semester and he/she
scored 3 gpa or above in exam.
ii. He/She must be the student of either 3rd, 4th, 5th or 6th semester.
5/22/2022 SWE-103T OOP SED,SSUET 24
5/22/2022 SWE-103T OOP SED,SSUET 25
Practice Task
• Write the gpa Assignment code on basis of following table:
5/22/2022 SWE-103T OOP SED,SSUET 26
4 A
3.66 A-
3.33 B+
3 B
2.66 B-
2.33 C+
2 C
1.66 C-
1.3 D+
1 D
0 F
Common Errors and Pitfalls
• Common Error 1: Forgetting Necessary Braces
• Common Error 2: Wrong Semicolon at the if Line
• Common Error 3: Redundant Testing of Boolean Values
• Common Error 4: Dangling else Ambiguity
• Common Error 5: Equality Test of Two Floating-Point Values
5/22/2022 SWE-103T OOP SED,SSUET 27
Generating Random Numbers
• Math.random( ) can be used to obtain a random double value
between 0.0 and 1.0, excluding 1.0.
• The number can be made integer by writing:
int Variable = (int)(Math.random() * 10);
5/22/2022 SWE-103T OOP SED,SSUET 28
switch Statements
• The main reasons for using a switch include improving clarity, by
reducing otherwise repetitive coding, and (if the heuristics
permit) also offering the potential for faster execution through
easier compiler optimization in many cases.
• A switch statement executes statements based on the value of a
variable or an expression.
• Switch works as:
• 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 to escape from switch.
5/22/2022 SWE-103T OOP SED,SSUET 29
Switch Syntax
• Following is the full syntax for the switch statement:
switch (switch-expression) {
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;
...
case valueN: statement(s)N;
break;
default: statement(s)-for-default;
}
5/22/2022 SWE-103T OOP SED,SSUET 30
Example 1
5/22/2022 SWE-103T OOP SED,SSUET 31

More Related Content

Similar to Basics of Java

Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective   Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective
Engineering Software Lab
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
melbruce90096
 
Test design techniques
Test design techniquesTest design techniques
Test design techniques
Pragya Rastogi
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 
Final Exam Solutions Fall02
Final Exam Solutions Fall02Final Exam Solutions Fall02
Final Exam Solutions Fall02
Radu_Negulescu
 

Similar to Basics of Java (20)

Operating System
Operating SystemOperating System
Operating System
 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
 
Application of theorem proving for safety-critical vehicle software
Application of theorem proving for safety-critical vehicle softwareApplication of theorem proving for safety-critical vehicle software
Application of theorem proving for safety-critical vehicle software
 
Code coverage in theory and in practice form the do178 b perspective
Code coverage in theory and in practice form the do178 b perspectiveCode coverage in theory and in practice form the do178 b perspective
Code coverage in theory and in practice form the do178 b perspective
 
Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective   Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective
 
Chapter 8 Testing Tactics.ppt
Chapter 8 Testing Tactics.pptChapter 8 Testing Tactics.ppt
Chapter 8 Testing Tactics.ppt
 
Unit 2 Unit level testing.ppt
Unit 2 Unit level testing.pptUnit 2 Unit level testing.ppt
Unit 2 Unit level testing.ppt
 
Tools and techniques of code coverage testing
Tools and techniques of code coverage testingTools and techniques of code coverage testing
Tools and techniques of code coverage testing
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
 
Test design techniques
Test design techniquesTest design techniques
Test design techniques
 
Ass-1_Critical Path Analysis.pptx
Ass-1_Critical Path Analysis.pptxAss-1_Critical Path Analysis.pptx
Ass-1_Critical Path Analysis.pptx
 
Chapter 8 Testing Tactics.ppt Software engineering
Chapter 8 Testing Tactics.ppt Software engineeringChapter 8 Testing Tactics.ppt Software engineering
Chapter 8 Testing Tactics.ppt Software engineering
 
White-box Unit Test Generation with Microsoft IntelliTest
White-box Unit Test Generation with Microsoft IntelliTestWhite-box Unit Test Generation with Microsoft IntelliTest
White-box Unit Test Generation with Microsoft IntelliTest
 
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and MockitoSample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and Mockito
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 hands
 
DST 정-말 좋아합니다!!
DST 정-말 좋아합니다!!DST 정-말 좋아합니다!!
DST 정-말 좋아합니다!!
 
Final Exam Solutions Fall02
Final Exam Solutions Fall02Final Exam Solutions Fall02
Final Exam Solutions Fall02
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 

Basics of Java

  • 1. SWE-103T Object Oriented Programming Instructors: Engr. Anila Saghir Software Engineering Department 5/22/2022 SWE-103T OOP SED,SSUET 1
  • 3. Chapter #3 Selections Chapter 1 introduces how to declare Boolean variables, and write Boolean expressions using relational operators. How to implement selection control using one-way if statements, two-way if-else statements, nested if and multi-way if statements and switch statements and how to use logical operators. 5/22/2022 SWE-103T OOP SED,SSUET 3
  • 4. Introduction • Another way to make programs more user-interactive and flexible is to make the program able to handle more than one case. This is done by making use of selection statements. • Selection Statements: Selection statements allow a program to test several conditions, and execute instructions based on which condition is true. That is why selection statements are also referred to as conditional statements. 5/22/2022 SWE-103T OOP SED,SSUET 4
  • 5. Selection Statements in JAVA 5/22/2022 SWE-103T OOP SED,SSUET 5 • Like all high-level programming languages, Java provides selection statements. Selection statements use conditions that are Boolean expressions. • Boolean expression is an expression that evaluates to a Boolean value: true or false. • Boolean Data Type (Already discussed in previous lectures) • The Boolean data type declares a variable with the value either true or false. • true and false are literals, just like a number such as 10. • Boolean Variable A variable that holds a Boolean value is known as a Boolean variable. • Example: boolean lightsOn = true;
  • 6. Relational/ Comparison Operators • Java provides six relational operators (also known as comparison operators), shown in Table 3.1 5/22/2022 SWE-103T OOP SED,SSUET 6
  • 8. Practice task • 3.18 Assuming that x is 1, show the result of the following Boolean expressions. i. !(x > 0) && (x > 0) ii. (x > 0) || (x < 0) iii. (x != 0) || (x == 0) iv. (x >= 0) || (x < 0) v. (x != 1) == !(x == 1) 5/22/2022 SWE-103T OOP SED,SSUET 8
  • 9. Example Program1 public class BooleanTest { public static void main(String[] args) { int x=1; int y=x-1; boolean test= x>y; System.out.println(" x>y is " + test); } } 5/22/2022 SWE-103T OOP SED,SSUET 9
  • 10. Example Program2 public class BooleanTest { public static void main(String[] args) { int x=1; int y=x-1; boolean test= x<y; System.out.println(" x<y is " + test); } } 5/22/2022 SWE-103T OOP SED,SSUET 10
  • 11. • 3.2 Assuming that x is 1, show the result of the following Boolean expressions: • (x > 0) • (x < 0) • (x != 0) • (x >= 0) • (x != 1) • 3.3 Can the following conversions involving casting be allowed? Write a test program to verify your answer. boolean b = true; i = (int)b; int i = 1; boolean b = (boolean)i; Test Questions 5/22/2022 SWE-103T OOP SED,SSUET 11
  • 12. If Statements • Java has several types of selection statements: one-way if statements, two-way if-else statements, nested if statements, multi-way if-else statements, switch statements, and conditional expressions. 5/22/2022 SWE-103T OOP SED,SSUET 12 One-Way if Statement • A one-way if statement executes an action if and only if the condition is true. The syntax for a one- way if statement is: if (boolean-expression) { statement(s); }
  • 13. Example • Write a Java program That takes an integer as input and test it, if the input is positive, print that input is positive. Otherwise quite the program. 5/22/2022 SWE-103T OOP SED,SSUET 13
  • 14. Example 2 • Calculate the area of circle, if and only if the radius is greater than or equal to 0. 5/22/2022 SWE-103T OOP SED,SSUET 14
  • 15. Example 3 SimpleIfDemo.java • Gives a program that prompts the user to enter an integer. If the number is a multiple of 5, the program displays HiFive. If the number is divisible by 2, it displays HiEven. 5/22/2022 SWE-103T OOP SED,SSUET 15
  • 16. Practice tasks • 3.4 Write an if statement that assigns 1 to x if y is greater than 0. • 3.5 Write an if statement that increases pay by 3% if score is greater than 90. 5/22/2022 SWE-103T OOP SED,SSUET 16
  • 17. Two-Way if-else Statements • A one-way if statement performs an action if the specified condition is true. If the condition is false, nothing is done. But by using a two-way if-else statement we can work with false statements as well. Following is the syntax for a two-way if-else statement: if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } 5/22/2022 SWE-103T OOP SED,SSUET 17
  • 18. Example 1 • Write a Java program that takes an integer as input and test it, if the input is positive, print that “the input is positive”, otherwise print that “the input is negative”. 5/22/2022 SWE-103T OOP SED,SSUET 18
  • 19. Example 2 • Write a program to check whether the input is even or odd. 5/22/2022 SWE-103T OOP SED,SSUET 19
  • 20. Practice Task • 1. Write a program to check the attendance of an student. If the attendance is greater than 75% print a message that you are allowed other wise print that you are not eligible for exam. • 2. Write an if statement that increases pay by 3% if KPI score is greater than 90, otherwise increases pay by 1%. 5/22/2022 SWE-103T OOP SED,SSUET 20
  • 21. Nested if and Multi-Way if-else Statements • An if statement can be inside another if statement to form a nested if statement. • The statement in an if or if-else statement can be any legal Java statement, including another if or if-else statement. • The inner if statement is said to be nested inside the outer if statement. The inner if statement can contain another if statement; • In fact, there is no limit to the depth of the nesting. 5/22/2022 SWE-103T OOP SED,SSUET 21
  • 22. Example 1 • IMF passed a resolution to charge income tax if the salary is greater than 50000. Write a program to calculate the tax amount as per the given slabs 5/22/2022 SWE-103T OOP SED,SSUET 22 Salary Income Tax Ratio 50000-62000 5% 62001-79000 10% 79001-104000 20% 104001-1000000 30%
  • 23. 5/22/2022 SWE-103T OOP SED,SSUET 23
  • 24. Example 2 • Write a java program to grant the scholarship to an student. Student will be granted scholarship in fulfilling the following conditions. Otherwise he/she wont be eligible for the scholarship. i. If he/she had 75% or more attendance during semester and he/she scored 3 gpa or above in exam. ii. He/She must be the student of either 3rd, 4th, 5th or 6th semester. 5/22/2022 SWE-103T OOP SED,SSUET 24
  • 25. 5/22/2022 SWE-103T OOP SED,SSUET 25
  • 26. Practice Task • Write the gpa Assignment code on basis of following table: 5/22/2022 SWE-103T OOP SED,SSUET 26 4 A 3.66 A- 3.33 B+ 3 B 2.66 B- 2.33 C+ 2 C 1.66 C- 1.3 D+ 1 D 0 F
  • 27. Common Errors and Pitfalls • Common Error 1: Forgetting Necessary Braces • Common Error 2: Wrong Semicolon at the if Line • Common Error 3: Redundant Testing of Boolean Values • Common Error 4: Dangling else Ambiguity • Common Error 5: Equality Test of Two Floating-Point Values 5/22/2022 SWE-103T OOP SED,SSUET 27
  • 28. Generating Random Numbers • Math.random( ) can be used to obtain a random double value between 0.0 and 1.0, excluding 1.0. • The number can be made integer by writing: int Variable = (int)(Math.random() * 10); 5/22/2022 SWE-103T OOP SED,SSUET 28
  • 29. switch Statements • The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. • A switch statement executes statements based on the value of a variable or an expression. • Switch works as: • 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 to escape from switch. 5/22/2022 SWE-103T OOP SED,SSUET 29
  • 30. Switch Syntax • Following is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; } 5/22/2022 SWE-103T OOP SED,SSUET 30
  • 31. Example 1 5/22/2022 SWE-103T OOP SED,SSUET 31