Ad Stu Lab
Ad Stu Lab
PROGRAM
import java.util.ArrayList;
import java.util.Collections;
// Creating an ArrayList
arrayList.add(5);
arrayList.add(10);
arrayList.add(3);
arrayList.add(8);
arrayList.remove(Integer.valueOf(3));
System.out.println(item);
OUTPUT:
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;
System.out.println(num);
return numbers;
filteredNumbers.add(num);
return filteredNumbers;
}
// Comparator to sort numbers according to tens place
@Override
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;
class Student {
// Constructor
this.name = name;
this.rollNumber = rollNumber;
// Getter methods
return name;
return rollNumber;
@Override
return "Student{" +
"name='" + name + '\'' +
'}';
System.out.println("Students:");
System.out.println(student);
scanner.close();
}
OUTPUT:
John
1001
Ram
1002
Seth
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;
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 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
OUTPUT
String 1:
String 2: Hello
String 3: World
String 4: Wor
String 5: Hello
String 6: Hel
PROGRAM CODE:
import java.util.Arrays;
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 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
OUTPUT
String 1:
String 2: Hello
String 3: World
String 4: Wor
String 5: Hello
String 6: Hel
// 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 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");
// 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");
}
}
OUTPUT: