DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Assignment 2
Computational Coding with Java Standard
Student Name: Parkhi Acchreja UID: 22BCS14694
Branch: CSE Date of Performance: 01/06/24
1) Write a code to demonstrate how bitwise operator works.
public class bitwise {
public static void main(String[] args) {
int num1 = 60; // 60 = 0011 1100
int num2 = 13; // 13 = 0000 1101
int result;
result = num1 & num2;
System.out.println("num1 & num2 = " + result );
result = num1 | num2;
System.out.println("num1 | num2 = " + result );
result = num1 ^ num2;
System.out.println("num1 ^ num2 = " + result );
result = ~num1;
System.out.println("~num1 = " + result );
result = num1 << 2; // shift left by 2 bits
System.out.println("num1 << 2 = " + result );
result = num1 >> 2; // shift right by 2 bits
System.out.println("num1 >> 2 = " + result );
result = num1 >>> 2; // shift right by 2 bits, preserving sign
System.out.println("num1 >>> 2 = " + result );
}
}
Code:-
Output:-
2) Write a code to demonstarte the uses of this keyword
public class this_keyword_usecases {
int x; // instance variable
// Constructor
this_keyword_usecases(int x) {
this.x = x; // using 'this' to refer to the instance variable
}
// Method
void display() {
System.out.println("Value of x: " + this.x); // using 'this' to refer to the instance
variable
}
// Inner class
class InnerClass {
void innerMethod() {
System.out.println("Value of x from inner class: " +this_keyword_usecases.this.x);
// using 'this' to refer to the outer class instance
}
}
public static void main(String[] args) {
this_keyword_usecases obj = new this_keyword_usecases(16);
obj.display();
this_keyword_usecases.InnerClass innerObj = obj.new InnerClass();
innerObj.innerMethod();
System.out.println("Parkhi Acchreja!");
}
}
Code:-
Output:-
3) How does static and local inner class work? Demonstrate.
4) Demonstrate the difference of reusing a method and overriding a method in
inheritance. Also discuss the uses of super keyword
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING