Operators Laboratory 6 2018/11/16
Arithmetic Operator Operator Description + addition - Subtraction * Multiplication / Division % Modules ++ Increment -- Decrement 2018/11/16
Special operator Expression Equivalent expression a = a + 2; a +=2; 2018/11/16
Increment and Decrement Expression Equivalent expression a = a + 1; ++a; a = a – 1; --a; result 2018/11/16
Bitwise Operators Operator Description ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift Right << Shift LEFT >>> Shift Right with zero fill 2018/11/16
Operation - examples Operator Expression AND 1 & 1 = 1; 1& 0 = 0 ~ 0 =~1; 1 =~0; ^ 0^ 0 = 0; 1^1 = 0; 1^0 =1; 0^1 = 1 >> 0x0010 = 0x0001 >>1 << 0x0001 = 0x0010 <<1 >>> 0x1001 = 0x0100 >>>1 2018/11/16
SHIFT >> (right) by one bit 1111 0010 (0xf2) >> 1 (shift right by one bit) --------------------- 0111 10001 (0x79) Example byte c = 0xf2; byte e = c >>1; //e is 0x79 2018/11/16
Program for Question 1 2018/11/16
sample 2018/11/16 /* demonstarting arithmetic operator */ public class lecture61 { public static void main(String[] args) { //a few numbers int x, y; x = 1 + 2 - 8/2*2; short j = 34; System.out.println("The value of x is ..."); System.out.println(" x = " + x); y = 5; x = y << 5; //using << System.out.println("demonstrating << "); x = 2; y = x++ + 2; //using ++ System.out.println("demonstrating ++ "); } 2018/11/16
Prime number 2018/11/16
Sample 2018/11/16 import java.io.*; public class lab62 { public static void main(String[] args) throws java.io.IOException InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s1; int num, loop; double sqr, num2; boolean prime = true; // read a number System.out.println("Please enter a positive integer:"); s1 = br.readLine(); num = Integer.parseInt(s1); if (num == 2) prime = true; else ........................ //please fill in if(prime) System.out.println(num + " is a prime number."); } 2018/11/16
String to Double/Integer/Float 2018/11/16
Quadratic Equation 2018/11/16
Sample 2018/11/16 import java.io.*; public class lab63 { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s1; double a, b, c; double x1, x2; // read a System.out.print("Please enter the value of a: "); s1 = br.readLine(); a = Double.valueOf(s1).doubleValue(); // convert string to double ..................... // please fill in System.out.println("When a = " + a + ", b = " + b + " and c = " + c + " X = " + x1 + " or " + x2); } 2018/11/16
Returning a value – from myarea.area() 2018/11/16
Sample 2018/11/16 import java.io.*; class Square { double width; double area() { return width*width; } class lab64 { public static void main(String[] argv) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr);double width, b; String s1; // read a number Square myarea = new Square(); //create an object System.out.print("Please enter the width: "); s1 = br.readLine(); b = Double.valueOf(s1).doubleValue(); // convert string to double myarea.width = b; //assign a value //compute the area double a = myarea.area(); //return the value System.out.print("area is " + a + "\n"); 2018/11/16
High-low 2018/11/16
Sample 2018/11/16 import java.io.*; public class lab65 { public static void main(String[] argv) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); int g, i; //guess and input int tries = 0; String s1; i = (int) (Math.random() * 100 + 1); // choose new random number between 1 and 100 // read a number System.out.print("Please enter the guess: "); s1 = br.readLine(); g = Integer.parseInt(s1); // convert string to integer while(g != i) { ......................... // please fill in g = Integer.parseInt(s1); } // Correct System.out.print("Correct!, You have tried " + tries +" times"); 2018/11/16