1. Divide, modulo, and auto-increment operations on two integer variables
(1) create the DivModDemo class in project MyProject2.
(2) enter the following code in the code editor:
The code is as follows: |
Copy code |
Public class DivModDemo {
/** * @ Param args */ Public static void main (String [] args ){ // TODO Auto-generated method stub Int a = 8, B = 5; System. out. println (a + "/" + B + "=" + a/B ); System. out. println (a + "/" + (-B) + "=" + a/-B ); System. out. println (-a + "/" + B + "=" +-a/B ); System. out. println (-a + "/" + (-B) + "=" + (-a)/(-B )); System. out. println (a + "%" + B + "=" + a % B ); System. out. println (a + "%" + (-B) + "=" + a %-B ); System. out. println (-a + "%" + B + "=" + (-a) % B ); System. out. println (-a + "%" + (-B) + "=" +-a %-B ); System. out. println (++ a + "+" + B + "=" + (++ a) + B ); System. out. println (a ++ "+" + (-B) + "=" + -- a +-B ); System. out. println (-++ a + "+" + B + "=" +-++ a + B ); System. out. println (-a ++ "+" + (-B) + "=" +-a ++-B ); System. out. println (); } } |
(3) run and debug the program and observe the running results of the DivModDemo class.
2. Binary moving algorithm
(1) create a BitShiftDemo class in project MyProject2.
(2) enter the following code in the code editor:
The code is as follows: |
Copy code |
Public class BitShiftDemo {
/** * @ Param args */ Public static void main (String [] args ){ // TODO Auto-generated method stub Int a =-16, B = 16; System. out. println ("******************************"); System. out. println ("The binary number of a is:" + Integer. toBinaryString ()); System. out. println ("a shifts 1 bit left and then all are" + Integer. toBinaryString (a <1 )); System. out. println (a + "<2 =" + (a <2 )); System. out. println ("******************************"); System. out. println ("The binary number of a is:" + Integer. toBinaryString ()); System. out. println ("a shifted to 1 after the right, everyone is" + Integer. toBinaryString (a> 1 )); System. out. println (a + "> 2 =" + (a> 2 )); System. out. println ("******************************"); System. out. println ("The binary number of a is:" + Integer. toBinaryString ()); System. out. println ("a shifted to 1 after the right, everyone is" + Integer. toBinaryString (a >>> 1 )); System. out. println (a + ">>> 2 =" + (a >>> 2 )); System. out. println ("******************************"); System. out. println ("The binary number of B is:" + Integer. toBinaryString (B )); System. out. println ("B shifts 1 bit left and then all are" + Integer. toBinaryString (B <1 )); System. out. println (B + "<2 =" + (B <2 )); System. out. println ("******************************"); System. out. println ("The binary number of B is:" + Integer. toBinaryString (B )); System. out. println ("B shifts one bit to the right and then all are" + Integer. toBinaryString (B> 1 )); System. out. println (B + "> 2 =" + (B> 2 )); System. out. println ("******************************"); System. out. println ("The binary number of B is:" + Integer. toBinaryString (B )); System. out. println ("B shifted to 1 after the right, you will be" + Integer. toBinaryString (B >>> 1 )); System. out. println (B + ">>> 2 =" + (B >>> 2 )); System. out. println ("******************************"); } } |
(3) run and debug the program and observe the results.
Tip: you can use Integer. toBinaryString () to convert the Integer variable to a binary character, and use <, right shift >>>>> to remove the binary value to the left. Is the unsigned right shift operator. After the variable value is shifted to the right, the left side is supplemented with 0.
3. Hybrid relational and logical operations
(1) create the OperationDemo class in project MyProject2.
(2) enter the following code in the code editor:
The code is as follows: |
Copy code |
Import java. io .*; Public class OperationDemo { /** * @ Param args */ Public static void main (String [] args ){ // TODO Auto-generated method stub Int a = 25, B = 3; Boolean d = a <B; System. out. println (a + "<" + B + "=" + d ); Int e = 3; D = (e! = 0 & a/e> 5 ); System. out. println (e + "! = 0 & "+ a +"/"+ e +"> 5 = "+ d ); Int f = 0; D = (f! = 0 & a/f> 5 ); System. out. println (f + "! = 0 & "+ a +"/"+ f +"> 5 = "+ d ); D = (f! = 0 & a/f> 5 ); System. out. println (f + "! = 0 & "+ a +"/"+ f +"> 5 = "+ d ); } } |
(3) run and debug and observe the program running result.
4. Input two integers from the keyboard. Use the following program to calculate the sum of the two integers and output the calculation result.
The code is as follows: |
Copy code |
Import java. io .*; Public class MySumDemo { /** * @ Param args */ Public static void main (String [] args ){ // TODO Auto-generated method stub Int num1, num2, sum; String str = ""; BufferedReader buf; Buf = new BufferedReader (new InputStreamReader (System. in )); System. out. print ("Input the first integer :"); Try { Str = buf. readLine (); } Catch (Exception e) { } Num1 = Integer. parseInt (str ); System. out. print ("Input the second integer :"); Try { Str = buf. readLine (); } Catch (Exception e) { } Num2 = Integer. parseInt (str ); Sum = num1 + num2; System. out. println ("The sum is" + sum ); } } |
5. Write a program that requires entering the circle radius on the keyboard, finding the area of the circle and outputting the result.
The code is as follows: |
Copy code |
Import java. io .*; Public class CircleArea { /** * @ Param args */ Public static void main (String [] args ){ // TODO Auto-generated method stub Double area; Int radius; Circle1 circle = null; String str = ""; BufferedReader buf; Buf = new BufferedReader (new InputStreamReader (System. in )); System. out. print ("Input the radius :"); Try { Str = buf. readLine (); } Catch (Exception e) { } Radius = Integer. parseInt (str ); Circle = new Circle1 (radius ); Area = circle. getArea (); System. out. println ("area of the circle:" + area ); } } Class Circle1 { Double radius, area; Circle1 (double r ){ Radius = r; } Double getArea (){ Area = 3.14159265 * radius; Return area; } } |
6. Debug the following program and analyze the program running result.
The code is as follows: |
Copy code |
/** * @ Author solo * */ Public class SanmuTest { /** * @ Param args */ Public static void main (String [] args ){ // TODO Auto-generated method stub Int iBig = 2; Int iSmall = 1; Boolean result = (iBig> = iSmall )? True: false; System. out. println ("Result Is:" + result ); } } |