0% found this document useful (0 votes)
5 views

Java lab Manual 4th sem

The document outlines a series of Java programming experiments, each with a specific aim and code implementation. It covers various topics including reading employee details, using constructors, managing object creation, implementing interfaces, access modifiers, exception handling, string methods, thread management, deadlock scenarios, and file merging. Each experiment includes code snippets and sample outputs demonstrating the expected functionality.

Uploaded by

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

Java lab Manual 4th sem

The document outlines a series of Java programming experiments, each with a specific aim and code implementation. It covers various topics including reading employee details, using constructors, managing object creation, implementing interfaces, access modifiers, exception handling, string methods, thread management, deadlock scenarios, and file merging. Each experiment includes code snippets and sample outputs demonstrating the expected functionality.

Uploaded by

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

Experiment No.

:- 01
Aim:- Write a program in Java to read from console employee details of 5 employees with following
details: Name of employee, Department, Age, Salary. Print the details of every employee.

Code:-
import java.util.Scanner;
class Experiment1{
public static void main(String args[]){
String name;
String department;
int age;
int salary;
Scanner s = new Scanner(System.in);
for(int i=0;i<5;i++){
System.out.println("Enter Name of employ : ");
name = s.next();
System.out.println("Enter Name of department : ");
department = s.next();
System.out.println("Enter age : ");
age = s.nextInt();
System.out.println("Enter salary : ");
salary = s.nextInt();
System.out.println("Name of "+(i+1)+" empolye is "+name);
System.out.println("Name of department of "+(i+1)+" empolye is "+department );
System.out.println("Age of "+(i+1)+" empoly is "+age);
System.out.println("Salary of "+(i+1)+" employ is "+salary);
}
}
}
Output:-
Enter Name of employ :
venktesh
Enter Name of department :
cse
Enter age :
19
Enter salary :
150000
Name of 1 empolye is venktesh
Name of department of 1 empolye is cse
Age of 1 empoly is 19
Salary of 1 employ is 150000
Enter Name of employ :
gaurav
Enter Name of department :
cse
Enter age :
19
Enter salary :
120000
Name of 2 empolye is gaurav
Name of department of 2 empolye is cse
Age of 2 empoly is 19
Salary of 2 employ is 120000
Enter Name of employ :
navneet
Enter Name of department :
cse
Enter age :
17
Enter salary :
100000
Name of 3 empolye is navneet
Name of department of 3 empolye is cse
Age of 3 empoly is 17
Salary of 3 employ is 100000
Enter Name of employ :
shubam
Enter Name of department :
mechanical
Enter age :
22
Enter salary :
50000
Name of 4 empolye is shubam
Name of department of 4 empolye is mechanical
Age of 4 empoly is 22
Salary of 4 employ is 50000
Enter Name of employ :
vikaas
Enter Name of department :
IT
Enter age :
20
Enter salary :
vikaas
Enter Name of department :
IT
Enter age :
20
Enter salary :
IT
Enter age :
20
Enter salary :
20
Enter salary :
60000
Name of 5 empolye is vikaas
Name of department of 5 empolye is IT
Age of 5 empoly is 20
Salary of 5 employ is 60000
Experiment No. :- 02
Aim:-Write a program to show the use 'this' keyword to call the default and parameterized constructors.
Code:-
public class demo{
int x;
int y;
public demo(){
this(0,0);
}
public demo(int x , int y){
this.x=x;
this.y=y;
}
public void printval(){
System.out.println("x="+x+"y="+y);
}
public static void main(String args[]){
demo obj_a=new demo();
obj_a.printval();
demo obj_b=new demo(5,15);
obj_b.printval();
}
}

Output:-
x=0y=0
x=5y=15
Experiment No. :- 03
Aim:- Write a program in Java to display the count of number of objects created and finalized. Provide
unique IDs to every object while creation and display the same ID during finalization.

Code:-
class Experiment3{
public static void main(String args[]){
Student s1 = new Student(555,"Venktesh");
Student s2 = new Student(666,"Shakish");
Student s3 = new Student(777,"Ankit");
s1.display();
s2.display();
s3.display();
}
}
class Student{
int rollNo;
String name;
static int totalStudent = 0;
public Student(int rollNo,String name){
this.rollNo = rollNo;
this.name = name;
totalStudent++;
System.out.println("One student is added");
System.out.println("Total of student is "+totalStudent);
}
public void display(){
System.out.println("Name of student is "+name);
System.out.println("Roll no of student "+rollNo);

}
}
Output:-
One student is added
Total of student is 1
One student is added
Total of student is 2
One student is added
Total of student is 3
Name of student is Venktesh
Roll no of student 555
Name of student is Shakish
Roll no of student 666
Name of student is Ankit
Roll no of student 777
Experiment No. :- 04
Aim:- Create a Shape Interface which has a member method area(). Derive two subclasses Circle and
Triangle from it. Using reference of Shape class fill the required members in Circle and Triangle also display
the area of Circle and Triangle. Take input from user while filling data members.

