KEMBAR78
Java Practical | PDF | Anonymous Function | Class (Computer Programming)
0% found this document useful (0 votes)
29 views13 pages

Java Practical

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

Java Practical

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Practical 1:

Aim : a) Write a program to sort a list of employees based on their salary using Comparable
and Comparator interfaces.
b) Write a Java program to get the first and last occurrence of the specified elements in a
linked list.
c) Develop a program that stores and displays country names in sorted order using
TreeSet.

Theory :
a) Comaparable and Comparator Interface :
1. Comparable Interface
 Purpose: The Comparable interface is used to impose a natural ordering on the objects of a
class. It’s typically implemented in the class whose objects need to be ordered.
 Method: It has a single method, compareTo, which takes an object as a parameter and returns
an integer.
o public int compareTo(T o);
 Return Values of compareTo:
o Returns 0 if the objects are equal.
o Returns positive if the current object is greater than the specified object.
o Returns negative if the current object is less than the specified object.
 Usage: Ideal when there’s a single, natural way to order instances (e.g., sorting String
lexicographically, or sorting Integer by value).

2. Comparator Interface
 Purpose: The Comparator interface is used to define multiple ways of ordering objects. It’s
useful when objects need to be sorted in ways other than the natural order.
 Method: It has a single method, compare, which takes two objects as parameters.
o public int compare(T o1, T o2);
 Return Values of compare:
o Returns 0 if the two objects are equal.
o Returns positive if the first object is greater than the second.
o Returns negative if the first object is less than the second.
 Usage: Allows creating different classes or anonymous inner classes to define multiple
comparison strategies.

Code :
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Employee implements Comparable<Employee> {


private int id;
private String name;
private double salary;

public Employee(int id, String name, double salary) {


this.id = id;
this.name = name;
this.salary = salary;
}

public double getSalary() {


return salary;
}

@Override
public int compareTo(Employee other) {
// Natural ordering based on salary
return Double.compare(this.salary, other.salary);
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}

// Static Comparator for salary comparison


public static Comparator<Employee> salaryComparator = new Comparator<Employee>()
{
@Override
public int compare(Employee e1, Employee e2) {
return Double.compare(e1.getSalary(), e2.getSalary());
}
};
}

public class Main {


public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "Alice", 50000));
employees.add(new Employee(2, "Bob", 70000));
employees.add(new Employee(3, "Charlie", 30000));

// Sorting using Comparable (natural ordering)


Collections.sort(employees);
System.out.println("Sorted by salary using Comparable:");
for (Employee e : employees) {
System.out.println(e);
}

// Sorting using Comparator (alternative way)


Collections.sort(employees, Employee.salaryComparator);
System.out.println("\nSorted by salary using Comparator:");
for (Employee e : employees) {
System.out.println(e);
}
}
}

Output:
Sorted by salary using Comparable:
Employee{id=3, name='Charlie', salary=30000.0}
Employee{id=1, name='Alice', salary=50000.0}
Employee{id=2, name='Bob', salary=70000.0}

Sorted by salary using Comparator:


Employee{id=3, name='Charlie', salary=30000.0}
Employee{id=1, name='Alice', salary=50000.0}
Employee{id=2, name='Bob', salary=70000.0}
b) 1.LinkedList in Java:
LinkedList is part of Java’s java.util package and implements both the List and Deque
interfaces. It is a doubly-linked list, where each element points to both the next and
previous elements.
LinkedList allows for constant-time insertions and deletions from both ends of the
list.
Use Cases: LinkedList is particularly useful for scenarios where elements are
frequently added or removed from the beginning or end.
2.indexOf and lastIndexOf Methods:
indexOf(Object o): Returns the index of the first occurrence of the specified element
in the list, or -1 if the element is not found.
lastIndexOf(Object o): Returns the index of the last occurrence of the specified
element, or -1 if the element is not found.

Code:
import java.util.LinkedList;

public class Main {


public static void main(String[] args) {
// Create a LinkedList and add some elements
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Apple");
list.add("Date");
list.add("Apple");

// Element to find occurrences of


String element = "Apple";

// Get first occurrence index


int firstOccurrence = list.indexOf(element);
System.out.println("First occurrence of '" + element + "': " + firstOccurrence);

// Get last occurrence index


int lastOccurrence = list.lastIndexOf(element);
System.out.println("Last occurrence of '" + element + "': " + lastOccurrence);

// Element that is not in the list


String nonExistentElement = "Mango";
int firstNonExistent = list.indexOf(nonExistentElement);
int lastNonExistent = list.lastIndexOf(nonExistentElement);

System.out.println("First occurrence of '" + nonExistentElement + "': " +


firstNonExistent);
System.out.println("Last occurrence of '" + nonExistentElement + "': " +
lastNonExistent);
}}

