Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators Laboratory 6 2018/11/16.

Similar presentations


Presentation on theme: "Operators Laboratory 6 2018/11/16."— Presentation transcript:

1 Operators Laboratory 6 2018/11/16

2 Arithmetic Operator Operator Description + addition - Subtraction
* Multiplication / Division % Modules ++ Increment Decrement 2018/11/16

3 Special operator Expression Equivalent expression a = a + 2; a +=2;
2018/11/16

4 Increment and Decrement
Expression Equivalent expression a = a + 1; ++a; a = a – 1; --a; result 2018/11/16

5 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

6 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 >> x0010 = 0x >>1 << 0x0001 = 0x <<1 >>> 0x1001 = 0x >>>1 2018/11/16

7 SHIFT >> (right) by one bit
(0xf2) >> 1 (shift right by one bit) (0x79) Example byte c = 0xf2; byte e = c >>1; //e is 0x79 2018/11/16

8 Program for Question 1 2018/11/16

9 sample 2018/11/16 /* demonstarting arithmetic operator */
public class lecture61 { public static void main(String[] args) { //a few numbers int x, y; x = /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

10 Prime number 2018/11/16

11 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

12 String to Double/Integer/Float
2018/11/16

13 Quadratic Equation 2018/11/16

14 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

15 Returning a value – from myarea.area()
2018/11/16

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

17 High-low 2018/11/16

18 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() * ); // 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


Download ppt "Operators Laboratory 6 2018/11/16."

Similar presentations


Ads by Google