Professional Documents
Culture Documents
Introduction To Programming: Java Lab 6: If Statement
Introduction To Programming: Java Lab 6: If Statement
Java Lab 6:
if Statement
QuizGrading
LeapYear
2
Class QuizGrading
• Reads in an integer (from the keyboard) as the score.
• Displays the grade assigned to the score according to the
following table:
Score Grade
90 - 100 A
80 – 89 B
70 - 79 C
60 – 69 D
< 60 E
• Objectives
– Understand the use of multiple if statements (if-else
statements).
– Applying relational operators: < , <= , >=
3
Class QuizGrading (2)
• Method
• Applying
– Read a numeric input (i.e. score) from the keyboard.
4
Anatomy of Class QuizGrading
import java.util.Scanner;
public class QuizGrading
{
public static void main(String[] args)
{
/* To Do - write code to read score from the
key board which is an integer of type int. */
char grade = ' ';
if (score >= 90)
{ grade = 'A'; }
else if (score >= 80)
{ grade = 'B'; }
else if // To Do - write similar Java if-else statements
// Lastly, print the grade, along with an appropriate description
}
} 5
Class LeapYear
• Reads in a year and computes whether the year is a leap
year.
• Objectives
– Understand the use of if-else statement
– Applying the arithmetic operator % (computes the
remainder of an integer division)
– Using relational operators: == , !=
– Using boolean operators: && , ||
6
Class LeapYear (2)
• Formulae
7
Anatomy of Class LeapYear
import java.util.Scanner;
public class LeapYear
{
public static void main(String[] args)
{
/* To Do:
(i) declare a variable year of type int
(ii) read a numeric input (year) from the keyboard */
/* declare the boolean variables a, b and c as
shown in slide 7 */
/* write an if-else statement and use the boolean
expression shown in slide 7 */
/* write a println statement to display the result
of the computation. */
}
8
}
Flow Chart for if-else statement
[back to slide 4]
start
true false
score
>= 90
9
Print statement