# Practice Inverted - If Else --- title: Agenda description: Today's Content duration: 30 card_type: cue_card --- # Today's Content 1. Revision 2. Practice of If - Else 3. Categorize triangle in equilateral, isosceles, scalene 4. Max of 3 5. Fizz Buzz 6. Categorize number --- title: Recap from previous lecture description: Some important terms from previous lecture. duration: 30 card_type: cue_card --- ## Recap **Some abbreviations that will be used in this class:** * System.out.print - SOP * System.out.println - SOPln --- title: Quiz 1 description: Quiz 1 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int a = 10,b = 10; if(a >= 10 && b >= 10){ System.out.print(a+b); } ``` # Choices - [ ] 10 - [x] 20 - [ ] 30 - [ ] None --- title: Quiz 2 description: Quiz 2 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int a = 10; int b = 10; if( ++a >= 12 && ++b >= 12 ){ System.out.println("Hello"); } System.out.println(a + b); ``` # Choices - [ ] Hello<br>10 - [ ] 22 - [x] 21 - [ ] None --- title: Quiz 3 description: Quiz 3 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int a = 10; int b = 10; if( ++ a >= 11 || ++ b >= 12 ){ System.out.println("Hello"); } System.out.println(a + b); ``` # Choices - [ ] 20 - [ ] 22 - [x] Hello<br>21 - [ ] None --- title: Quiz 4 description: Quiz 4 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int a = 10; int b = 10; if( ++a >= 12 || ++b >= 12 ){ System.out.println("Hello"); } System.out.println(a + b); ``` # Choices - [ ] 20 - [ ] 21 - [x] 22 - [ ] None --- title: Quiz 5 description: Quiz 5 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int N = 5; if(N > 2) System.out.println("Yayay"); else System.out.println("Blahblah!!"); ``` # Choices - [x] Yayay - [ ] Blahblah!! --- title: Quiz 6 description: Quiz 6 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int N = 5; if(N > 2) System.out.println("Yayay"); System.out.println("Hmmmm"); else System.out.println("Blahblah!!"); System.out.println("Blahblah!!"); ``` # Choices - [x] Error :( - [ ] No Error, this code rocks! :D - [ ] Yayay<br>Hmmmm - [ ] Blahblah!!<br>Blahblah!! --- title: Quiz 7 description: Quiz 7 duration: 30 card_type: quiz_card --- # Question What will be the output of the following code? ```java int marks = 80; if(marks > 70) { System.out.print("Distinction "); System.out.print("Congrats "); } else if(marks > 35) { System.out.print("Pass "); } else System.out.print("Fail "); System.out.print("Good luck"); ``` # Choices - [x] Distinction Congrats Good luck - [ ] Good luck - [ ] Error - [ ] Distinction Congrats --- title: If or Else Problems description: If / Else duration: 200 card_type: cue_card --- **Note:** Run the following codes in IDE as well. ### Example 1 Read age of a person, check if person is at retirement age, or still have few years left to work. Retirement age is 65. ``` Scanner sc = new Scanner(System.in); int age = sc.nextInt(); if (age > 65) { System.out.println("Retired"); }else{ System.out.println("Few more years of service."); } ``` --- title: Quiz 8 description: Quiz 8 duration: 30 card_type: quiz_card --- # Question Predict the output: ``` if(9 > 5){ System.out.println("If block"); } else{ System.out.println("Else block"); } ``` # Choices - [x] If block - [ ] If block<br>Else block - [ ] Error --- title: Quiz 9 description: Quiz 9 duration: 30 card_type: quiz_card --- # Question Predict the output: ``` if(false){ System.out.println("Line 1"); } else { System.out.println("Line 2"); } ``` # Choices - [ ] Line 1 - [x] Line 2 - [ ] Line 1<br>Line 2 - [ ] Error --- title: Modulus Operator description: Modulus Operator duration: 200 card_type: cue_card --- ### Modulus Operator Modulus operator (%) -> Gives remainder ``` System.out.println(17 % 4) -> Remainder = 1 System.out.println(24 % 2) -> Remainder = 0 System.out.println(97 % 2) -> Remainder = 1 System.out.println(82 % 2) -> Remainder = 0 ``` Explain even and odd numbers. **Even numbers:** Numbers which are divisible by 2. Eg: 2, 4, 6, 8, 10, 12.. When we divide the number with 2, remainder = 0 **Odd numbers:** Numbers which are not divisible 2. Eg: 1, 3, 5, 7, 9, 11.. When we divide the number with 2, remainder = 1 --- title: Even or Odd description: Read a number and check if number is odd or even. duration: 200 card_type: cue_card --- ### Example 1 Read a number and check if number is odd or even. ``` Scanner sc = new Scanner(System.in); int a = sc.nextInt(); if (a % 2 == 0) { System.out.println("Number is even"); }else{ System.out.println("Number is odd"); } ``` --- title: Show code in Python tutor description: duration: 200 card_type: cue_card --- We will be incorporating the Python Tutor website into our curriculum to provide you with a more interactive and visual understanding of coding concepts. https://pythontutor.com/render.html#mode=display (Change language to JAVA) Paste this code in the site and show the learners how your code works step by step. (Don't use scanner, it wont work in the site) ```java public class MainClass { public static void main(String[] args) { int a = 10; if (a % 2 == 0) { System.out.println("Number is even"); }else{ System.out.println("Number is odd"); } } } ``` --- title: Divisible by 5 description: Check if a number is divisible by 5. duration: 200 card_type: cue_card --- ### Example 2 Check if a number is divisible by 5. ``` Scanner sc = new Scanner(System.in); int a = sc.nextInt(); if (a % 5 == 0) { System.out.println("Number is divisible by 5"); }else{ System.out.println("Number is not divisible by 5"); } ``` --- title: Show code in Python tutor description: duration: 200 card_type: cue_card --- https://pythontutor.com/render.html#mode=display Paste this code in the site and show the learners how your code works step by step. (Don't use scanner, it wont work in the site) ```java public class MainClass { public static void main(String[] args) { int a = 10; if (a % 5 == 0) { System.out.println("Number is divisible by 5"); }else{ System.out.println("Number is not divisible by 5"); } } } ``` --- title: Divisible by 2 or 3 description: Check if a number is divisible by 2 or 3. duration: 200 card_type: cue_card --- ### Example 2 Check if a number is divisible by 2 or 3. ```java Scanner sc = new Scanner(System.in); int a = sc.nextInt(); if (a % 2 == 0 || a % 3 == 0) { System.out.println("Number is divisible by 2 or 3"); }else{ System.out.println("Number is not divisible by 2 and 3 both"); } ``` --- title: Quiz 10 description: Quiz 10 duration: 30 card_type: quiz_card --- # Question Can we have if without an else block? # Choices - [x] Yup!! - [ ] Nope!! - [ ] Don't know --- title: Quiz 11 description: Quiz 11 duration: 30 card_type: quiz_card --- # Question Can we have else without an if block? # Choices - [ ] Yup!! - [x] Nooo!! - [ ] Maybe --- title: Max of 2 numbers description: Max of 2 numbers duration: 100 card_type: cue_card --- Read 2 numbers and print max of 2 numbers. **Examples:** ```plaintext a = 5 , b = 10 Max of a and b = 10 ``` ```plaintext a = 15 , b = 10 Max of a and b = 15 ``` ``` Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); if (a > b) { System.out.println(a); }else{ System.out.println(b); } ``` --- title: Quiz 12 description: Quiz 12 duration: 30 card_type: quiz_card --- # Question Predict the output: For input: 45 45 ``` Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); if(a > b){ System.out.print(a); } else{ System.out.print(b); } ``` # Choices - [ ] Error - [ ] 45<br>45 - [x] 45 --- title: Categorize Number description: If / Else if and example introduction duration: 300 card_type: cue_card --- Introduce this example before formally introducing this section first. ### Categorize Number Given an integer n0, categorize it into positive, negative or zero. Category: n = 10: n > 0: print "positive number" n = -27: n < 0: print "negative number" n = 0: n == 0: print "zero" Give some more examples. Idea: ``` public static void main() { Scanner sc = new Scanner(System.in); int a = scn.nextInt(); if (a > 0) { System.out.println("positive number"); } if (a < 0) { System.out.println("negative number"); } if (a == 0) { System.out.println("zero"); } } ``` **Q.** Is the above logic correct? **A.** Yes Dry run the above code for some examples. Explain the problem in the above approach. It's the wastage of comparisions. --- title: Categorize Triangles description: Categorize triangle on the basis of the length of the sides duration: 1200 card_type: cue_card --- # Categorize Triangles Categorize triangle on the basis of the length of the sides **Equilateral:** When the length of the all the sides are equal. **Isosceles:** When the length of any two sides are equal. **Scalene:** When the length of all sides are different. Let `a`, `b`, `c` be the length of the three sides of a triangle. Given in each case they take some values, tell the category of the triangle. It is the given that the input values for a, b, c are positive integer values. ```plaintext a = 20, b = 20, c = 20 -- Output = Equilaterial ``` ```plaintext a = 7, b = 12, c = 9 -- Output = Scalene ``` ```plaintext a = 5, b = 13, c = 5 -- Output = Isosceles ``` ```plaintext a = 12, b = 7, c = 7 -- Output = Isosceles ``` The equivalent code for implementing the above logic is as follows: ```java if(a == b && b == c){ SOPln("Equilateral"); } else if(a == b || b == c || a == c){ SOPln("Isosceles"); } else{ SOPln("Scalene"); } ``` --- title: Show code in Python tutor description: duration: 200 card_type: cue_card --- We will be incorporating the Python Tutor website into our curriculum to provide you with a more interactive and visual understanding of coding concepts. https://pythontutor.com/render.html#mode=display Paste this code in the site and show the learners how your code works step by step. ```java 1 public class MainClass { 2 public static void main(String[] args) { 3 int a=10, b=10, c=10; 4 if(a == b && b == c){ 5 System.out.println("Equilateral"); 6 } 7 else if(a == b || b == c || a == c){ 8 System.out.println("Isosceles"); 9 } 10 else{ 11 System.out.println("Scalene"); 12 } 13 } 14 } ``` --- title: Max of three description: duration: 1300 card_type: cue_card --- # Max of three **Ques:** Given three numbers, print the maximum among them. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/045/724/original/upload_a249488f4da0204e8671e22d85267672.png?1693733958" alt= “” width ="700" height="600"> Note that `a`, `b`, `c` can take any integer values. Stress on the point that `a`, `b`, `c` can also take equal values. The three test case demonstrates this point. For example, * a = 7, b = 20, c = 50 ==> max = 50 * a = 10, b = 9, c = 10 ==> max = 10 * a = 3, b = 3, c = 3 ==> max = 3 The equivalent code for implementing the above logic is as follows: ```java if(a >= b && a >= c){ SOPln("a"); } else if(b >= c){ SOPln("b"); } else{ SOPln("c"); } ``` --- title: Fizz-Buzz description: Printing Fizz, Buzz and Fizz-Buzz based on the number divisbility by 3 and 5. duration: 900 card_type: cue_card --- # Fizz-Buzz **Ques:** Given a number, * print "Fizz" if the number is divisible by 3. * print "Buzz" if the number is divisible by 5. * print "Fizz-Buzz" if the number is divisble by both 3 and 5. For example, * n = 39, O/p = Fizz * n = 25, O/p = Buzz * n = 15, O/p = Fizz-Buzz * n = 13, O/p = `No output` **How to implement this? ** The following code shows a **wrong implementation** of the above logic: ```java if(n % 3 == 0){ SOPln("Fizz"); } else if(n % 5 == 0){ SOPln("Buzz"); } else{ SOPln("Fizz-Buzz"); } ``` The above code prints "Fizz-Buzz" for n = 11, but this is wrong as n is neither divisble by 3 nor 5. So there should have no output for this number. **Another wrong implementation is as follows:** ```java if(n % 3 == 0){ SOPln("Fizz"); } else if(n % 5 == 0){ SOPln("Buzz"); } else if(n % 3 == 0 && n % 5 == 0){ SOPln("Fizz-Buzz"); } ``` The above code prints "Fizz" for n = 15, but this is wrong as n is divisble by 3 and 5 both. So the correct output should be "Fizz-Buzz". So finally, the **correct implementation** of this logic is as follows: ```java if(n % 3 == 0 && n % 5 == 0){ SOPln("Fizz-Buzz"); } else if(n % 3 == 0){ SOPln("Fizz"); } else if(n % 5 == 0){ SOPln("Buzz"); } ``` ## Insist the learners to give the live contests --- title: Unlock Assignment description: duration: 600 card_type: cue_card --- Please unlock the assignment for students by clicking this "question mark" button on top bar. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/078/685/original/Screenshot_2024-06-19_at_7.17.12_PM.png?1718804854" width=300 /> Ask students to submit any one problem from assignment and let you know.