ADVANCED JAVA LAB EXPERIMENTS
PROGRAM 1.Implement a java program to demonstrate creating an ArrayList, adding
elements, removing elements,sorting elements of ArrayList. Also illustrate the use of
toArray() method.
PROGRAM
import java.util.ArrayList;
import java.util.Collections;
public class NewArray {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<Integer> arrayList = new ArrayList<>();
// Adding elements to the ArrayList
arrayList.add(5);
arrayList.add(10);
arrayList.add(3);
arrayList.add(8);
// Displaying the elements of the ArrayList
System.out.println("ArrayList elements: " + arrayList);
// Removing an element from the ArrayList
arrayList.remove(Integer.valueOf(3));
// Displaying the elements after removing
System.out.println("ArrayList after removing element: " + arrayList);
// Sorting the elements of the ArrayList
Collections.sort(arrayList);
// Displaying the elements after sorting
System.out.println("ArrayList after sorting: " + arrayList);
// Converting ArrayList to Array
Integer[] array = arrayList.toArray(new Integer[0]);
// Displaying the elements of the Array
System.out.println("Array elements: ");
for (Integer item : array) {
System.out.println(item);
OUTPUT:
ArrayList elements: [5, 10, 3, 8]
ArrayList after removing element: [5, 10, 8]
ArrayList after sorting: [5, 8, 10]
Array elements:
10
PROGRAM 2: Develop a program to read random numbers between a
given range that are multiples of 2 and 5, sort the numbers according to
tens place using comparator.
PROGRAM
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
public class NumberSorter {
public static void main(String[] args) {
int minRange = 100; // Minimum range value
int maxRange = 1000; // Maximum range value
int count = 10; // Number of random numbers to generate
// Generate random numbers
List<Integer> numbers = generateRandomNumbers(minRange, maxRange, count);
// Filter multiples of 2 and 5
List<Integer> filteredNumbers = filterMultiplesOf2And5(numbers);
// Sort numbers according to tens place using comparator
Collections.sort(filteredNumbers, new TensPlaceComparator());
// Display sorted numbers
System.out.println("Sorted Numbers:");
for (Integer num : filteredNumbers) {
System.out.println(num);
// Method to generate random numbers in a given range
private static List<Integer> generateRandomNumbers(int minRange, int maxRange, int
count) {
Random random = new Random();
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < count; i++) {
numbers.add(random.nextInt(maxRange - minRange + 1) + minRange);
return numbers;
// Method to filter numbers that are multiples of 2 and 5
private static List<Integer> filterMultiplesOf2And5(List<Integer> numbers) {
List<Integer> filteredNumbers = new ArrayList<>();
for (Integer num : numbers) {
if (num % 2 == 0 && num % 5 == 0) {
filteredNumbers.add(num);
return filteredNumbers;
}
// Comparator to sort numbers according to tens place
static class TensPlaceComparator implements Comparator<Integer> {
@Override
public int compare(Integer num1, Integer num2) {
int tensPlace1 = (num1 % 100) / 10; // Extract tens place of num1
int tensPlace2 = (num2 % 100) / 10; // Extract tens place of num2
return tensPlace1 - tensPlace2;
OUTPUT:
Sorted Numbers:
960
190
Program 3: Implement a java program to illustrate storing user defined classes in
collection.
PROGRAM
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Define the Student class
class Student {
private String name;
private int rollNumber;
// Constructor
public Student(String name, int rollNumber) {
this.name = name;
this.rollNumber = rollNumber;
// Getter methods
public String getName() {
return name;
public int getRollNumber() {
return rollNumber;
// Override toString method for better representation
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", rollNumber=" + rollNumber +
'}';
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student> studentList = new ArrayList<>();
// Read input from the user
for (int i = 1; i <= 3; i++) {
System.out.println("Enter name for Student " + i + ":");
String name = scanner.nextLine();
System.out.println("Enter roll number for Student " + i + ":");
int rollNumber = Integer.parseInt(scanner.nextLine());
studentList.add(new Student(name, rollNumber));
// Display the students stored in the list
System.out.println("Students:");
for (Student student : studentList) {
System.out.println(student);
scanner.close();
}
OUTPUT:
Enter name for Student 1:
John
Enter roll number for Student 1:
1001
Enter name for Student 2:
Ram
Enter roll number for Student 2:
1002
Enter name for Student 3:
Seth
Enter roll number for Student 3:
1003
Students:
Student{name='John', rollNumber=1001}
Student{name='Ram', rollNumber=1002}
Student{name='Seth', rollNumber=1003}
PROGRAM 4: Implement a java program to illustrate the use of different types of
string class constructors.
PROGRAM CODE:
import java.util.Arrays;
public class StringConstructorExample {
public static void main(String[] args) {
// Creating strings using different constructors
String str1 = new String(); // Using default constructor
String str2 = new String("Hello"); // Using constructor with string parameter
char[] charArray = {'W', 'o', 'r', 'l', 'd'};
String str3 = new String(charArray); // Using constructor with char array parameter
String str4 = new String(charArray, 0, 3); // Using constructor with char array, offset,
and length parameters
String str = "Hello";
byte[] byteArray = new byte[str.length()];
for (int i = 0; i < str.length(); i++) {
byteArray[i] = (byte) str.charAt(i);
String str5 = new String(byteArray); // Using constructor with byte array parameter
String str6 = new String(byteArray, 0, 3); // Using constructor with byte array, offset,
and length parameters
// Displaying the constructed strings
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
System.out.println("String 3: " + str3);
System.out.println("String 4: " + str4);
System.out.println("String 5: " + str5);
System.out.println("String 6: " + str6);
// Print ASCII values for "Hello"
System.out.println("ASCII values for \"Hello\": " + Arrays.toString(byteArray));
OUTPUT
String 1:
String 2: Hello
String 3: World
String 4: Wor
String 5: Hello
String 6: Hel
ASCII values for "Hello": [72, 101, 108, 108, 111]
PROGRAM 4: Implement a java program to illustrate the use of different types of
string class constructors.
PROGRAM CODE:
import java.util.Arrays;
public class StringConstructorExample {
public static void main(String[] args) {
// Creating strings using different constructors
String str1 = new String(); // Using default constructor
String str2 = new String("Hello"); // Using constructor with string parameter
char[] charArray = {'W', 'o', 'r', 'l', 'd'};
String str3 = new String(charArray); // Using constructor with char array parameter
String str4 = new String(charArray, 0, 3); // Using constructor with char array, offset,
and length parameters
String str = "Hello";
byte[] byteArray = new byte[str.length()];
for (int i = 0; i < str.length(); i++) {
byteArray[i] = (byte) str.charAt(i);
String str5 = new String(byteArray); // Using constructor with byte array parameter
String str6 = new String(byteArray, 0, 3); // Using constructor with byte array, offset,
and length parameters
// Displaying the constructed strings
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
System.out.println("String 3: " + str3);
System.out.println("String 4: " + str4);
System.out.println("String 5: " + str5);
System.out.println("String 6: " + str6);
// Print ASCII values for "Hello"
System.out.println("ASCII values for \"Hello\": " + Arrays.toString(byteArray));
OUTPUT
String 1:
String 2: Hello
String 3: World
String 4: Wor
String 5: Hello
String 6: Hel
ASCII values for "Hello": [72, 101, 108, 108, 111]
Program 6: Implement a java program to illustrate the use of different types of
StringBuffer methods
PROGRAM CODE:
public class StringBufferExample {
public static void main(String[] args) {
// Create a StringBuffer object
StringBuffer stringBuffer = new StringBuffer("Hello");
// Append method
stringBuffer.append(" World");
System.out.println("After append: " + stringBuffer);
// Insert method
stringBuffer.insert(5, ", ");
System.out.println("After insert: " + stringBuffer);
// Delete method
stringBuffer.delete(5, 8);
System.out.println("After delete: " + stringBuffer);
// Reverse method
stringBuffer.reverse();
System.out.println("After reverse: " + stringBuffer);
// Length method
System.out.println("Length of StringBuffer: " + stringBuffer.length());
// Capacity method
System.out.println("Capacity of StringBuffer: " + stringBuffer.capacity());
// SetLength method
stringBuffer.setLength(5);
System.out.println("After setLength: " + stringBuffer);
}
}
OUTPUT
After append: Hello World
After insert: Hello, World
After delete: HelloWorld
After reverse: dlroWolleH
Length of StringBuffer: 10
Capacity of StringBuffer: 21
After setLength: dlroW
Program 7: Demonstrate a swing event handling application that creates 2 buttons
Alpha and Beta and displays the text “Alpha pressed” when alpha button is clicked and
“Beta pressed” when beta button is clicked.
PROGRAM CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonEventHandlingExample extends JFrame implements ActionListener {
private JButton alphaButton, betaButton;
public ButtonEventHandlingExample() {
// Set frame properties
setTitle("Button Event Handling Example");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create buttons
alphaButton = new JButton("Alpha");
betaButton = new JButton("Beta");
// Add action listeners
alphaButton.addActionListener(this);
betaButton.addActionListener(this);
// Set layout
setLayout(new FlowLayout());
// Add buttons to the frame
add(alphaButton);
add(betaButton);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == alphaButton) {
JOptionPane.showMessageDialog(this, "Alpha pressed");
} else if (e.getSource() == betaButton) {
JOptionPane.showMessageDialog(this, "Beta pressed");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ButtonEventHandlingExample example = new ButtonEventHandlingExample();
example.setVisible(true);
});
}
}
OUTPUT: