LAB 1: INTRODUCTION TO PROGRAMMING

advertisement
LAB 3: SELECTIONS.
Like all high-level programming languages, Java provides selection statements that let you choose
actions with two or more alternative courses. Selection statements use conditions. Conditions are
Boolean expressions. This topic first introduces Boolean types, and expressions.
LAB SHEET 3.1: boolean Data Type
ESTIMATED TIME
:
0.5 hours
OBJECTIVE
:
To declare boolean type and write Boolean expressions using
comparison operators.
REQUIREMENTS
:
JCreator 5.00 Pro
PROCEDURE
:
Read the question and write the program. Then run the program and
get the correct output.
CONCLUSION
:
A boolean expression contains conditional and boolean operators and
evaluates to true or false.
DISCUSSION / RESULT / EXERCISE
a) (Checking whether a number is odd) Write a program that reads an integer and checks whether
it is odd. Here are two sample runs:
Enter an integer: 17
Is 17 an odd number? True
Enter an integer: 500
Is 500 an odd number? false
1
LAB SHEET 3.2: if Statements
ESTIMATED TIME
:
1.5 hour
OBJECTIVE
:
To implement selection control using one-way if statements.
To implement selection control using two-way if statements.
To implement selection control using nested if statements.
REQUIREMENTS
:
JCreator 5.00 Pro
PROCEDURE
:
Read all the questions and write the program. Then run the program
and get the correct output.
CONCLUSION
:
An if statement allows a program to choose whether to execute a
particular statement. An if-else statement allows a program to do
one thing if a condition is true and another thing if the condition is
false. A special form of nested if-else statements forms a
multibranch structure.
DISCUSSION / RESULT / EXERCISE
a) Write a Java application that inputs an integer and uses an if statement to determine whether
the integer is even, and, if it is, prints that number.
2
b) Write a Java application that inputs an integer and uses an if...else statement to determine
whether the integer is odd or even. If it is odd, print the number followed by “is odd”; if it is
even, print the number followed by “is even”.
3
c) (Checking a number) Write a program that prompts the user to enter an integer and checks
whether the number is divisible by both 3 and 4, or neither of them, or just one of them. Here
are some sample runs:
Enter an integer: 16
16 is divisible by 3 or 4, but not both.
Enter an integer: 24
24 is divisible by both 3 and 4.
Enter an integer: 7
7 is not divisible by either 3 or 4.
4
LAB SHEET 3.3: switch Statements
ESTIMATED TIME
:
0.5 hours
OBJECTIVE
:
To implement selection control using switch statements.
REQUIREMENTS
:
JCreator 5.00 Pro
PROCEDURE
:
Read all the questions and write the program. Then run the program
and get the correct output.
CONCLUSION
:
The switch statement is useful for expressing a selection control
based on equality testing between data. The break statement causes
the control to break out of the surrounding switch statement.
DISCUSSION / RESULT / EXERCISE
One million is 106 and 1 billion is 109. Write a program that reads a power of 10 (6, 9, 12, etc.) and
displays how big the number is (Million, Billion, etc.) Display an appropriate message for the
input value that has no corresponding word. The table below shows the correspondence between
the power of 10 and the word for that number.
Power of 10
6
9
12
15
18
21
30
100
Number
Million
Billion
Trillion
Quadrillion
Quintillion
Sextillion
Nonillion
Googol
5
LAB SHEET 3.4: Conditional Expressions
ESTIMATED TIME
:
0.5 hours
OBJECTIVE
:
To write expressions using the conditional operator.
REQUIREMENTS
:
JCreator 5.00 Pro
PROCEDURE
:
Read all the questions and write the program. Then run the program
and get the correct output.
CONCLUSION
:
Conditional expressions are in a completely different style, with no
explicit if in the statement.
DISCUSSION / RESULT / EXERCISE
Rewrite your solution to EXERCISE3_2b so that it uses the conditional operator (?:), rather than an
if...else statement.
6
Download