Lesson 2 - While

Todays lesson

  • More Conditionals - if and else

  • while , break and do-while

שאלת חימום

מצא את המקסימום בין 4 מספרים

public class Main {
    public static void main(String[] args) {
        int a = 1, b=71, c=3, d= 10, max;

        max = (a >b) ? a : b;
        max = Math.max(max, c); //same thing
        max = (max >d) ? max : d;

        System.out.println(max);
    }
}
71

כמה ספרות וכמה אפסים

קלוט מספר n

  • הדפס מספר הספרות

  • הדפס מספר האפסים

public class Main {
    public static void main(String[] args) {

        int num = MyConsole.readInt("enter a number: ");
        System.out.println("You entered a " + (num >= 0 ? "positive" : "negative") +" number");

        int countZeros=0;
        int countDigits=0;
        int absNum = num>=0 ? num : -num;

        if (num==0){
            countZeros=1;
            countDigits=1;
        }

        while (absNum > 0) {
            if (absNum%10==0)
                countZeros++;
            absNum /= 10;
            countDigits++;
        }
        System.out.println("number of digits: " + countDigits);
        System.out.println("number of zeros: " + countZeros);
    }
}
enter a number:  -30012
You entered a negative number
number of digits: 5
number of zeros: 2

זה if מקוצר

 int absNum = num>=0 ? num : -num;

version 2

let num be a random number from -1000 until 9999 (including)

        int max = 10_000, min = -1_000;
        int num = (int) ((Math.random() * (max - min)) + min); //random number from -1,000 to 9,999 (including)
        System.out.println("Random num:" + num);
Random num:6318
You entered a positive number
number of digits: 4
number of zeros: 0

Note: it includes the min but not the max [min, max)

Rotate Square

יש לסובב את הקוביה

public class Main {
    public static void main(String[] args) {
         int a = 1, b =2, c=3, d=4, temp;

         temp = d;
         d = c;
         c = b;
         b = a;
         a = temp;

        System.out.println("a: " + a + "\nb: " + b + "\nc: " + c + "\nd: " + d);
    }
}
a: 4
b: 1
c: 2
d: 3

טבלת מעקב

Version 2 : n right or left shifts

קלוט מספר n:

  • אם הוא חיובי אז סובב n פעמים ימינה

  • אם הוא שלילי אז סובב n פעמים שמאלה

public class Main {
    public static void main(String[] args) {
         int a = 1, b =2, c=3, d=4, temp;
         boolean isRightShift=true;

         int n_shifts = MyConsole.readInt("enter:\n* posivite num for right shift" +
                 "\n* negative for left shift\ninput :");
         if (n_shifts<0) {
             isRightShift = false;
             n_shifts = n_shifts * -1; //since while works on positive number, otherwise we would need 2 different whiles
             System.out.println("Left shifting " + n_shifts +" times");
         } else if (n_shifts==0)
             System.out.println("No shifts");
          else
             System.out.println("Right shifting " + n_shifts +" times");


         while (n_shifts>0){
             if (isRightShift) {
                 temp = d;
                 d = c;
                 c = b;
                 b = a;
                 a = temp;
                 n_shifts = n_shifts-1;
                 System.out.println("a: " + a + "\tb: " + b + "\tc: " + c + "\td: " + d);
             }else {
                 temp = a;
                 a = b;
                 b = c;
                 c = d;
                 d = temp;
                 n_shifts = n_shifts-1;
                 System.out.println("a: " + a + "\tb: " + b + "\tc: " + c + "\td: " + d);
             }
         }
    }
}
enter:
* posivite num for right shift
* negative for left shift
input : -2
Left shifting 2 times
a: 2    b: 3    c: 4    d: 1
a: 3    b: 4    c: 1    d: 2

note

since while (n_shifts>0) checks for positive numbers and we do n_shifts = n_shifts-1; inside the while loop without n_shifts = n_shifts * -1 if we have a left shift we would need another while loop

Find Aprox. log2

מצא עבור קלט x את:

קודם כל בא נבין מה קורה כאן

אם הוא לא חזקה שלמה הדפס את חזקה שלמה ועוד .x

public class Main {
    public static void main(String[] args) {
        float x = (float)MyConsole.readDouble("enter positive num: ");

        while (x<0.)
            x = (float)MyConsole.readDouble("Try again\nPlease enter a positive num: ");

//        float x = 1<<10;
//        float x = 1_024f;
        float original = x;
        int count = 0;
        boolean isPowOf2 = false;

        while(x > 2) {
            x = x / 2;
            count +=1;
            if (x==2.){
                System.out.println("log_2("+original+")="+(count+1));
                isPowOf2 = true;
            }
        }
        if (isPowOf2==false)
            System.out.println("log_2("+original+")="+count + ".x");
    }
}
enter num:  1024
log_2(1024.0)=10

2nd run

enter num:  1025
log_2(1025.0)=10.x

To find the real answer we should use a Talyor series.

Find Parallel lines

מצא האם 2 ישרים:

  • מקבילים

  • מאונכים

  • או לזה זה ולא זה

img source

img source

public class Main {
    public static void main(String[] args) {
        //parallel - 2 greens
//        int l1_x1 = 0, l1_y1 = 0, l1_x2 =3, l1_y2=2;
//        int l2_x1 = 3, l2_y1 = 0, l2_x2 =0, l2_y2=-2;

        //perpendicular - green and orange
//        int l1_x1 = 0, l1_y1 = 0, l1_x2 =3, l1_y2=2;
//        int l2_x1 = 0, l2_y1 = 0, l2_x2 =-2, l2_y2=3;

        //neither - green and purple
        int l1_x1 = 0, l1_y1 = 0, l1_x2 =3, l1_y2=2;
        int l2_x1 = 0, l2_y1 = 0, l2_x2 =1, l2_y2=3;

        double Δ_l1_y = l1_y1-l1_y2, Δ_l1_x = l1_x1-l1_x2;
        double Δ_l2_y = l2_y1-l2_y2, Δ_l2_x = l2_x1-l2_x2;

        double slope_l1 = Δ_l1_y/Δ_l1_x;
        double slope_l2 = Δ_l2_y/Δ_l2_x;

        if (slope_l1==slope_l2)
            System.out.println("The lines are parallel\n"
            + "slope is:" + slope_l1);
        else if((-1*Δ_l1_x)/Δ_l1_y==slope_l2)
            System.out.println("The lines are perpendicular\n"
                    + "* slope of line 1 :" + slope_l1
                    + "\n* slope of line 2 :" + slope_l2);
        else
            System.out.println("lines are neither:" +
                    "\n* parallel\n* perpendicular"
                    + "\n\t* slope of line 1 :" + slope_l1
                    + "\n\t* slope of line 2 :" + slope_l2);
    }
}
lines are neither:
* parallel
* perpendicular
    * slope of line 1 :0.6666666666666666
    * slope of line 2 :3.0

Last updated