Code:-
import java.util.Scanner;
class Experiment4{
public static void main(String[] args) {
Circle callc = new Circle();
callc.area();
Triangle callt = new Triangle();
callt.area();
}
}
interface Shape{
void area();
}
class Circle implements Shape{
Scanner s = new Scanner(System.in);
public void area(){
double a;
double pi = 3.14;
System.out.println("Enter the radius of circle");
double r = s.nextDouble();
a = r*r*pi;
System.out.println("The area of circle is "+ a);
}
}
class Triangle implements Shape{
Scanner s = new Scanner(System.in);
public void area(){
System.out.println("Enter base of triangle ");
int base = s.nextInt();
System.out.println("Enter height of triangle");
int height = s.nextInt();
double area = 0.5*base*height;
System.out.println("Area of triangle is "+area);
}
}

Output:-
Enter the radius of circle
5
The area of circle is 78.5
Enter base of triangle
12
Enter height of triangle
15
Area of triangle is 90.0
Experiment No. :- 05
Aim:- Write a program to demonstrate the effect of access modifiers (default, protected, public and
private) on members with and without inheritance within a package and outside a package.

Code:-
import java.util.Scanner;
class A{
private int a = 1;
int b = 2;
protected int c = 3;
public int d = 4;
void display(){
System.out.println("Access modifier inside base class");
System.out.println(a+" Private var ");
System.out.println(b+" default var ");
System.out.println(c+" Protected var ");
System.out.println(d+" Public var ");
}
}
class B extends A{
void display(){
System.out.println("Access modifier inside derived class");
System.out.println(b+" default var ");
System.out.println(c+" Protected var ");
System.out.println(d+" Public var ");
}
}
class C{
void display(){
System.out.println("Access modifier inside a different class c");
}
}
public class Exp5{
public static void main(String[] args) {
A a = new A();
a.display();
B b = new B();
b.display();
C c = new C();
c.display();
}
}

Output:-
Access modifier inside base class
1 Private var
2 default var
3 Protected var
4 Public var
Access modifier inside derived class
2 default var
3 Protected var
4 Public var
Access modifier inside a different class c
Experiment No. :- 06
Aim:- Write a program to show inbuilt and user defined: checked and unchecked exceptions.
Code:-
import java.util.*;
class Experiment6 {
public static void main(String[] args) {
System.out.println("Enter age");
Scanner s= new Scanner(System.in);
int a= s.nextInt();
try{
Lmv(a);
}
catch(Exception e){
System.out.println(e);
}
int p=5,q=0;
try{
int r=p/q;
System.out.println(r);
} catch(Exception e){
System.out.println("Inbuilt Exception (arihtmetic)");
}

}
public static void Lmv(int a) throws LmvException{
if(a<=18){
throw new LmvException("Not eligible");
}
else{
System.out.println("eligible");
}
}
}
class LmvException extends Exception{
LmvException( String s){
System.out.println(s);
}
}

Output:-
Enter age
15
Not eligible
LmvException
Inbuilt Exception (arihtmetic)
Experiment No. :- 07
Aim:- Write a program to show the use of various member methods of String class.
Code:-
public class Experiment7 {
public static void main(String[] args) {
String s = "Hey I am Venktesh";

// Length method
int length = s.length();
System.out.println("Length of the string: " + length);

// CharAt method
char Idx = s.charAt(5);
System.out.println("Character at index 5: " + Idx);

// Substring method
String substring = s.substring(9);
System.out.println("Substring from index 9: " + substring);

// Substring method with start and end index


String substring2 = s.substring(7, 12);
System.out.println("Substring from index 7 to 12: " + substring2);

// Concatenation with concat method


String concate = s.concat(" I am a App developer.");
System.out.println("Concatenated string: " + concate);

// Replace method
String replacedStr = s.replace('I', 'U');
System.out.println("String after replacing 'I' with 'U': " + replacedStr);

// ToUpperCase method
String upperCase = s.toUpperCase();
System.out.println("Uppercase string: " + upperCase);

// ToLowerCase method
String lowerCase = s.toLowerCase();
System.out.println("Lowercase string: " + lowerCase);

// Trim method
String v = " I learn java ";
String trimmedStr = v.trim();
System.out.println("Trimmed string: " + trimmedStr);

// Contains method
boolean contains = s.contains("Venktesh");
System.out.println("Does the string contain 'Venktesh' " + contains);

// Equals method
boolean equals = s.equals("Hey I am Venktesh");
System.out.println("Is the string equal to 'Hey I am Venktesh'" + equals);

// IndexOf method
int indexOfv = s.indexOf('V');
System.out.println("Index of 'V': " + indexOfv);

// LastIndexOf method
int lastIndexOff = s.lastIndexOf('t');
System.out.println("Last index of 't': " + lastIndexOff);

// StartsWith method
boolean startsWith = s.startsWith("Hey");
System.out.println("Does the string start with 'Hey'? " + startsWith);

// EndsWith method
boolean endsWith = s.endsWith("Venktesh");
System.out.println("Does the string end with 'Venktesh' " + endsWith);
}
}