Output:
First occurrence of 'Apple': 0
Last occurrence of 'Apple': 5
First occurrence of 'Mango': -1
Last occurrence of 'Mango': -1
c) TreeSet in Java:
TreeSet is a part of Java's java.util package and implements the Set interface.
It is based on a TreeMap and uses a Red-Black tree structure internally to store
elements in sorted, ascending order.
Automatic Sorting: When elements are added to a TreeSet, they are automatically
sorted in natural order (alphabetical for strings).
Uniqueness: Like all Set implementations, TreeSet only stores unique elements. If
you try to add a duplicate, it will ignore the duplicate.
Null Handling: TreeSet does not allow null values, as it relies on natural ordering or
custom comparators for sorting.

Code:
import java.util.TreeSet;

public class Main {


public static void main(String[] args) {
// Create a TreeSet to store country names
TreeSet<String> countries = new TreeSet<>();

// Adding country names to the TreeSet


countries.add("India");
countries.add("United States");
countries.add("Australia");
countries.add("Canada");
countries.add("India"); // Duplicate entry

// Displaying countries in sorted order


System.out.println("Countries in sorted order:");
for (String country : countries) {
System.out.println(country);
}
}
}
Output:
Countries in sorted order:
Australia
Canada
India
United States

Conclusion: We have successfully completed the practical by executing all the programs.
Practical2:
Aim: a) Write a Java program to get a list of all file/directory names in the given directory.
b) Write a Java program to check if a file or directory specified by pathname exists or
not.
d) Write a Java program to read file content line by line.

Theory:
a) File Class in Java:
 The File class, part of the java.io package, represents both file and directory paths in
the filesystem. It is used to create, delete, and inspect the properties of files and
directories.
 Constructors: The File class has several constructors that take a file path as a String
or URI.
 Directory and File Operations: Through methods like isDirectory(), isFile(),
exists(), and getName(), you can determine if a path is a directory or file, check if it
exists, and get its name.
 Listing Files and Directories: The list() and listFiles() methods return lists of file
names or File objects in a directory.
Methods Used in This Program:
 listFiles(): Returns an array of File objects representing the files and directories in the
directory.
 isDirectory(): Checks if the File object is a directory.
 getName(): Retrieves the name of the file or directory.

Code:
import java.io.File;

public class Main {


public static void main(String[] args) {
// Specify the directory path
String directoryPath = " C:\Users\Marti\OneDrive\Desktop\Sajesh";

// Create a File object for the directory


File directory = new File(directoryPath);

// Check if the directory exists and is a directory


if (directory.exists() && directory.isDirectory()) {
// Get the list of all files and directories
File[] fileList = directory.listFiles();

System.out.println("Files and directories in " + directoryPath + ":");


for (File file : fileList) {
if (file.isDirectory()) {
System.out.println("[DIR] " + file.getName());
} else {
System.out.println("[FILE] " + file.getName());
}
}
} else {
System.out.println("The specified path does not exist or is not a directory.");
}
}
}
Output:
Files and directories in C:\Users\Marti\OneDrive\Desktop\Sajesh:
[DIR] .git
[DIR] .idea
[DIR] .vscode
[FILE] 7th_IT_Syllabus_20Scheme_25_05.doc
[FILE] aadhar card.pdf
[DIR] Accenture
[FILE] Angular JS and React JS.png
[DIR] Java - TTT -GL
[DIR] JAVA FSD

b) File Class in Java:


 The File class in Java’s java.io package represents a file or directory path in the
filesystem, abstracting the file system details so that the code is platform-independent.
 Constructing a File Object: A File object can be created with a pathname (either
absolute or relative) as a String:
 File Operations: The File class provides various methods to check a file's properties,
such as whether it exists, if it's a file or directory, and its name.
Methods Used:
 exists(): Checks if the specified file or directory exists. Returns true if it exists, false
otherwise.
 isFile(): Checks if the File object represents a regular file (not a directory).
 isDirectory(): Checks if the File object represents a directory.

Code:
import java.io.File;

public class Main {


public static void main(String[] args) {
// Specify the pathname for the file or directory
String pathname = "path/to/your/fileOrDirectory";

// Create a File object for the specified pathname


File file = new File(pathname);

// Check if the file or directory exists


if (file.exists()) {
if (file.isFile()) {
System.out.println("The specified path is a file and it exists.");
} else if (file.isDirectory()) {
System.out.println("The specified path is a directory and it exists.");
}
} else {
System.out.println("The specified file or directory does not exist.");
}
}
}
Output:
The specified path is a directory and it exists.

