CSE 215L: Programming Language II Lab Faculty: Silvia Ahmed, Sec - 9,10
CSE 215L: Programming Language II Lab Faculty: Silvia Ahmed, Sec - 9,10
II Lab
Faculty: Silvia Ahmed,
Sec – 9,10
Lab 2 – Spring 2021
Switch statement
• You have to use only values for the case statements.
• You can not use Boolean expressions like: less than or greater
than.
• You can watch Anisul Islam’s videos on this topic:
1. https://www.youtube.com/watch?v=NDtRRR3WDL0&list=P
LgH5QX0i9K3oAZUB2QXR-dZac0c9HNyRa&index=22
2. https://www.youtube.com/watch?
v=hRUkljRO25g&list=PLgH5QX0i9K3oAZUB2QXR-
dZac0c9HNyRa&index=23
Task 01
• Use
the discriminant ( – 4ac) value in the if-else statement.
• If positive, use r1 and r2.
• If zero, use only r1 as in this case r1 == r2.
Task 02 and Task 03
• Use switch statements.
• For task 2, the month is derived by using, (int)(Math.
random() * 12) + 1.
• You can find details on Math.random() here:
https://javarevisited.blogspot.com/2013/05/how-to-generate-
random-numbers-in-java-between-range.html
• For task 3, you shall need two switch statements.
• If today is 3 and elapsed day is 20, then the future day is (20 +
3) % 7.
Task 04
• If you want to calculate the reverse, then use a loop and find the
determinant.
• Add the determinant to a variable each time multiplying it with
10.
• For example: int num = 121, num 1 = num, sum = 0.
• while(num1 != 0){
int rem = num1 % 10;
sum = (sum*10) + rem;
num1 = num1 /10;
}
If num == sum them palindrome else not a palindrome.
Task 05
• Take two variables int me and int computer;
• For computer use (int)(Math.random() * 3).
• You can find details on Math.random() here:
https://javarevisited.blogspot.com/2013/05/how-to-generate-
random-numbers-in-java-between-range.html
• If(me == computer) then draw.
• else if (me == 0 and computer == 2 or me == 1 and computer
== 0 or me == 2 and computer == 1) then me is winner
• Also if you swap the values of me and computer then you can
write the else if statement for computer to be the winner.
Task 06
• Read side1, side2 and side3.
• Sum of two sides greater than the third one.
• Otherwise invalid triangle.
Task 07
• Take the input and check for it to be positive or negative.
• If input is num then the loop should be,
• while(num != 0)
{ if(num > 0)
----------- your code
else
----your code
}
Task 08
• Use the “\t” for tab.
• Take two variables for pound and in the same loop increase the
first one by two and the second one by 5.
Task 09
• Use bubble sorting.
• If you know the maximum, use sorting again and find the
second maximum by not taking the maximum number.
• It is better to do with an array so you can omit this one.
Attention!!
• You have to write the output exactly as given in the sample
input and output.
• Notice the structure of your code.
• Now go back again to the Task 01 and find your shortcomings
from all the tasks.
• Improve yourself!