5 - Selections - Examples
5 - Selections - Examples
Exercise (1):
Fill in the missing parts to print the values true and false:
isJavaFun = true;
isFishTasty = false;
System.out.println(isJavaFun);
System.out.println(isFishTasty);
Exercise (2):
int x = 10;
int y = 9;
System.out.println( );
Exercise (3):
int x = 50;
int y = 10;
(x y) {
System.out.println("Hello World");
}
Exercise (4):
Print "Hello World" if x is equal to y.
int x = 50;
int y = 50;
(x y) {
System.out.println("Hello World");
}
Exercise (5):
int x = 50;
int y = 50;
(x y) {
System.out.println("Yes");
} {
System.out.println("No");
}
Exercise (6):
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");
}
Exercise (7):
Insert the missing parts to complete the following "short hand if...else statement"
(ternary operator):
Exercise (8):
int day = 2;
switch ( ) {
1:
System.out.println("Saturday");
break;
2:
System.out.println("Sunday");
;
}
Exercise (9):
Complete the switch statement, and add the correct keyword at the end to specify some
code to run if there is no case match in the switch statement.
int day = 4;
switch ( ) {
1:
System.out.println("Saturday");
break;
2:
System.out.println("Sunday");
;
:
System.out.println("Weekend");
}
*************************************************************
5)
public static void main(String[] args) {
int weight = 700;
System.out.println(weight >= 500);
char gender = 'm';
System.out.println(gender <= 'f');
double colorWaveLength = 1.630;
System.out.println(colorWaveLength > 1.621);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
6)
public static void main(String[] args) {
int a = 5;
int b = 7;
boolean condition = (b > a) && (a + b < a * b);
System.out.println(condition);
System.out.println('B' == 'A' + 1);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
*************************************************************
D) Find the errors and correct them:
1)
public class Summation {
public static void main(String[] args) {
int Sum;
Sum = 25;
double res = Sum + 10.5;
System.out.println(" The result = " + Sum);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
public class Summation {
public static void main(String[] args) {
int num = 34;
if (num >=20){
int num = 17;
System.out.println("The num = " + num);
} else
System.out.println("The num = " + num);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public class Summation {
public static void main(String[] args) {
x=60;
x= x +20;
System.out.println("X:" + x);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
4)
public class Summation {
public static void main(String[] args) {
int x=70;
if(x>60)
System.out.println("Pass");
else
System.out.println("Fail");
else
System.out.println("Equal");
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
5)
public class Summation {
public static void main(String[] args) {
int x=1;
double y=3.5;
if(x>2)
x=y;
else
x++;
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
4. Write a complete Java program that asks for a number. If the number
is less than 5, it is written out, but if it is greater than or equal to 5,
twice that number is written out.
Category Price
1 100
2 150
3 200
- The program should compute and print the total price of the item
bought.
- The program should ask the user whether he want to enter another
item. If the user enters ‘y’ the program should repeat again the
above steps. If he enters ‘n’ the program ends.
Hint: The total price is computed as: (qty * price)