You are on page 1of 21

Name of Student : Madhur Mahajan PRN:22070122508

Semester : IV Year AY 22-23


Subject Title: OOP with Java
Assignment No : 3

14. WAP to demonstrate static block.

class Static { static


int num;

static {
System.out.println("Static Block 1");
num = 68;
}

static {
System.out.println("Static Block 2");
num = 98;
}

public static void main(String args[])


{
System.out.println("Value of num: "+num);

}
}
15. WAP to print the number of objects created for a class.

class Object
{
private static int count = 0;

public Object () {
count++;
}

public static int getCount() {return


count;
}

public static void main(String[] args) {Object


obj1 = new Object();
Object obj2 = new Object();
Object obj3 = new Object();
System.out.println("Number of objects created: " +
Object.getCount());
}

}
16. In Box class, add member function to find values of private variables

class Circle {

private int

radius;

public Circle(int radius)

{ this.radius = radius;

public int getRadius()

{ return radius;

public double area() {

double area;

area=3.14*radius*radi

us; return area;

public static void main(String[]

args) { Circle circle = new

Circle(40);

System.out.println("area: " + box.area());

}
17. In Box class, add member function to print volume of box.

class Box

private int

length; private

int width; private

int height;

public int getLength()

{ return length;

public int getWidth()

{ return width;

public int getHeight()

{ return height;

public Box(int length, int width, int

height) { this.length = length;

this.width = width;

this.height = height;

public void printVolume() {

int volume = length * width * height;


System.out.println("Volume: " + volume);

public static void main(String[] args)

{ Box box = new Box(40, 50,

60);

System.out.println("Length: " +

box.getLength()); System.out.println("Width: "

+ box.getWidth()); System.out.println("Height:

" + box.getHeight()); box.printVolume();

}
18. In Box class, add getter and setter methods for private variables.

class Box

private int

length; private

int width; private

int height;

public Box(int length, int width, int

height) { this.length = length;

this.width = width;

this.height =

height;

public int getLength()

{ return length;

public void setLength(int

length) { this.length = length;

public int getWidth()

{ return width;

public void setWidth(int width)

{ this.width = width;

public int getHeight()

{ return height;
}

public void setHeight(int

height) { this.height = height;

public static void main(String[] args)

{ Box box = new Box(40, 50,

60);

System.out.println("Length: " +

box.getLength()); System.out.println("Width: "

+ box.getWidth()); System.out.println("Height:

" + box.getHeight());

}
19. Execute Inner class-related program conducted during Saturday session

class OuterClass {

private int

outerValue; class

InnerClass {

private int innerValue;

public void displayOuterValue() {

System.out.println("Outer value: " +

outerValue);

public void setOuterValue(int

outerValue) { this.outerValue =

outerValue;

public void displayInnerValue() {

InnerClass inner = new

InnerClass(); inner.innerValue =

10;

System.out.println("Inner value: " + inner.innerValue);

public static void main(String[] args) {

OuterClass outer = new

OuterClass();

outer.setOuterValue(20);

OuterClass.InnerClass inner = outer.new

InnerClass(); inner.displayOuterValue();
}

}
20. Study questions given in below link:
21. WAP to enter an integer value, a character value, a double value and store

them in primitive datatypes. Use Wrapper class to convert these primitive

datatypes to objects of the class.

import java.util.Scanner;

public class

WrapperExample {

public static void main(String[] args) {

Scanner scanner = new

Scanner(System.in);

System.out.print("Enter an integer

value: "); int intValue = scanner.nextInt();

Integer intWrapper = Integer.valueOf(intValue);

System.out.println("Integer object: " +

intWrapper);

System.out.print("Enter a character

value: "); char charValue =

scanner.next().charAt(0);

Character charWrapper =

Character.valueOf(charValue);

System.out.println("Character object: " +

charWrapper);

System.out.print("Enter a double value:

"); double doubleValue =

scanner.nextDouble();
Double doubleWrapper =

Double.valueOf(doubleValue);

System.out.println("Double object: " +

doubleWrapper); scanner.close();

}
22. WAP to print ASCII values

public class Ascii {

public static void main(String[]

args) { for (int i = 32; i <= 126; i++)

char c = (char) i;

System.out.println("ASCII value of " + c + " is: " + i);

}
23. WAP to check if given number is palindrome or not

class Palindrome{

public static boolean isPalindrome(int number)

int reverse = 0, originalNumber =

number; while (number != 0) {

int lastDigit = number % 10;

reverse = (reverse * 10) +

lastDigit; number /= 10;

return originalNumber == reverse;

public static void main(String[] args )

int number = 121;

if (isPalindrome(number))

System.out.println(number + " is a palindrome");

else

System.out.println(number + " is not a palindrome");

}
}
24. WAP to check if given String is palindrome or not

class Palindrome{

public static boolean isPalindrome(String

str) { int length = str.length();

for (int i = 0; i < length / 2; i++) {

if (str.charAt(i) != str.charAt(length - i -

1)) { return false;

return true;

public static void main(String[] args )

String str = "level";

if (isPalindrome(str)) {

System.out.println(str + " is a palindrome");

} else {

System.out.println(str + " is not a palindrome");

}
25. WAP to create 2 variables as final and try to change their value to see the error.

public class Main {

public static void main(String[]

args) { final int x = 10;

final double y = 20.5;

x = 15; // compilation error

y = 25.5; // compilation error

}
26. WAP to perform linear search.

public class linearSearch {

public static int linearSearch(int[] array, int

key) { for (int i = 0; i < array.length; i++) {

if (array[i] == key)

{ return i;

return -1;

public static void main(String[]

args) { int[] numbers = {1, 2, 3, 4,

5};

int key = 3;

int index = linearSearch(numbers,

key); if (index == -1) {

System.out.println("Key not found");

} else {

System.out.println("Key found at index: " + index);

}
27. WAP to swap 2 numbers using call by value in Java. How swapping using

call by reference will be performed in Java? Demonstrate with a program

public class
Swap {

public static void swap(int a, int

b) { int temp = a;

a = b;

b = temp;

public static void main(String[]

args) { int x = 10;

int y = 20;

System.out.println("Before swapping: x = " + x + ", y = "

+ y); swap(x,y);

System.out.println("After swapping: x = " + x + ", y = " + y);

}
28. WAP to demonstrate ragged array in Java.

public class
RaggedArray {

public static void main(String[]

args) { int[][] raggedArray = new

int[3][]; raggedArray[0] = new

int[3]; raggedArray[1] = new

int[4]; raggedArray[2] = new

int[2];

for (int i = 0; i < 3; i++) {

for (int j = 0; j < raggedArray[i].length;

j++) { raggedArray[i][j] = i + j;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < raggedArray[i].length;

j++) { System.out.print(raggedArray[i][j]

+ " ");

System.out.println();

You might also like