1 Advanced Java [BIS402]
1. Implement a java program to demonstrate creating and ArrayList,
adding elements, removing elements, sorting elements of ArrayList.
Also illustrate the use of toArray() method.
ArrayListExample.java
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListExample
{
public static void main(String[] args)
{
// Create an ArrayList
ArrayList<String> AL = new ArrayList<>();
// Adding elements to the ArrayList
AL.add("C");
AL.add("C++");
AL.add("Java");
AL.add("Python");
AL.add("Sql");
// Display the ArrayList
System.out.println("Original ArrayList: " + AL);
// Removing an element from the ArrayList
AL.remove("Sql");
System.out.println("ArrayList after removing 'SQL': " + AL);
// Sorting the ArrayList
Collections.sort(AL);
System.out.println("Sorted ArrayList: " + AL);
// Converting ArrayList to Array
String[] s = AL.toArray(new String[0]);
// Display the elements of the array
System.out.println("Array elements: ");
for (String element : s)
{
System.out.println(element);
}
}
}
OUTPUT:
Original ArrayList: [C, C++, Java, Python, Sql]
ArrayList after removing 'SQL': [C, C++, Java, Python]
Sorted ArrayList: [C, C++, Java, Python]
Array elements:
C
C++
Java
Python
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 1
2 Advanced Java [BIS402]
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.
Explanation: This comparator focuses solely on the tens place digit.
If two numbers have the same tens digit, their relative order remains
as in the original list.
The tens place digits of the numbers are:YouTube
• 123: tens digit is 2
• 789: tens digit is 8
• 234: tens digit is 3
• 456: tens digit is 5
Sorting based on these digits results in the order: 123 (2), 234
(3), 456 (5), 789 (8).
TensPlaceComparator.java
import java.util.*;
public class TensPlaceComparator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: range
System.out.print("Enter lower bound: ");
int lower = scanner.nextInt();
System.out.print("Enter upper bound: ");
int upper = scanner.nextInt();
// Generate random numbers that are divisible by 10
List<Integer> numbers = new ArrayList<>();
Random random = new Random();
for (int i = lower; i <= upper; i++) {
if (i % 2 == 0 && i % 5 == 0) {
numbers.add(i);
}
}
// Print unsorted numbers
System.out.println("\nGenerated Numbers:");
for (int n : numbers) {
System.out.print(n + " ");
}
// Sort by tens place using Comparator
numbers.sort(new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
int tensA = (a / 10) % 10;
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 2
3 Advanced Java [BIS402]
int tensB = (b / 10) % 10;
return Integer.compare(tensA, tensB);
}
});
// Print sorted numbers
System.out.println("\n\nSorted by Tens Place:");
for (int n : numbers) {
System.out.print(n + " ");
}
}
}
OUTPUT:
Enter lower bound: 50
Enter upper bound: 150
Generated Numbers:
50 60 70 80 90 100 110 120 130 140 150
Sorted by Tens Place:
100 110 120 130 140 50 150 60 70 80 90
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 3
4 Advanced Java [BIS402]
3. Implement a java program to illustrate storing user
defined classes in collection.
Student.java
import java.util.*;
public class Student {
int id;
String name;
String branch;
// Constructor
public Student(int id, String name, String branch) {
this.id = id;
this.name = name;
this.branch = branch;
}
// Getter Methods
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getBranch() {
return branch;
}
public String toString() {
return "Student : ID=" + id + ", Name=" + name + ", Branch="
+ branch;
}
}
class StudentCollectionExample {
public static void main(String[] args) {
// Creating a List to store Student objects
List<Student> sl = new ArrayList<>();
// Adding Student objects to the collection
sl.add(new Student(101, "ANIL", "IS"));
sl.add(new Student(102, "SUNIL", "EC"));
sl.add(new Student(103, "PATIL", "CS"));
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 4
5 Advanced Java [BIS402]
// Displaying students using for-each loop
System.out.println("Student List:");
for (Student s : sl) {
System.out.println(s);
}
}
}
Output:
Student List:
Student : ID=101, Name=ANIL, Branch=IS
Student : ID=102, Name=SUNIL, Branch=EC
Student : ID=103, Name=PATIL, Branch=CS
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 5
6 Advanced Java [BIS402]
4. Implement a java program to illustrate the use of different types of string class
constructors.
StringConstructorsExample.java
public class StringConstructorsExample
{
public static void main(String[] args)
{
String a=new String();
System.out.println("EmptyString"+a);
char ch[]={'a','b','c','d'};
String b=new String(ch);
System.out.println("String with one argument as Char="+b);
String c=new String(ch,1,3);
System.out.println("String with Three argument as Char="+c);
String d=new String(b);
System.out.println("String with String object="+d);
byte e[]={65,66,67,68,69};
String f=new String(e);
System.out.println("byte to String="+f);
String g=new String(e,1,3);
System.out.println("byte to string for subbyte="+g);
StringBuffer h=new StringBuffer("BIET");
String i=new String(h);
System.out.println("StringBuffer to String="+i);
StringBuilder j=new StringBuilder("ISE");
String k=new String(j);
System.out.println("StringBuilder to Stirng="+k);
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 6
7 Advanced Java [BIS402]
int m[]={66,67,68,69,70};
String n=new String(m,1,3);
System.out.println("codepoint to String="+n);
}
}
OUTPUT:
EmptyString
String with one argument as Char=abcd
String with Three argument as Char=bcd
String with String object=abcd
byte to String=ABCDE
byte to string for subbyte=BCD
StringBuffer to String=BIET
StringBuilder to Stirng=ISE
codepoint to String=CDE
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 7
8 Advanced Java [BIS402]
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 StringMethods
{
public static void main(String[] args)
{
String example = "BIET DAVANAGERE";
// Character extraction methods
char charAtExample= example.charAt(7); //Extracts character at index 7
char charArrayExample[]= example.toCharArray();
//Converts the string to a char array
// String comparison methods
String str1 = "BIET DAVANAGERE";
String str2 = "bIET DAVANAGERE";
boolean equalsExample = str1.equals(str2); // Case-sensitive
comparison
boolean equalsIgnoreCaseExample = str1.equalsIgnoreCase(str2);
// Case-insensitive comparison
int compareToExample = str1.compareTo(str2); // Lexicographical
comparison
// String search methods
boolean containsExample = example.contains("BIET"); // Checks if the
string sequence
int indexOfExample = example.indexOf("A"); // Returns the index of the
first occurrence
int lastIndexOfExample = example.lastIndexOf('A'); // Returns the
index of the last occurrence
// String modification methods
String substringExample = example.substring(7); // Extracts a
substring starting from index 7
String replaceExample = example.replace("DAVANAGERE", "ISE"); //
Replaces DAVANAGERE " ISE "
String toLowerCaseExample = example.toLowerCase(); // Converts the
string to lower case
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 8
9 Advanced Java [BIS402]
String toUpperCaseExample = example.toUpperCase(); // Converts the
string to upper case
// Output the results
System.out.println("Original String: " + example);
System.out.println("charAt(7): " + charAtExample);
System.out.println("toCharArray(): " + new String(charArrayExample));
System.out.println("equals(comparisonStr1): " + equalsExample);
System.out.println("equalsIgnoreCase(comparisonStr1): " +
equalsIgnoreCaseExample);
System.out.println("compareTo(comparisonStr2): " + compareToExample);
System.out.println("contains(\"BIET\"): " + containsExample);
System.out.println("indexOf(\"A\"): " + indexOfExample);
System.out.println("lastIndexOf('A'): " + lastIndexOfExample);
System.out.println("substring(7): " + substringExample);
System.out.println("replace(\"DAVANAGERE \", \"ISE\"): " +
replaceExample);
System.out.println("toLowerCase(): " + toLowerCaseExample);
System.out.println("toUpperCase(): " + toUpperCaseExample);
}
}
OUTPUT:
Original String: BIET DAVANAGERE
charAt(7): V
toCharArray(): BIET DAVANAGERE
equals(comparisonStr1): false
equalsIgnoreCase(comparisonStr1): true
compareTo(comparisonStr2): -32
contains("BIET"): true
indexOf("A"): 6
lastIndexOf('A'): 10
substring(7): VANAGERE
replace("DAVANAGERE", "ISE"): BIET ISE
toLowerCase(): biet davanagere
toUpperCase(): BIET DAVANAGERE
6. Implement a java program to illustrate the use of different types of StringBuffer
methods.
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 9
10 Advanced Java [BIS402]
StringBufferExample.java
public class StringBufferExample
{
public static void main(String[] args)
{
// Creating a StringBuffer object
StringBuffer stringBuffer = new StringBuffer("BIET");
// append() method
stringBuffer.append(" ISE");
System.out.println("After append(): " + stringBuffer);
// insert() method
stringBuffer.insert(5, ", ");
System.out.println("After insert(): " + stringBuffer);
// delete() method
stringBuffer.delete(5, 7);
System.out.println("After delete(): " + stringBuffer);
// reverse() method
stringBuffer.reverse();
System.out.println("After reverse(): " + stringBuffer);
// capacity() method
System.out.println("Capacity of the StringBuffer: " + stringBuffer.capacity());
// length() method
System.out.println("Length of the StringBuffer: " + stringBuffer.length());
// charAt() method
char charAtIndex = stringBuffer.charAt(0);
System.out.println("Character at index 0: " + charAtIndex);
// indexOf() method
int indexOfSubstring = stringBuffer.indexOf("IS");
System.out.println("Index of 'IS': " + indexOfSubstring);
// substring() method
String substring = stringBuffer.substring(2, 5);
System.out.println("Substring from index 2 to 5: " + substring);
// replace() method
stringBuffer.replace(0, 2, "DVG");
System.out.println("After replace(): " + stringBuffer);
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 10
11 Advanced Java [BIS402]
// toString() method
String str = stringBuffer.toString();
System.out.println("String representation: " + str);
}
}
OUTPUT:
After append(): BIET ISE
After insert(): BIET , ISE
After delete(): BIET ISE
After reverse(): ESI TEIB
Capacity of the StringBuffer: 20
Length of the StringBuffer: 8
Character at index 0: E
Index of 'IS': -1
Substring from index 2 to 5: I T
After replace(): DVGI TEIB
String representation: DVGI TEIB
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 11
12 Advanced Java [BIS402]
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.
EventHandlingExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventHandlingExample
{
public static void main(String[] args) {
// Create a new JFrame container
JFrame f = new JFrame("Button Click Example");
f.setLayout(new FlowLayout());
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create buttons
JButton alphaButton = new JButton("Alpha");
JButton betaButton = new JButton("Beta");
// Create label
JLabel l = new JLabel("Click a button.");
// Add action listeners
alphaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.setText("Alpha pressed");
}
});
betaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.setText("Beta pressed");
}
});
// Add components to frame
f.add(alphaButton);
f.add(betaButton);
f.add(l);
// Set frame visibility
f.setVisible(true);
}
}
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 12
13 Advanced Java [BIS402]
OUTPUT:
Prof. Santhosh T, Department of IS&E, BIET, Davanagere 13