Advance Jab Lab Programs
Advance Jab Lab Programs
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<String> fruits = new ArrayList<>();
// Adding elements
fruits.add("Apple");
fruits.add("Mango");
fruits.add("Banana");
fruits.add("Grapes");
// Removing an element
fruits.remove("Banana");
System.out.println("After removing Banana: " + fruits);
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 a comparator.
import java.util.*;
// Custom Comparator to sort based on the tens place
class TensPlaceComparator implements Comparator<Integer> {
public int compare(Integer num1, Integer num2) {
int tensPlace1 = (num1 / 10) % 10;
int tensPlace2 = (num2 / 10) % 10;
return Integer.compare(tensPlace1, tensPlace2);
}
}
// Input range
System.out.print("Enter the lower bound of the range: ");
int lowerBound = scanner.nextInt();
System.out.print("Enter the upper bound of the range: ");
int upperBound = scanner.nextInt();
import java.util.*;
//User-defined class
class Student {
private int id;
private String name;
private double grade;
public Student(int id, String name, double grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public String toString() {
return "Student{ID=" + id + ", Name='" + name + "', Grade=" + grade + "}";
}
}
Program 4
Problem statement
Implement a java program to illustrate the use of different types of string class
constructors.
Code
public class Main1 {
public static void main(String[] args) {
// 1. Default constructor
String str1 = new String();
System.out.println("str1: '" + str1 + "'");
// 6. String from integer array (code points) with offset and count
int[] codePoints = {72, 101, 108, 108, 111};
String str6 = new String(codePoints, 0, 5);
System.out.println("str6: " + str6);
Output:
str1: ''
str2: ABCD
str3: BC
str4: Hello
str5: ell
str6: Hello
str7: Example
str8: BufferedString
str9: BuilderString
Program 5
Problem Statement
Implement a java program to illustrate the use of different types of character extraction,
string comparison, string search and string modification methods.
// 1. length()
System.out.println("Length of the string: " + str.length());
// 2. trim()
System.out.println("Trimmed string: '" + str.trim() + "'");
// 3. toUpperCase()
System.out.println("Uppercase string: " + str.toUpperCase());
// 4. toLowerCase()
System.out.println("Lowercase string: " + str.toLowerCase());
// 5. charAt()
System.out.println("Character at index 5: " + str.charAt(5));
// 6. contains()
System.out.println("Contains 'Java': " + str.contains("Java"));
// 7. startsWith()
System.out.println("Starts with ' Java': " + str.startsWith(" Java"));
// 8. endsWith()
System.out.println("Ends with 'fun!': " + str.endsWith("fun!"));
// 9. indexOf()
System.out.println("Index of 'Programming': " + str.indexOf("Programming"));
// 10. lastIndexOf()
System.out.println("Last index of 'a': " + str.lastIndexOf('a'));
// 11. equals()
String str2 = "Java Programming is fun!";
System.out.println("Strings are equal: " + str.equals(str2));
// 12. equalsIgnoreCase()
System.out.println("Strings are equal (ignoring case): " + str.equalsIgnoreCase("java
programming is fun!"));
// 13. replace()
System.out.println("Replace 'Java' with 'C++': " + str.replace("Java", "C++"));
// 14. substring()
System.out.println("Substring from index 5: " + str.substring(5));
// 16. split()
String[] words = str.split(" ");
System.out.println("Split string by spaces: ");
for (String word : words) {
System.out.println(word);
}
// 17. valueOf()
int num = 123;
System.out.println("String representation of number: " + String.valueOf(num));
// 18. concat()
System.out.println("Concatenated string: " + str.concat(" Let's code more!"));
// 19. isEmpty()
System.out.println("Is the string empty? " + str.isEmpty());
// 20. replaceAll()
// 21. compareTo()
System.out.println("Compare to another string: " + str.compareTo(str2));
// 22. compareToIgnoreCase()
System.out.println("Compare to another string (ignore case): " +
str.compareToIgnoreCase("java programming is fun!"));
}
}
Output
Length of the string: 28
Trimmed string: 'Java Programming is fun!'
Uppercase string: JAVA PROGRAMMING IS FUN!
Lowercase string: java programming is fun!
Character at index 5: a
Contains 'Java': true
Starts with ' Java': true
Ends with 'fun!': false
Index of 'Programming': 7
Last index of 'a': 12
Strings are equal: false
Strings are equal (ignoring case): false
Replace 'Java' with 'C++': C++ Programming is fun!
Substring from index 5: a Programming is fun!
Substring from index 5 to 15: a Programm
Split string by spaces:
Java
Programming
is
fun!
String representation of number: 123
Concatenated string: Java Programming is fun! Let's code more!
Is the string empty? False
Replace all 'a' with 'o': Jovo Progromming is fun!
Compare to another string: -42
Compare to another string (ignore case): -74
Program 6
Problem Statement
Implement a java program to illustrate the use of different types of StringBuffer methods
public class Main {
public static void main(String[] args) {
// Creating a StringBuffer
StringBuffer sb = new StringBuffer(" Java Programming is fun! ");
sb.deleteCharAt(5); // 6. deleteCharAt()
System.out.println("After deleteCharAt: " + sb);
sb.reverse(); // 8. reverse()
System.out.println("After reverse: " + sb);
sb.setLength(10); // 9. setLength()
System.out.println("After setLength: " + sb);
// 15. ensureCapacity()
sb.ensureCapacity(50);
System.out.println("After ensureCapacity(50), capacity: " + sb.capacity());
}
}
Output
Length of the StringBuffer: 28
Capacity of the StringBuffer: 44
After append: Java Programming is fun! Let's code more!
After insert: JavAwesome a Programming is fun! Let's code more!
After delete: Java Programming is fun! Let's code more!
After deleteCharAt: Jav Programming is fun! Let's code more!
After replace: JavProgrammingng is fun! Let's code more!
After reverse: !erom edoc s'teL !nuf si gngnimmargorPvaJ
After setLength: !erom edoc
Character at index 5:
Index of 'Java': -1
Last index of 'a': -1
Substring from index 5: edoc
StringBuffer to String: !erom edoc
After ensureCapacity(50), capacity: 90
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.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public AlphaBetaButtons() {
// Set the title of the frame
setTitle("Alpha Beta Button Demo");
// Frame settings
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center the window
setVisible(true);
}
Program 11:
Write a JAVA Program to insert data into Student DATA BASE and retrieve info based on
particular queries(For example update, delete, search etc…).
USE studentdb;
import java.sql.*;
import java.util.Scanner;
public class StudentDBOperations {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/studentDB"; // DB URL
String user = "root"; // Replace with your MySQL username
String password = "Admin123"; // Replace with your MySQL password
Scanner sc = new Scanner(System.in);
do {
System.out.println("\n--- Student Database Menu ---");
System.out.println("1. Insert");
System.out.println("2. Update");
System.out.println("3. Delete");
System.out.println("4. Search");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Branch: ");
String branch = sc.nextLine();
System.out.print("Enter Marks: ");
float marks = sc.nextFloat();
String insertQuery = "INSERT INTO Students VALUES (?, ?, ?, ?)";
PreparedStatement insertStmt = con.prepareStatement(insertQuery);
insertStmt.setInt(1, id);
insertStmt.setString(2, name);
insertStmt.setString(3, branch);
insertStmt.setFloat(4, marks);
insertStmt.executeUpdate();
System.out.println("Record Inserted Successfully.");
break;
case 2:
System.out.print("Enter ID to update: ");
searchStmt.setInt(1, sid);
ResultSet rs = searchStmt.executeQuery();
if (rs.next()) {
System.out.println("ID: " + rs.getInt("id"));
System.out.println("Name: " + rs.getString("name"));
System.out.println("Branch: " + rs.getString("branch"));
System.out.println("Marks: " + rs.getFloat("marks"));
} else {
System.out.println("Record Not Found.");
}
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
} while (choice != 5);
con.close();
sc.close();
}
}