Output:-
Length of the string: 17
Character at index 5:
Substring from index 9: Venktesh
Substring from index 7 to 12: m Ven
Concatenated string: Hey I am Venktesh I am a App developer.
String after replacing 'I' with 'U': Hey U am Venktesh
Uppercase string: HEY I AM VENKTESH
Lowercase string: hey i am venktesh
Trimmed string: I learn java
Does the string contain 'Venktesh' true
Is the string equal to 'Hey I am Venktesh'true
Index of 'V': 9
Last index of 't': 13
Does the string start with 'Hey'? true
Does the string end with 'Venktesh' true
Experiment No. :- 08
Aim:- Create two threads T1 and T2. The thread T1 should print numbers from 1 to 10 and thread T2
prints characters from A to J. Ensure that T2 starts first and T1 should only start when T2 finishes. (Note:
use join())

Code:-
public class Experiment8 {
public static void main(String[] args) throws InterruptedException {
// Thread T2 that prints characters from A to J
Thread t2 = new Thread(() -> {
for (char c = 'A'; c <= 'J'; c++) {
System.out.print(c + " ");
}
System.out.println();
});
// Thread T1 that prints numbers from 1 to 10
Thread t1 = new Thread(() -> {
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
System.out.println();
});
// Start thread T2 first
t2.start();
// Wait for T2 to finish before starting T1
t2.join();
// Start thread T1
t1.start();
}
}

Output:-
ABCDEFGHIJ
1 2 3 4 5 6 7 8 9 10
Experiment No. :- 09
Aim:- Demonstrate using a Java program, how DEADLOCK occurs between threads and also give solution
program.

Code:-
public class Experiment9 {
static class Resource {
private final String name;
public Resource(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
final Resource resource1 = new Resource("Venktesh");
final Resource resource2 = new Resource("Shakish");
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resource1) {
System.out.println("Thread 1: Locked " + resource1.getName());

synchronized (resource2) {
System.out.println("Thread 1: Locked " + resource2.getName());
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resource2) {
System.out.println("Thread 2: Locked " + resource2.getName());

synchronized (resource1) {
System.out.println("Thread 2: Locked " + resource1.getName());
}
}
}
});

thread1.start();
thread2.start();
}
}

Output:-
Thread 2: Locked Shakish
Thread 1: Locked Venktesh

Code:-
public class Experiment9{
static class Resource {
private final String name;
public Resource(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
final Resource resource1 = new Resource("Venktesh");
final Resource resource2 = new Resource("Shakish");
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resource1) {
System.out.println("Thread 1: Locked " + resource1.getName());
synchronized (resource2) {
System.out.println("Thread 1: Locked " + resource2.getName());
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resource1) { // Ensure the same lock order
System.out.println("Thread 2: Locked " + resource1.getName());
synchronized (resource2) {
System.out.println("Thread 2: Locked " + resource2.getName());
}
}
}
});
thread1.start();
thread2.start();
}
}

Output:-
Thread 1: Locked Venktesh
Thread 1: Locked Shakish
Thread 2: Locked Venktesh
Thread 2: Locked Shakish
Experiment No. :- 10
Aim:- Write a program to merge the contents of text files T1.txt and T2.txt into T3.txt. The contents of
T1.txt should appear first and then T2.txt in the destination file T3.txt.

Code:-
import java.io.*;

public class exp10 {


public static void main(String[] args) {
try {
// Open the files for reading and writing
FileReader fr1 = new FileReader("C:\\Users\\Gyan\\OneDrive\\Desktop\\f1.txt");
FileReader fr2 = new FileReader("C:\\Users\\Gyan\\OneDrive\\Desktop\\f2.txt");
FileWriter fw = new FileWriter("C:\\Users\\Gyan\\OneDrive\\Desktop\\f3.txt");

// Create buffered readers for the input files


BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);

// Read and write the contents of the files


String line;
while ((line = br1.readLine()) != null) {
fw.write(line + "\n");
}
while ((line = br2.readLine()) != null) {
fw.write(line + "\n");
}

// Close the files


fr1.close();
fr2.close();
fw.close();

System.out.println("Files merged successfully!");


} catch (IOException e) {
System.out.println("An error occurred while merging the files.");
e.printStackTrace();
}
}
}

Output:-
Files merged successfully

You might also like