3. Implement a java program to illustrate storing user defined classes in collection.
import [Link];
class Student {
private String name;
private int age;
public Student(String name, int age) {
[Link] = name;
[Link] = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class Main {
public static void main(String[] args) {
// Create ArrayList to store Student objects
ArrayList<Student> studentList = new ArrayList<>();
// Adding Student objects to the ArrayList
[Link](new Student("Alice", 20));
[Link](new Student("Bob", 22));
[Link](new Student("Charlie", 21));
// Displaying the contents of the ArrayList
[Link]("Students in the list:");
for (Student student : studentList) {
[Link](student);
}
}
}
Output:
Students in the list:
Student [name=Alice, age=20]
Student [name=Bob, age=22]
Student [name=Charlie, age=21]
4. Implement a java program to illustrate the use of different types of string class
constructors.
public class StringConstructorDemo {
public static void main(String[] args) {
// Using a string literal
String str1 = "Hello, World!";
[Link]("String created using a string literal: " + str1);
// Using a character array
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str2 = new String(charArray);
[Link]("String created using a character array: " + str2);
// Using a portion of a character array
String str3 = new String(charArray, 2, 3); // Start index: 2, Length: 3
[Link]("String created using a portion of a character array: " + str3);
// Using another string
String str4 = new String(str1);
[Link]("String created using another string: " + str4);
// Using bytes and character encoding
byte[] byteArray = {72, 101, 108, 108, 111};
// ASCII values for "Hello"
String str5 = new String(byteArray);
[Link]("String created using bytes and character encoding: " + str5);
// Using bytes, character encoding, and specifying a subrange
String str6 = new String(byteArray, 1, 4);
// Start index: 1, Length: 4
[Link]("String created using bytes, character encoding, and specifying a
subrange: " + str6);
}
}
Output:
String created using a string literal: Hello, World!
String created using a character array: Hello
String created using a portion of a character array: llo
String created using another string: Hello, World!
String created using bytes and character encoding: Hello
String created using bytes, character encoding, and specifying a subrange: ello
5. Implement a java program to illustrate the use of different types of character extraction, string
comparison, string search and string modification methods.
public class StringMethodsDemo {
public static void main(String[] args) {
// Character extraction methods
String str = "Hello, World!";
[Link]("Original String: " + str);
[Link]("Character at index 4: " + [Link](4));
// Extracting character at index 4
[Link]("Substring from index 7 to end: " + [Link](7));
// Extracting substring from index 7 to end
[Link]("Substring from index 0 to 5: " + [Link](0, 5));
// Extracting substring from index 0 to 5
// String comparison methods
String str1 = "Hello";
String str2 = "hello";
[Link]("Comparing 'Hello' with 'hello': " + [Link](str2));
//Comparing case-sensitive
[Link]("Comparing 'Hello' with 'hello' (ignore case): " + [Link](str2));
// Comparing ignoring case
// String search methods
[Link]("Index of 'World': " + [Link]("World"));
// Finding the index of substring "World"
[Link]("Index of 'l' after index 5: " + [Link]('l', 5));
// Finding the index of character 'l' after index 5
[Link]("Last index of 'l': " + [Link]('l'));
// Finding the last index of character 'l'
// String modification methods
[Link]("Replacing 'World' with 'Java': " + [Link]("World", "Java"));
// Replacing substring "World" with "Java"
[Link]("Uppercase: " + [Link]()); // Converting to uppercase
[Link]("Lowercase: " + [Link]()); // Converting to lowercase
[Link]("Trimming whitespace: " + " Hello, World! ".trim()); // Trimming
leading and trailing whitespace
}
}
Output:
Original String: Hello, World!
Character at index 4: o
Substring from index 7 to end: World!
Substring from index 0 to 5: Hello
Comparing 'Hello' with 'hello': -32
Comparing 'Hello' with 'hello' (ignore case): 0
Index of 'World': 7
Index of 'l' after index 5: 10
Last index of 'l': 10
Replacing 'World' with 'Java': Hello, Java!
Uppercase: HELLO, WORLD!
Lowercase: hello, world!
Trimming whitespace: Hello, World!
6. Implement a java program to illustrate the use of different types of StringBuffer methods
public class StringBufferMethodsDemo {
public static void main(String[] args) {
// Creating a StringBuffer object
StringBuffer stringBuffer = new StringBuffer("Hello");
// Append method
[Link](" World");
[Link]("After appending: " + stringBuffer);
// Insert method
[Link](5, ", ");
[Link]("After inserting: " + stringBuffer);
// Delete method
[Link](5, 8);
[Link]("After deleting: " + stringBuffer);
// Reverse method
[Link]();
[Link]("After reversing: " + stringBuffer);
// Replace method
[Link](0, 5, "Hola");
[Link]("After replacing: " + stringBuffer);
// Capacity method
[Link]("Capacity of StringBuffer: " + [Link]());
// Length method
[Link]("Length of StringBuffer: " + [Link]());
// EnsureCapacity method
[Link](50);
[Link]("Capacity after ensuring: " + [Link]());
// SetLength method
[Link](10);
[Link]("StringBuffer after setting length: " + stringBuffer);
}
}
Output:
After appending: Hello World
After inserting: Hello, World
After deleting: HelloWorld
After reversing: dlroWolleH
After replacing: HolaolleH
Capacity of StringBuffer: 21
Length of StringBuffer: 9
Capacity after ensuring: 50
StringBuffer after setting length: HolaolleH