Operators and Numbers

Objective:

In this assignment, you will create a Java program. Requirements or tasks are given below. This assignment will test your knowledge of Java OPERATORS and NUMBERS.

Tasks:

  1. Write a program that declares an integer a variable x, assigns the value 2 to it, and prints out the binary string version of the number (Integer.toBinaryString(x)). Now, use the left shift operator (<<) to shift by 1 and assign the result to x. Before printing the results, write a comment with the predicted decimal value and binary string. Now, print out x in decimal form and in binary notation.

    int x = 2;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x << 1; // Predicted decimal value: 4, binary string: 100
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    

    Perform the preceding exercise with the following values:

    int x = 9;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x << 1; // Predicted decimal value: 18, binary string: 10010
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    
    int x = 17;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x << 1; // Predicted decimal value: 34, binary string: 100010
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    
    int x = 88;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x << 1; // Predicted decimal value: 176, binary string: 10110000
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    
  2. Write a program that declares a variable x, and assigns 150 to it, and prints out the binary string version of the number. Now use the right shift operator (>>) to shift by 2 and assign the result to x. Write a comment indicating what you anticipate the decimal and binary values to be. Now print the value of x and the binary string.

    int x = 150;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x >> 2; // Predicted decimal value: 37, binary string: 100101
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    

    Perform the preceding exercise with the following values:

    int x = 225;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x >> 2; // Predicted decimal value: 56, binary string: 111000
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    
    int x = 1555;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x >> 2; // Predicted decimal value: 388, binary string: 110000100
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    
    int x = 32456;
    System.out.println("Binary string version of x: " + Integer.toBinaryString(x));
    x = x >> 2; // Predicted decimal value: 8114, binary string: 111111001010
    System.out.println("x in decimal form: " + x);
    System.out.println("x in binary notation: " + Integer.toBinaryString(x));
    
  3. Write a program that declares three int variables: x, y, and z. Assign 7 to x and 17 to y. Write a comment that indicates what you predict will be the result of the bitwise and operation on x and y. Now use the bitwise and operator to derive the decimal and binary values, and assign the result to z.

    int x = 7, y = 17;
    // Predicted result: decimal: 1, binary: 1
    int z = x & y;
    System.out.println("z in decimal form: " + z);
    System.out.println("z in binary notation: " + Integer.toBinaryString(z));
    

    Now, with the preceding values, use the bitwise and operator to calculate the “or” value between x and y. As before, write a comment that indicates what you predict the values to be before printing them out.

    int x = 7, y = 17;
    // Predicted result: decimal: 23, binary: 10111
    int z = x | y;
    System.out.println("z in decimal form: " + z);
    System.out.println("z in binary notation: " + Integer.toBinaryString(z));
    
  4. Write a program that declares an integer variable, assigns a number, and uses a postfix increment operator to increase the value. Print the value before and after the increment operator.

    int x = 5;
    System.out.println("x before increment: " + x);
    x++;
    System.out.println("x after increment: " + x);
    
  5. Write a program that demonstrates at least three ways to increment a variable by 1 and does this multiple times. Assign a value to an integer variable, print it, increment by 1, print it again, increment by 1, and then print it again.

    int x = 3;
    System.out.println("x before increment: " + x);
    x = x + 1;
    System.out.println("x after increment: " + x);
    x += 1;
    System.out.println("x after increment: " + x);
    x++;
    System.out.println("x after increment: " + x);
    
  6. Write a program that declares two integer variables: x, and y, and then assigns 5 to x and 8 to y. Create another variable sum and assign the value of ++x added to y, and print the result. Notice the value of the sum (should be 14). Now change the increment operator to postfix (x++) and re-run the program. Notice what the value of the sum is. The first configuration incremented x, and then calculated the sum, while the second configuration calculated the sum, and then incremented x.

    int x = 5, y = 8;
    int sum = ++x + y;
    System.out.println("sum: " + sum);
    
    int x = 5, y = 8;
    int sum = x++ + y;
    System.out.println("sum: " + sum);

Last updated