c) BufferedReader Class:
 BufferedReader is a part of Java’s java.io package, designed to read text from an input
stream (such as a file) efficiently.
 It buffers the input from a file, which means it reads large chunks of data at once, and
allows you to read data line by line, reducing the time needed for frequent I/O
operations.
 Constructor: You can create a BufferedReader by passing a FileReader (or another
Reader) object to its constructor.
 readLine() Method: The readLine() method reads a line of text until a newline
character (\n) or the end of the file is reached. It returns null when the end of the file is
reached.
FileReader Class:
 FileReader is also a part of the java.io package and is used to read the contents of a
file as a character stream.
 FileReader reads data character by character and works well for text files.
Exception Handling:
 FileNotFoundException: Thrown when the specified file is not found.
 IOException: General I/O exception handling, required to handle any issues during
file reading.

Closing the Stream:


 It’s important to close the BufferedReader after reading the file to free up system
resources. This can be done using close() or with a try-with-resources statement.

Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {


public static void main(String[] args) {
// Specify the file path
String filePath = "C:\\Users\\Marti\\OneDrive\\Desktop\\Sajesh\\New Text
Document.txt";

// Using try-with-resources to ensure the file is closed after reading


try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;

System.out.println("File contents:");
// Read each line until end of file
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}
}

Output:
File contents:
Peter always saw the world in black and white. There were two choices for every situation
and you had to choose one of them. It was therefore terribly uncomfortable for him to spend
time with Ashley. She saw the world in shades of gray with hundreds of choices to choose
from in every situation.
Stranded. Yes, she was now the first person ever to land on Venus, but that was of little
consequence. Her name would be read by millions in school as the first to land here, but that
celebrity would never actually be seen by her. She looked at the control panel and knew there
was nothing that would ever get it back into working order. She was the first and it was not
clear this would also be her last.

Process finished with exit code 0.

Conclusion: We have successfully completed the practical by executing all the programs.
Practical 3
A)Aim:
To create a functional interface Calculator with a method for performing arithmetic
operations, and implement addition, subtraction, multiplication, and division using lambda
expressions.

Theory:
A functional interface in Java is an interface with only one abstract method, which makes it
suitable for lambda expressions. Lambda expressions allow us to represent the instance of a
functional interface in a concise way, enabling inline implementations for single-method
interfaces. In this task, we define a Calculator functional interface with a method to perform
arithmetic operations.
Using lambda expressions, we implement the four basic operations: addition, subtraction,
multiplication, and division. This approach minimizes boilerplate code and enhances
readability, as lambdas offer a more streamlined syntax compared to anonymous inner
classes. Functional interfaces and lambda expressions promote functional programming
practices in Java, leading to modular, reusable, and efficient code.

Code:

// Functional Interface for Arithmetic Operations


@FunctionalInterface
interface Calculator {
double operation(double a, double b);
}

public class Main {


public static void main(String[] args) {
// Addition
Calculator add = (a, b) -> a + b;
System.out.println("Addition: " + add.operation(10, 5));

// Subtraction
Calculator subtract = (a, b) -> a - b;
System.out.println("Subtraction: " + subtract.operation(10, 5));

// Multiplication
Calculator multiply = (a, b) -> a * b;
System.out.println("Multiplication: " + multiply.operation(10, 5));

// Division
Calculator divide = (a, b) -> (b != 0) ? a / b : 0;
System.out.println("Division: " + divide.operation(10, 5));
}
}
Output:
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0

Conclusion: We have successfully completed the practical by executing all the programs.
Practical 3B
Aim:
To implement a lambda expression for calculating the factorial of a given number using a
functional interface.

Theory:
Factorial calculation involves finding the product of an integer and all positive integers below
it, represented as n!. Traditional methods for calculating factorials include iterative loops and
recursion, but functional interfaces and lambda expressions offer a modern alternative. Here,
we define a functional interface Factorial and use a lambda expression to compute the
factorial of a given number.
Using a lambda expression for factorial calculations allows concise implementation of the
factorial logic within the interface, promoting modularity and improving code readability.
This approach highlights the versatility of lambda expressions, enabling efficient expression
of iterative logic in a functional programming style.

Code:
// Functional Interface for Factorial Calculation
@FunctionalInterface
interface Factorial {
int calculate(int n);
}

public class Main {


public static void main(String[] args) {
// Lambda Expression for Factorial
Factorial factorial = (n) -> {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
};
System.out.println("Factorial of 5: " + factorial.calculate(5));
}
}

Output:
Factorial of 5: 120

Conclusion: We have successfully completed the practical by executing all the programs.
Practical 4

Aim:
To design a class that takes details of an employee, including ID, name, and salary, and
displays the name of employees whose salary is greater than 1500 and less than 50000 using
Java Stream API.

