0% found this document useful (0 votes)
9 views14 pages

Java Lab Programs

The document contains a table of contents followed by ten programming examples in Java, covering topics such as sorting arrays, find and replace operations, calculator implementation, command line arguments, polymorphism, multiple inheritance, multi-threading, applet programming, file reading, and a student database connection. Each section includes code snippets and their respective outputs. The examples illustrate fundamental programming concepts and practical applications in Java.
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)
9 views14 pages

Java Lab Programs

The document contains a table of contents followed by ten programming examples in Java, covering topics such as sorting arrays, find and replace operations, calculator implementation, command line arguments, polymorphism, multiple inheritance, multi-threading, applet programming, file reading, and a student database connection. Each section includes code snippets and their respective outputs. The examples illustrate fundamental programming concepts and practical applications in Java.
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

TABLE OF CONTENTS

SN DATE TITLE PAGE NO. SIGNATURE

1 SORT USING ARRAYS

2 FIND AND REPLACE OPERATIONS IN

THE GIVEN TEXT

3 IMPLEMENT A CALCULATOR

4 COMMAND LINE ARGUMENTS

5 POLYMORPHISM AND INHERITANCE

6 MULTIPLE INHERITANCE

7 MULTI THREADING

8 APPLET PROGRAM

9 READ DATA FROM FILE

10 STUDENT DATABASE
1. SORT USING ARRAYS
PROGRAM:
import [Link].*;
public class sort1
{
public static void main(String[]args)
{
int a[] = {7,8,1,4,6};
int temp=0;
for(int i=0;i<[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
[Link]();
[Link]("Elements of array sorted in ascending order:");
for(int i=0;i<[Link];i++)
{
[Link](a[i]+"");
}
}
}

OUTPUT:
Elements of array sorted in ascending order : 1 4 6 7 8
2. FIND AND REPLACE OPERATIONS IN THE GIVEN TEXT

PROGRAM:
import [Link];
class FindReplace
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the text:");
String text = [Link]();
[Link]("Enter word to find: ");
String find = [Link]();
[Link]("Enter replacement word: ");
String replace = [Link]();
String result = [Link](find, replace);
[Link]("Modified text:");
[Link](result);
}
}

OUTPUT:
Enter the text : Java is easy
Enter word to find : easy
Enter replacement word : powerful
Modified text : Java is powerful
3. IMPLEMENT A CALCULATOR

PROGRAM:

class Calculator
{
int a, b;
Calculator(int x, int y)
{
a = x;
b = y;
}
void add()
{
[Link]("Addition: " + (a + b));
}
void sub() {
[Link]("Subtraction: " + (a - b));
}
void mul()
{
[Link]("Multiplication: " + (a * b));
}
void div()
{
[Link]("Division: " + (a / b));
}
public static void main(String[] args)
{
Calculator c = new Calculator(10, 5);
[Link]();
[Link]();
[Link]();
[Link]();
}
}

OUTPUT:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
4. COMMAND LINE ARGUMENTS
PROGRAM:

class StudentGrade
{
public static void main(String[] args)
{
int m1 = [Link](args[0]);
int m2 = [Link](args[1]);
int m3 = [Link](args[2]);
int total = m1 + m2 + m3;
double percentage = total / 3.0;
[Link]("Percentage: " + percentage);
if (percentage >= 75)
[Link]("Grade: A");
else if (percentage >= 60)
[Link]("Grade: B");
else if (percentage >= 50)
[Link]("Grade: C");
else
[Link]("Grade: Fail");
}
}

OUTPUT :

javac [Link]
java StudentGrade 70 80 90
Percentage: 80.0
Grade: A
5. POLYMORPHISM AND INHERITANCE

PROGRAM:

class Shape
{
void draw()
{
[Link]("Drawing Shape");
}
}
class Circle extends Shape
{
void draw()
{
[Link]("Drawing Circle");
}
}
class Square extends Shape
{
void draw()
{
[Link]("Drawing Square");
}
}
class TestShape
{
public static void main(String[] args)
{
Shape s;
s = new Circle();
[Link]();
s = new Square();
[Link]();
}
}

OUTPUT :

Drawing Circle
Drawing Square
6. MULTIPLE INHERITANCE
PROGRAM:

interface Employee
{
void salary();
}
interface Department
{
void dept();
}
class Company implements Employee, Department
{
public void salary()
{
[Link]("Salary: 30000");
}
public void dept()
{
[Link]("Department: IT");
}
public static void main(String[] args)
{
Company c = new Company();
[Link]();
[Link]();
}
}

OUTPUT:
Salary: 30000
Department: IT
7. MULTI THREADING
PROGRAM:

class MyThread extends Thread


{
public void run()
{
[Link]("Thread is running");
}
public static void main(String[] args)
{
MyThread t = new MyThread();
[Link]();
}
}

OUTPUT :

Thread is running
8. APPLET PROGRAM
PROGRAM

import [Link].*;
import [Link].*;
public class AudioApplet extends Applet implements Runnable
{
Thread t;
AudioClip ac;
public void init()
{
ac = getAudioClip(getCodeBase(), "[Link]");
t = new Thread(this);
[Link]();
}
public void run()
{
[Link]();
}
}

OUTPUT :

C:\student>javac [Link]
C:\student>appletviewer [Link]
9. READ DATA FROM FILE
PROGRAM:

import [Link].*;
class ReadEmployee
{
public static void main(String[] args) throws Exception
{
FileReader fr = new FileReader("[Link]");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = [Link]()) != null)
{
[Link](line);
}
[Link]();
}
}
OUTPUT :

C:\student>javac [Link]
C:\student>java ReadEmployee

class MyThread extends Thread


{
public void run()
{
[Link]("Thread is running");
}
public static void main(String[] args)
{
MyThread t = new MyThread();
[Link]();
}
}
10. STUDENT DATABASE
PROGRAM:

import [Link].*;
class StudentDB
{
public static void main(String[] args) throws Exception
{
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/college", "root", "");
Statement stmt = [Link]();
ResultSet rs = [Link]("select * from student");
while ([Link]())
{
[Link]([Link](1) + " " + [Link](2));
}
[Link]();
}
}

OUTPUT :

1 Ajay
2 Ajith
3 Veena
4 Praveena
5 Praba

You might also like