0% found this document useful (0 votes)
4 views10 pages

Java Inner Classes and String Manipulation

Uploaded by

yashasvitemp64
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Java Inner Classes and String Manipulation

Uploaded by

yashasvitemp64
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical – 1

Aim : Write a Program to show the concept of static inner class

Source code:

public class College {


static String message = "College reopens on 27th Oct!";

static class NoticeBoard


{ void showNotice() {
[Link](message);
}
}

public static void main(String[] args) {


[Link] board = new [Link]();
[Link]();
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical 2

AIM – WAP to show the concept of non static inner class

Source code:

public class Outer


{ String name =
"Disha"; class Inner {
void show()
{ [Link]("Hello " +
name);
}
}
public static void main(String[] args) {
// Create Outer class object
Outer outerObj = new Outer();
// Use outer object to create Inner class object
[Link] innerObj = [Link] Inner();
// Call method of inner class
[Link]();
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 3

AIM- Create an anonymous inner class with the help of class

Source code:
class Animal {
//Parent Class
void sound() {
[Link]("Animal makes sound");
}
}
public class AnonymousInnerClassDemo
{ public static void main(String[] args) {
// Creating anonymous inner class
Animal myAnimal = new Animal() {
void sound()
{ [Link]("Dog
barks");
}
};
[Link]();
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 4

AIM- Create an anonymous inner class with the help of an interface

Source code:

interface Greeting {// Interface


void sayHello();
}
public class AnonymousInterfaceDemo
{ public static void main(String[] args) {
// Anonymous inner class implementing Greeting interface
Greeting greet = new Greeting() {
public void sayHello() {
[Link]("Hello guys! Welcome to THE DISHA SHOW.");
}
};
// Call the method
[Link]();
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical - 5

Aim – WAP to pass and return an array to a method

Source code:
public class SimpleArray {
// takes an array and returns a new array with +1 added
static int[] addOne(int[] arr) {
int[] newArr = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
newArr[i] = arr[i] + 1;
}
return newArr;
}
public static void main(String[] args) {
int[] numbers = {25, 27, 7}; // Original array
int[] result = addOne(numbers); // Pass array and get new array
[Link]("New Array:");
for (int num : result)
{ [Link](num + " ");
}
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 6

AIM – WAP to pass a class object to a method

Source code:
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
}
public class PassObjectDemo {
// Method that takes Student object as parameter
static void displayStudent(Student s) {
[Link]("Name: " + [Link]);
[Link]("Age: " + [Link]);
}
public static void main(String[] args) {
Student s1 = new Student("Disha'sIntuition", 2);
displayStudent(s1);
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 7

AIM- WAP to create String using all three methods

Source code:

public class StringCreationDemo


{ public static void main(String[]
args) {
// 1. Using string literal
String str1 = "Hello Disha";

//2. Using new keyword


String str2 = new String("Welcome to Java");

// [Link] character array


char[] letters = {'J', 'a', 'v', 'a'};
String str3 = new String(letters);
[Link]("String using literal: " + str1);
[Link]("String using new keyword: " + str2);
[Link]("String using char array: " + str3);
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 8

AIM – WAP to create StringBuffer objects and apply all kind of operators

Source code:
public class StringBufferDemo {
public static void main(String[] args) {
// Create StringBuffer object
StringBuffer sb = new
StringBuffer("Hello"); [Link](" Disha");
[Link]("After append: " + sb);
[Link](6, "Java "); // Insert text
[Link]("After insert: " + sb);
[Link](6, 10, "World");
[Link]("After replace: " + sb);
[Link](6, 11);
[Link]("After delete: " + sb);
[Link](); // Reverse text
[Link]("After reverse: " + sb);
[Link]("Capacity: " + [Link]()); // Capacity of buffer
[Link]("Length: " + [Link]());
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 9

Aim – WAP to create a String Tokenizer Class

Source code:

import [Link];
public class StringTokenizerDemo {
public static void main(String[] args) {
String sentence = "Ethical Hacking is still to learn"; // Input string
// Create StringTokenizer object
StringTokenizer st = new StringTokenizer(sentence);
// Print each token (word)
[Link]("Disha Khaneja words:");
while ([Link]()) {
String word = [Link]();
[Link](word);
}
}
}

Output:

NAME: DISHA ROLL ID: 23CE170


Practical – 28

AIM – WAP to create a thread using thread class and using runnable interface

Source code:
// Method 1: Using Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread running using Thread class");
}
}
// Method 2: Using Runnable interface
class MyRunnable implements Runnable {
public void run() {
[Link]("Thread running using Runnable interface");
}}
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread(); // using Thread class
[Link]();
MyRunnable r1 = new MyRunnable(); // using Runnable interface
Thread t2 = new Thread(r1);
[Link]();
}
}

Output:

NAME: DISHA ROLL ID: 23CE170

You might also like