Theory:
In Java, the Stream API provides a powerful way to process collections of data in a
declarative manner. Introduced in Java 8, Stream API enables functional-style operations
such as filtering, mapping, and collecting data.
In this task, we use the Employee class to store each employee’s ID, name, and salary. We
then apply Stream API operations to filter employees based on their salary. The filter()
method is used to select employees whose salaries meet specific conditions, and map() is
used to transform each Employee object to just the name. Finally, the filtered names are
collected and displayed.
The Stream API is particularly useful for tasks involving bulk data processing, as it allows
concise and readable code without the need for complex loops or conditional statements. This
functionality enhances the code’s efficiency and makes it more maintainable.

Code:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Employee {
private int id;
private String name;
private double salary;

// Constructor
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}

// Getters
public int getId() {
return id;
}

public String getName() {


return name;
}

public double getSalary() {


return salary;
}
}

public class EmployeeFilter {


public static void main(String[] args) {
// List of employees
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "John Doe", 2000));
employees.add(new Employee(2, "Jane Smith", 1400));
employees.add(new Employee(3, "Alice Johnson", 30000));
employees.add(new Employee(4, "Bob Brown", 50000));
employees.add(new Employee(5, "Tom Clark", 800));

// Filter employees based on salary and display their names


List<String> employeeNames = employees.stream()
.filter(emp -> emp.getSalary() > 1500 && emp.getSalary() < 50000)
.map(Employee::getName)
.collect(Collectors.toList());

System.out.println("Employees with salary between 1500 and 50000:");


employeeNames.forEach(System.out::println);
}
}

Output:

Employees with salary between 1500 and 50000:


John Doe
Alice Johnson

Conclusion: : We have successfully completed the practical by executing all the programs.
Practical 5

Aim:
To develop a Java program using JDBC to connect to a MySQL database, retrieve records
from the Student table, and insert new records into it.

Theory:
Java Database Connectivity (JDBC) is an API that allows Java applications to interact with
relational databases. JDBC provides a common interface for connecting to various databases,
executing SQL queries, and retrieving data.
For this task, we will:
1. Use DriverManager.getConnection() to establish a connection with a MySQL
database.
2. Create an Employee class that represents each employee.
3. Fetch records from the Student table using a SELECT SQL query, displaying each
student’s details such as ID, Name, Age, and Grade.
4. Insert new student records using the INSERT SQL command.
5. Use PreparedStatement for parameterized queries, which provides security by
preventing SQL injection attacks and allows flexibility in query construction.
This example demonstrates core JDBC functionality, including establishing connections,
querying data, inserting records, and closing resources, following best practices for database
operations.

Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDatabase {


// Database URL, username, and password
private static final String URL = "jdbc:mysql://localhost:3306/your_database_name"; //
Replace with your database name
private static final String USER = "your_username"; // Replace with your MySQL
username
private static final String PASSWORD = "your_password"; // Replace with your MySQL
password

public static void main(String[] args) {


// Connect to the database
try (Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD)) {
System.out.println("Connected to the database successfully!");

// Fetch and display student records


fetchStudentRecords(connection);

// Insert a new student record


insertStudentRecord(connection, 101, "Alice Green", 20, "A");

} catch (SQLException e) {
e.printStackTrace();
}
}
// Method to fetch and display student records
private static void fetchStudentRecords(Connection connection) {
String query = "SELECT * FROM Student";

try (PreparedStatement statement = connection.prepareStatement(query);


ResultSet resultSet = statement.executeQuery()) {

System.out.println("Student Records:");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String grade = resultSet.getString("grade");

System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age + ", Grade: "
+ grade);
}

} catch (SQLException e) {
e.printStackTrace();
}
}

// Method to insert a new student record


private static void insertStudentRecord(Connection connection, int id, String name, int age,
String grade) {
String insertQuery = "INSERT INTO Student (id, name, age, grade) VALUES
(?, ?, ?, ?)";

try (PreparedStatement statement = connection.prepareStatement(insertQuery)) {


statement.setInt(1, id);
statement.setString(2, name);
statement.setInt(3, age);
statement.setString(4, grade);

int rowsInserted = statement.executeUpdate();


if (rowsInserted > 0) {
System.out.println("A new student record was inserted successfully!");
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

Output:
Connected to the database successfully!
Student Records:
ID: 100, Name: John Doe, Age: 21, Grade: B
A new student record was inserted successfully!
ID: 100, Name: John Doe, Age: 21, Grade: B
ID: 101, Name: Alice Green, Age: 20, Grade: A

Conclusion: : We have successfully completed the practical by executing all the programs.

You might also like