← Back to Midterms

Java Operators

Explanation & Learnings

This activity was all about mastering Java operators. I was tasked with creating a program utilizing different categories of operators such as bitwise, ternary, and assignment operators.

bitwiseop.java
public class bitwiseop {
    public static void main(String[] args) {
        //Bitwise AND on booleans (produces true)
        boolean boolVal = true & true;

        //Bitwise AND on integers (14 & 11 = 10)
        //14: 1110, 11: 1011 -> Result: 1010 (10)
        int intVal1 = 14 & 11;

        //Bitwise XOR on booleans (true ^ true = false)
        boolean boolVal2 = true ^ true;

        //Bitwise OR on integers (13 | 11 = 15)
        //13: 1101, 11: 1011 -> Result: 1111 (15)
        int intVal2 = 13 | 11;

        System.out.println(boolVal + ", " + intVal1 + ", " + boolVal2 + ", " + intVal2);
    }
}
Terminal
true, 10, false, 15

Part I – Question 1: Bitwise Operators

For this question, I had to produce the output: true, 10, false, 15 using bitwise operators. I used bitwise AND on booleans to get true, AND on integers 14 and 11 to get 10, XOR on two true booleans to get false, and OR on 13 and 11 to get 15. The trickiest part was thinking in binary — I had to manually trace the bit patterns to make sure the operations would produce the correct results.

ternaryop.java
public class ternaryop {
    public static void main(String[] args) {
        boolean first = (5 > 2) ? true : false;      // true
        boolean second = (10 < 1) ? true : false;    // false
        int third = (20 == 20) ? 25 : 50;            // 25
        int fourth = (100 != 100) ? 10 : 30;         // 30

        System.out.println(first + ", " + second + ", " + third + ", " + fourth);
    }
}
Terminal
true, false, 25, 30

Part I – Question 2: Ternary Operators

This question required me to output: true, false, 25, 30 using the ternary operator. Each value came from a condition being evaluated — whether it was true or false determined which value got assigned to the variable. It was a good exercise in writing compact conditional logic, and it made me appreciate how the ternary operator can replace simple if-else statements in a single readable line.

assignop.java
public class assignop {
    public static void main(String[] args) {
        int a = 53; a += 67;  // Addition: 120
        int b = 110; b -= 40; // Subtraction: 70
        int c = 12;  c *= 12; // Multiplication: 144
        int d = 64;  d /= 4;  // Division: 16

        System.out.println("Addition: " + a);
        System.out.println("Subtraction: " + b);
        System.out.println("Multiplication: " + c);
        System.out.println("Division: " + d);
    }
}
Terminal
Addition: 120
Subtraction: 70
Multiplication: 144
Division: 16
          

Part I – Question 3: Assignment Operators

For this question, I used compound assignment operators to produce: Addition: 120, Subtraction: 70, Multiplication: 144, and Division: 16. Instead of writing out full expressions, I applied operators like +=, -=, *=, and /= directly onto the variables. It was straightforward but reinforced how these shorthand operators modify a variable in place rather than creating a new one.

program2.java
public class program2 {
    public static void main(String[] args) {
        //Output 1 (Numeric): Tricky unary case
        //Logic: k = 15. ++k (16) + k-- (16).
        int k = 15;
        int trickyResult = ++k + k--;
        System.out.println(trickyResult);

        //Output 2 (Numeric): Assignment with subtraction
        //Logic: 250 - 67 = 183
        int balance = 250;
        balance -= 67;
        System.out.println(balance);

        //Output 3 (Numeric): Arithmetic with multiplication and modulo
        //Logic: (12 * 4) % 7 -> 48 % 7 = 6
        int factor = 12;
        int modResult = (factor * 4) % 7;
        System.out.println(modResult);

        //Output 4 (numeric): assignment operator (*=) + arithmetic (*)
        int lantern = 6;
        lantern *= 11;
        System.out.println(lantern);

        //Output 5 (Boolean): Relational and Logical OR
        //Logic: (80 >= 100) is false || (15 != 9) is true. Result: true
        int score = 80;
        boolean bool1 = (score >= 100) || (15 != 9);
        System.out.println(bool1);

        //Output 6 (Boolean): Logical NOT and Relational
        //Logic: !(67 < 25) -> !(false) -> true
        int level = 67;
        boolean bool2 = !(level < 25);
        System.out.println(bool2);
    }
}
Terminal
32
183
6
66
true
true
          

Part II: Multi-Operator Program

Part II required combining at least five operator categories into one program with exactly six outputs — four numeric and two boolean. The most challenging part was the tricky unary case using ++k + k--, where I had to trace the order of evaluation carefully since pre-increment and post-decrement behave differently within the same expression. Planning each output ahead of time before writing the code helped me stay within the six-output limit and meet all the requirements without overlap.