2 Java Record Program Single Side Print
2 Java Record Program Single Side Print
(Autonomous)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC with Section 2(f), Accredited by NAAC with A+, NBA Accredited
in the………………………………………………………………………………laboratory of
Date: Date:
Register No :
INDEX
Pg
[Link]. DATE EXPERIMENT NAME MARK SIGN
No.
ARRAYI IMPLEMENTATION
INHERITANCE
Nehru Institute of Technology
(Autonomous)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC with Section 2(f), Accredited by NAAC with A+, NBA Accredited
FILE OPERATIONS
Average
Signature
U23CS312-Java Lab
Exp No:
AIM:
To study and understand how to write and execute Java programs using Eclipse.
Step 1: Open Eclipse and click File > New > Java Project
1
U23CS312-Java Lab
Step 2: Provide the Project Name and click on the Finish button.
2
U23CS312-Java Lab
Step 3: In the Package Explorer (left-hand side of the window) select the project which
you have created.
Step 4: Right-click on the src folder, select New > Class from the submenu. Provide
the Class name and click on Finish button.
3
U23CS312-Java Lab
Step 6: Now, press Ctrl+F11 or click on the Run menu and select Run or click on Run
button.
Step 7: Output
RESULT:
Thus, writing and executing Java program in Eclipse has been studied successfully.
4
U23CS312-Java Lab
Exp No:
AIM:
To write a Java program to implement exception handling
ALGORITHM:
STEP 1: Start
STEP 2: Create a BufferedReader object to read input from the user.
STEP 3: Prompt the user to enter a number.
STEP 4: Read the input as a string using [Link]().
STEP 5: Convert the string input to an integer using [Link](input).
STEP 6: Check if the number is divisible by 2 (i.e., number % 2 == 0).
STEP 7: If the number is divisible by 2, print that the number is even.
STEP 8: Otherwise, print that the number is odd.
STEP 9: Handle possible IOException by printing an error message if input reading fails.
STEP 10: Handle NumberFormatException by printing an error message if the input is not
a valid integer.
STEP 11: Stop
PROGRAM:
import [Link];
import [Link];
import [Link];
public class OddEvenCheckerIOStream {
public static void main(String[] args) {
// Create BufferedReader object to read input from InputStream
BufferedReader reader = new BufferedReader(new InputStreamReader([Link]));
try {
// Prompt the user to enter a number
5
U23CS312-Java Lab
RESULT:
6
U23CS312-Java Lab
Exp No:
Java Program to implement for loop
Date:
AIM:
To write, a Java program to implement for loop
ALGORITHM:
STEP 1: Start
STEP 2: Create a Scanner object to read user input.
STEP 3: Prompt the user to enter a number (n).
STEP 4: Read the input number using [Link]().
STEP 5: Print the message "Numbers from 1 to n:".
STEP 6: Initialize a for loop with the loop variable i starting at 1, and continue looping
until i is less than or equal to n.
STEP 7: Inside the loop, print the current value of i.
STEP 8: Increment i by 1 after each iteration.
STEP 9: After the loop finishes, close the Scanner object to release resources.
STEP 10: Stop
PROGRAM:
import [Link];
public class ForLoopExample {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int n = [Link]();
7
U23CS312-Java Lab
RESULT:
Thus, for loop in Java was implemented successfully
8
U23CS312-Java Lab
Exp No:
Java Program to implement while loop
Date:
AIM:
To write a Java program to implement while loop
ALGORITHM:
STEP 1: Start
STEP 2: Create a Scanner object to read user input.
STEP 3: Prompt the user to enter a number (n).
STEP 4: Read the input number using [Link]().
STEP 5: Initialize a counter variable i to 1.
STEP 6: Print the message "Numbers from 1 to n:".
STEP 7: Start a while loop that continues as long as i is less than or equal to n.
STEP 8: Inside the loop, print the current value of i.
STEP 9: Increment i by 1 after each iteration.
STEP 10: When the loop ends, close the Scanner object to release resources.
STEP 11: Stop
PROGRAM:
import [Link];
public class WhileLoopExample {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int n = [Link]();
// Initialize the counter
int i = 1;
9
U23CS312-Java Lab
RESULT:
Thus, while loop in Java was implemented successfully
10
U23CS312-Java Lab
Exp No:
Java Program to implement simple array
Date:
AIM:
To write a Java program to implement simple array
ALGORITHM:
STEP 1: Start
STEP 2: Create a Scanner to read input.
STEP 3: Ask the user for the number of elements in the array.
STEP 4: Read the size of the array from the user.
STEP 5: Declare an array to hold the numbers.
STEP 6: Use a loop to get each element from the user and store it in the array.
STEP 7: Use a loop to print all the elements in the array.
STEP 8: Close the Scanner.
STEP 9: Stop
PROGRAM:
import [Link];
public class SimpleArrayExample {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter the size of the array
[Link]("Enter the number of elements in the array: ");
int size = [Link]();
// Declare an array to store the user input
int[] numbers = new int[size];
// Loop to get input from the user
11
U23CS312-Java Lab
RESULT:
Thus, simple array in Java was implemented successfully
12
U23CS312-Java Lab
Exp No:
Java Program to implement 2-Dimensional array
Date:
AIM:
To write a Java program to implement 2-Dimensional array
ALGORITHM:
STEP 1: Start
STEP 2: Create a Scanner to read user input.
STEP 3: Ask the user for the number of rows and columns of the first matrix.
STEP 4: Ask the user for the number of rows and columns of the second matrix.
STEP 5: Check if matrix multiplication is possible (i.e., the number of columns of the first
matrix must equal the number of rows of the second matrix).
PROGRAM:
import [Link];
public class MatrixMultiplication {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner([Link]);
// Get the dimensions of the first matrix
13
U23CS312-Java Lab
14
U23CS312-Java Lab
matrix2[i][j] = [Link]();
}
}
// Perform matrix multiplication
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) { // cols1 is equal to rows2
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Display the result of the matrix multiplication
[Link]("The resulting matrix after multiplication is:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
[Link](result[i][j] + "\t");
}
[Link]();
}
// Close the scanner
[Link]();
}
}
RESULT:
Thus, 2-Dimentional array in Java was implemented successfully
15
U23CS312-Java Lab
Exp No:
Simple program to implement classes and objects
Date:
AIM:
To write a simple Java program to implement classes and objects
ALGORITHM:
STEP 1: Start
STEP 2: Define a Student class with instance variables name, age, and grade.
STEP 3: Define a method inputDetails to:
• Get student details (name, age, and grade) from the user.
STEP 4: Define a method displayDetails to:
STEP 6: Stop
PROGRAM:
import [Link];
class Student {
String name;
int age;
double grade;
16
U23CS312-Java Lab
void inputDetails() {
name = [Link]();
age = [Link]();
grade = [Link]();
void displayDetails() {
[Link]("\nStudent Details:");
17
U23CS312-Java Lab
[Link]();
[Link]();
RESULT:
Thus, classes and objects in Java was implemented successfully
18
U23CS312-Java Lab
Exp No:
AIM:
To write a Java program to implement single inheritance
ALGORITHM:
STEP 1: Start
STEP 2: Define a Person class with the following:
• Instance variables: name, age.
• Method inputDetails:
o Get name and age from the user.
• Method displayDetails:
o Display name and age.
STEP 3: Define a Student class that inherits from Person:
• Instance variable: major.
• Method inputStudentDetails:
o Call inputDetails from Person to get name and age.
o Get major from the user.
• Method displayStudentDetails:
o Call displayDetails from Person to display name and age.
o Display major.
STEP 4: In the main method:
• Create a Student object (student).
• Call inputStudentDetails to input student details.
• Call displayStudentDetails to display the student's details.
STEP 5: Stop
19
U23CS312-Java Lab
PROGRAM:
import [Link];
// Base class
class Person {
String name;
int age;
// Method to input person details
void inputDetails() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter name: ");
name = [Link]();
[Link]("Enter age: ");
age = [Link]();
}
// Method to display person details
void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Derived class
class Student extends Person {
String major;
// Method to input student details
void inputStudentDetails() {
Scanner scanner = new Scanner([Link]);
// Call the inputDetails method from the Person class
inputDetails();
[Link]("Enter major: ");
major = [Link]();
}
// Method to display student details
void displayStudentDetails() {
20
U23CS312-Java Lab
RESULT:
Thus, Java program to implement single inheritance was completed successfully
21
U23CS312-Java Lab
Exp No:
Java program that implements Multilevel inheritance
Date:
AIM:
ALGORITHM:
STEP 1: Start
STEP 2: Create a Person class with:
• Variable: major.
• Create a Graduate object (graduate). And Input and display graduate details.
STEP 6: Stop
22
U23CS312-Java Lab
PROGRAM:
import [Link];
// Base class
class Person {
String name;
int age;
// Method to input person details
void inputDetails() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter name: ");
name = [Link]();
[Link]("Enter age: ");
age = [Link]();
}
// Method to display person details
void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Derived class
class Student extends Person {
String major;
// Method to input student details
void inputStudentDetails() {
// Call the inputDetails method from the Person class
inputDetails();
Scanner scanner = new Scanner([Link]);
[Link]("Enter major: ");
major = [Link]();
}
// Method to display student details
void displayStudentDetails() {
23
U23CS312-Java Lab
24
U23CS312-Java Lab
[Link]();
}
}
RESULT:
Thus, Multi-level Inheritance in Java was implemented successfully
25
U23CS312-Java Lab
Exp No:
Java program that implements Hierarchical inheritance
Date:
AIM:
To write a Java program to implement Hierarchical Inheritance
ALGORITHM:
STEP 1: Start
STEP 2: Create a Person class with:
• Variable: major.
• Variable: subject.
• Create a separate object for Student and Teacher input their details.
STEP 6: Stop
26
U23CS312-Java Lab
PROGRAM:
import [Link];
// Base class
class Person {
String name;
int age;
// Method to input person details
void inputDetails() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter name: ");
name = [Link]();
[Link]("Enter age: ");
age = [Link]();
}
// Method to display person details
void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Derived class for students
class Student extends Person {
String major;
// Method to input student details
void inputStudentDetails() {
inputDetails(); // Call the inputDetails method from Person
Scanner scanner = new Scanner([Link]);
[Link]("Enter major: ");
major = [Link]();
}
// Method to display student details
void displayStudentDetails() {
displayDetails(); // Call the displayDetails method from Person
27
U23CS312-Java Lab
28
U23CS312-Java Lab
[Link]();
// Display teacher details
[Link]("\nTeacher Details:");
[Link]();
}
}
RESULT:
Thus, Hierarchical Inheritance in Java was implemented successfully
29
U23CS312-Java Lab
Exp No:
Java program that implements Multiple inheritance using
Date: interface
AIM:
To write a Java program to implement Multiple Inheritance using interface
ALGORITHM:
STEP 1: Start
STEP 2: Define a Student interface with:
• Method inputStudentDetails: To get student details (name, age, major).
• Method displayStudentDetails: To display student details.
STEP 3: Define an Employee interface with:
• Method inputEmployeeDetails: To get employee details (company, stipend).
• Method displayEmployeeDetails: To display employee details.
STEP 4: Create an Intern class that implements both Student and Employee interfaces with:
• Variables: name, age, major, company, stipend.
• Method inputStudentDetails: Get student details.
• Method displayStudentDetails: Display student details.
• Method inputEmployeeDetails: Get employee details (company, stipend).
• Method displayEmployeeDetails: Display employee details.
STEP 5: In the main method:
• Create an Intern object.
• Call inputStudentDetails to input student details.
• Call inputEmployeeDetails to input employee details.
• Call displayStudentDetails to display student details.
• Call displayEmployeeDetails to display employee details.
STEP 6: Stop
30
U23CS312-Java Lab
PROGRAM:
import [Link];
// Interface for Student
interface Student {
void inputStudentDetails();
void displayStudentDetails();
}
// Interface for Employee
interface Employee {
void inputEmployeeDetails();
void displayEmployeeDetails();
}
// Class that implements both interfaces
class Intern implements Student, Employee {
String name;
int age;
String major;
String company;
double stipend;
// Method to input student details
@Override
public void inputStudentDetails() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter student name: ");
name = [Link]();
[Link]("Enter student age: ");
age = [Link]();
[Link](); // Consume newline
[Link]("Enter major: ");
major = [Link]();
}
// Method to display student details
@Override
31
U23CS312-Java Lab
32
U23CS312-Java Lab
[Link]();
}
}
RESULT:
Thus, Multiple Inheritance using interface was successfully implemented
33
U23CS312-Java Lab
Exp No:
Java program that implements method overloading
Date:
AIM:
To write a Java program to implement method overloading
ALGORITHM:
STEP 1: Start
STEP 2: Define a calculateArea method for the rectangle:
• If choice is 1 (rectangle):
• If choice is 2 (circle):
o Input radius.
STEP 5: Stop
34
U23CS312-Java Lab
PROGRAM:
import [Link];
public class MethodOverloadingExample {
public static void calculateArea(int length, int breadth) {
int area = length * breadth;
[Link]("Area of rectangle: " + area);
}
public static void calculateArea(double radius) {
double area = [Link] * radius * radius;
[Link]("Area of circle: " + area);
}
public static void main(String[] args) {
Scanner
scanner = new Scanner([Link]);
[Link]("Enter 1 for rectangle or 2 for circle:");
int choice = [Link]();
if (choice == 1) {
[Link]("Enter length: ");
int length = [Link]();
[Link]("Enter breadth: ");
int breadth = [Link]();
calculateArea(length, breadth);
} else if (choice == 2) {
[Link]("Enter radius: ");
double radius = [Link]();
calculateArea(radius);
} else {
[Link]("Invalid choice!");
}
[Link]();
RESULT:
Thus, Method overloading in Java was implemented successfully
35
U23CS312-Java Lab
Exp No:
Java program to handle exceptions
Date:
AIM:
To write a Java program to handle exceptions
ALGORITHM:
STEP 1: Start
STEP 2: Create a Scanner object to take user input.
STEP 3: Use a try block to:
PROGRAM:
import [Link];
import [Link];
public class ExceptionHandlingExample {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
try {
// Taking user input for two integers
36
U23CS312-Java Lab
} catch (InputMismatchException e) {
// Handling case where the user enters a non-integer value
[Link]("Error: Invalid input. Please enter an integer.");
} catch (ArithmeticException e) {
// Handling division by zero
[Link]("Error: Division by zero is not allowed.");
} finally {
// This block will always execute
[Link]("End of exception handling demo.");
[Link]();
}
}
}
RESULT:
Thus, Program to handle exceptions was implemented successfully
37
U23CS312-Java Lab
Exp No:
Java program to implement multithreading
Date:
AIM:
To write a Java program to implement multithreading
ALGORITHM:
STEP 1: Start
STEP 2: Create a Scanner object to take user input.
STEP 3: Ask the user for the number of threads (numThreads).
STEP 4: Create an array threads of size numThreads.
STEP 5: For each thread (from 0 to numThreads - 1):
STEP 8: After all threads have finished, print "All threads have finished."
STEP 9: Close the scanner.
STEP 10: Stop
PROGRAM:
import [Link];
public class MultithreadingExample {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of threads: ");
int numThreads = [Link]();
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
int threadNumber = i; // Capturing the correct index in a final variable
38
U23CS312-Java Lab
RESULT:
Thus, Multi-threading in Java was implemented successfully
39
U23CS312-Java Lab
Exp No:
Java program to perform basic file reading from and
Date: writing to text files
AIM:
To write a Java program to perform basic file reading from and write to text file
ALGORITHM:
STEP 1: Start
STEP 2: Define the file name ([Link]) and content to write
STEP 3: Write content to the file:
• If an error occurs during writing, catch IOException and print an error message.
• Use a FileReader to open the file and read each character one by one.
• If an error occurs during reading, catch IOException and print an error message.
STEP 5: Stop
PROGRAM:
import [Link];
import [Link];
import [Link];
40
U23CS312-Java Lab
RESULT:
Thus, Program to perform basic file reading and writing was implemented successfully
41
U23CS312-Java Lab
Exp No:
Java program to perform reading from and writing to
binary files
Date:
AIM:
To write a Java program to perform reading and writing binary files
ALGORITHM:
STEP 1: Start
STEP 2: Define the file name ([Link]) and data to write
STEP 3: Write data to a binary file:
• Use FileOutputStream to open the file and write the byte array to it.
• If an error occurs during writing, catch IOException and print an error message.
STEP 4: Read data from the binary file:
• Use FileInputStream to open the file and read one byte at a time.
PROGRAM:
import [Link];
import [Link];
import [Link];
42
U23CS312-Java Lab
RESULT:
Thus, Java program to read and write Binary files was implemented successfully
43
U23CS312-Java Lab
Exp No:
Java program to demonstrate basic networking using
Date: TCP sockets
AIM:
To write a Java program to demonstrate basic networking using TCP sockets
ALGORITHM:
[Link] (Server Side):
STEP 1: Start
STEP 2: Initialize the server to listen on a specified port (e.g., 6789).
STEP 3: Wait for a client to connect using [Link]().
STEP 4: Once the client connects, create input and output streams to communicate with the
client:
44
U23CS312-Java Lab
PROGRAM:
TCPServer,java:
import [Link].*;
import [Link].*;
[Link]:
import [Link].*;
import [Link].*;
45
U23CS312-Java Lab
RESULT:
Thus, Program to demonstrate basic networking using TCP sockets was completed
successfully
46
U23CS312-Java Lab
Exp No:
Java program to demonstrate basic
networking using UDP sockets
Date:
AIM:
To write a Java program to demonstrate basic networking using UDP sockets
ALGORITHM:
[Link] (Server Side):
STEP 1: Start
STEP 2: Create a DatagramSocket to listen for incoming client messages on a specific port
(e.g., 9876).
STEP 3: Set up a buffer to receive incoming data from the client.
STEP 4: Wait for a packet (message) from the client using [Link]().
STEP 5: Read the data from the packet and convert it into a string.
STEP 6: Prepare a response message ("Hello from UDP Server!") and convert it into bytes.
STEP 7: Send the response message back to the client using [Link]().
STEP 8: Close the socket connection.
STEP 9: Stop
47
U23CS312-Java Lab
PROGRAM:
[Link]:
import [Link];
import [Link];
import [Link];
// Prepare response
String responseMessage = "Hello from UDP Server!";
byte[] sendBuffer = [Link]();
48
U23CS312-Java Lab
[Link]:
import [Link];
import [Link];
import [Link];
} catch (Exception e) {
[Link]("Client error: " + [Link]());
[Link]();
}
}
}
RESULT:
Thus, Program to demonstrate basic networking using UDP sockets was completed
successfully
49