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

ื˜ื‘ืœืช ืžืขืงื‘

track-table

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 ืืช:

log2x=answerlog_2x=answer

ืงื•ื“ื ื›ืœ ื‘ื ื ื‘ื™ืŸ ืžื” ืงื•ืจื” ื›ืืŸ

23=2โˆ—2โˆ—2=82^3=2*2*2=8
log28=3log_28=3

ืื ื”ื•ื ืœื ื—ื–ืงื” ืฉืœืžื” ื”ื“ืคืก ืืช ื—ื–ืงื” ืฉืœืžื” ื•ืขื•ื“ .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

Find The Slope Of A Line That Passess Through 2 Points - YouTube

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

Was this helpful?