SRI RAMAKRISHNA MISSION VIDYALAYA
COLLEGE OF ARTS AND SCIENCE
[AUTONOMOUS]
Re-Accredited by NAAC with A+ Grade
Coimbatore – 641 020
OCTOBER – 2024
DEPARTMENT OF COMPUTER APPLICATIONS
NAME : __________________________________
REG. NO : __________________________________
SEMESTER : IIIrd SEMESTER
SUBJECT : Java Programming Lab
SUBJECT CODE: 23UCA3CP3
SRI RAMAKRISHNA MISSION VIDYALAYA
COLLEGE OF ARTS AND SCIENCE (S.F)
[AUTONOMOUS]
Re-Accredited by NAAC with A+ Grade
Coimbatore – 641 020
DEPARTMENT OF COMPUTER APPLICATIONS
BONAFIDE CERTIFICATE
This is to certify that it is a bonafide record work done by
________________________________ in “Java Programming Lab” for the third
Semester during the year October, 2024 Submitted for the Semester Practical
Examinations held on ______________.
Head of the Department Staff in Charge
Internal Examiner External Examiner
INDEX
S. No Date Name of the Practical [Link] Staff Sign
1 INHERITANCE
2 INTERFACE
3 PACKAGES
4 CONSTRUCTOR OVERLODING
5 EXCEPTION HANDLING
(A).Try, Catch and Finally Statements
(B).Throws Statement
6 STRING HANDLING FUNCTIONS
7
THREAD CONCEPT
(A). Single Thread
(B). Multithread
8 TO DISPLAY THE HUMAN FACE USING
APPLET
9 APPLET CONTROLS
10 FILE INPUT STREAMS
11 FILE OUTPUT STREAMS
12 DATABASE READING
13 FINDING IP ADDRESS
[Link]
Date: INHERITANCE
Aim:
To perform the inheritance concept in the java programming language
Algorithm:
Step 1: Create a new text document.
Step2: Type the java program for implementing the inheritance concept.
Step3: create a super class named “employee” and assign the float value of salary,
assign the int value of bonus.
Step4: create a sub class named “programmer” , create the main method
Step5: create an object named “p” and print the variable value of salary and bonus
using “System .out. println” command
Step6: Save the program with “.java” extension
Step7: In the command prompt, compile the program using “javac “ command. If
the error occurs, solve it and run the program using “java" command.
Program:
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer P=new Programmer();
[Link]("Programmer Salary is:"+[Link]);
[Link]("Programmer Bonus is:"+[Link]);
}
}
Output:
Programmer Salary is:40000.0
Programmer Bonus is:10000
Result:
Thus, the Program was Verified and Executed Successfully.
[Link] INTERFACE
Date:
Aim:
To perform the interface concept in the java programming language
Algorithm:
Step 1: create a new text document
Step 2: Type the java program for implementing the interface concept.
Step 3: create a super class named “MyInterface” and assign “method 1”.
Step 4: create a sub class named “ XYZ” and assign the ” method 2”.
Step 5: print the implementing of method 1 and method 2 using
“[Link]” method.
Step6: create an object named “obj”.
Step7: Save the program with “.java” extension in the command prompt and
compile the java program using “javac” command.
Step8: If error occur solve it and run the program using “java” command.
Program:
interface MyInterface
{
public void method1();
public void method2();
}
class XYZ implements MyInterface
{
public void method1()
{
[Link]("Implementation of method1");
}
public void method2()
{
[Link]("Implementation of method2");
}
public static void main(String args[])
{
MyInterface obj = new XYZ();
obj.method1();
obj.method2();
}
}
Output:
Implementation of method1
Implementation of method2
Result:
Thus, the Program was Verified and Executed Successfully.
[Link] PACKAGES
Date:
Aim:
To perform the package concept in the java programming langauge
Algorithm:
Step1: create a new text document.
Step2: Type the java program for implementing the package concept.
Step3: Create a pack named “Mypack” using package keyword and create a super
class named “Balance”.
Step4: Assign the variable name as “name” and “bal”, and initialize “name =n”,
“bal=b”.
Step5: “if” condition is used to check the condition for balance is greater then 0.
Step6: The “import” keyword is used to import the package program.
Step7: Create a main class named “TestBalance”, and create an object named “ob”.
Step8: Print the name and balance.
Step9: Save the super class program and sub class program using “.java” command.
Step10: Using “javac” command for compiling and using “java” command for run the
program.
Program:
package Mypack;
public class Balance
{
public String name;
public double bal;
public Balance(String n,double b)
{
name=n;
bal=b;
}
public void show()
{
if(bal<0)
[Link]("------->");
[Link](name+"$"+bal);
}
}
import [Link];
class TestBalance
{
public static void main(String args[])
{
Balance ob=new Balance("Ramu",99.88);
[Link]();
}
}
Output:
Ramu $ 99.88
Result:
Thus, the Program was Verified and Executed Successfully.
[Link] CONSTRUCTOR OVERLODING
Date:
Aim:
To perform the constructor using method overloading concept in the java programming
language
Algorithm:
Step1: Create a new text document and type the java program for implementing
constructor overloading concept.
Step2: Create a base class named “over “ and initialize the employee no, name, job
and salary.
Step3: Using “this” operator we can get the instance variable and using in various
methods.
Step4: Print emp name, no, job, and salary using “[Link]()” method.
Step5: Create a main class named “ consover” and create object x1, x2, x3 and x4.
Step6: Display the value of x1, x2, x3, and x4.
Step7: Save the program with “.java” extension.
Step8: Compile it using “javac” command and run the program using “java”
command.
Program:
class over
{
int eno;
String ename,job;
double sal;
over()
{
eno=0;
ename=job=null;
sal=0.0;
}
over(int eno)
{
[Link]=eno;
[Link]="Ramesh";
[Link]="Salesman";
[Link]=4500;
}
over(int eno,String ename)
{
[Link]=eno;
[Link]=ename;
[Link]="Clerk";
[Link]=5600;
}
over(int eno,String ename,String job,double sal)
{
[Link]=eno;
[Link]=ename;
[Link]=job;
[Link]=sal;
}
void Display()
{
[Link]("Employee Number:"+eno);
[Link]("Employee name:"+ename);
[Link]("Job:"+job);
[Link]("Salary:"+sal);
}
}
class consover
{
public static void main(String args[])
{
over x1=new over();
over x2=new over(101);
over x3=new over(102,"Ganesh");
over x4=new over(103,"Dinesh","programmer",6700);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output:
Employee Number:0
Employee name:null
Job:null
Salary:0.0
Employee Number:101
Employee name:Ramesh
Job:Salesman
Salary:4500.0
Employee Number:102
Employee name:Ganesh
Job:Clerk
Salary:5600.0
Employee Number:103
Employee name:Dinesh
Job:programmer
Salary:6700.0
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]
Date: EXCEPTION HANDLING
(A).Try, Catch and Finally Statements
Aim:
To perform the Exception handling concept in the java programming language
Algorithm:
Step1: Create a new text document and type the java program for implementing the
exception handling concept.
Step2: Create a parent class named “Examfinally”.
Step3: Intialize the variable “a=65” as integer and “b=0” as integer in the main
program.
Step4: “try” block is the used to handling the errors, and “catch” block is used to
handling the errors.
Step5: “finally” block is used to execute the program for run the program upto the
last line.
Step6: Save the program using “.java” command.
Step7: Compile and run the program using “javac” and “java” commands.
Program :
class Examfinally
{
public static void main(String arg[])
{
int a;
try
{
a=[Link](arg[0]);
}
catch(ArithmeticException e)
{
[Link]("Error:"+e);
}
catch(ArrayStoreException e1)
{
[Link]("Error:"+e1);
}
catch(ArrayIndexOutOfBoundsException e2)
{
[Link]("Error:"+e2);
}
finally
{
[Link]("I am Always Executed....");
}
}
}
Output:
Error:[Link]: 0
I am Always Executed....
Result:
Thus, the Program was Verified and Executed Successfully.
(B).Throws Statement
Aim:
To perform the exception handling concept in the java programming language.
Algorithm:
Step1: Create a new text document and type the java program for implementing the
exception handling concept.
Step2: “import” keyword is used to import the java program.
Step3: Create a parent class named “ExamThrows”.
Step4: Intialize the variable “v=0” as intger and “ch” as character.
Step5: “switch” statement is used to check the option cases and print the option case
which is selected.
Step6: Save the program using “,java” command.
Step7: Compile and run the program using “javac” and “java” commands.
Program:
import [Link].*;
class ExamThrows
{
public static void main(String args[])throws IOException
{
int v=0;
char ch;
while((ch=(char)[Link]())!='.')
{
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':v++;
[Link]("numbers of vowels:"+v);
}
}
}
}
Output:
i am deepan.
numbers of vowels:1
numbers of vowels:2
numbers of vowels:3
numbers of vowels:4
numbers of vowels:5
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]
Date: STRING HANDLING FUNCTIONS
Aim:
To perform the string handling mathods concept in the java programming language.
Algorithm:
Step 1: Create a new text document and type the java program for implementing the
string handling concept.
Step 2: Create a parent class named ”ExamString”.
Step 3: Assign the variable “s” as string and create an object named “s”.
Step 4: Print the string length using “length()” method.
Step 5: Print the char’s position using “charAt()” method.
Step 6: Compare the strings using “compareTo()” method.
Step 7: Concatinate the string using “concat()” method.
Step 8: Print the string from lower to upper using “toUpperCase()” method.
Step 9: Remove the space before and after the string using “trim()” method.
Step 10: Save the program using “,java” command, To compile and run the java
program using “javac” and “java” commands.
Program:
import [Link];
class ExamString
{
public static void main(String args[])
{
String s=new String("Hello");
[Link]("String Object length :"+[Link]());
[Link]("Char at position :"+[Link](1));
[Link]("Comparing Object :"+[Link]("hello"));
[Link]("String Concadination :"+[Link]("hw"));
[Link]("String end with :"+[Link]("o"));
[Link]("Equals :"+[Link]("hello"));
[Link]("Index of :"+[Link]("o"));
[Link]("Replace Char :"+[Link]('H','Z'));
[Link]("Start with :"+[Link]("Z"));
[Link]("Sub String :"+[Link](1,4));
[Link]("Sub String :"+[Link](1));
[Link]("Upper Lower :"+"Hello".toLowerCase());
[Link]("Lower to Upper :"+"hello".toUpperCase());
[Link]("Trim th space :"+" Hello ".trim());
}
}
Output:
String object length :5
char At position :e
comparing object :0
String concatination :Hellohow
String EndsWith :false
Equals :true
Index of :-1
Replace char :Zello
StartsWith :false
SubString :ell
SubString :ello
Upper to Lower :hello
Lower to Upper :HELLO
Trim the Space :Hello
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]: 7
Date: THREAD CONCEPT
(A).Single Thread
Aim:
To perform the singlethread concept in the java programming language
Algorithm:
Step1: Create a new text doucement and type the java prograram for implementing
the thread concept.
Step2: Create a paraent class named “SingleThread” and initialize the variable “i” as
integer.
Step3: “for” loop is used for executing the thread and “try” block is used to find the
errors, if the program is stopped.
Step4: “catch” block is used to catch the errors.
Step5: Save the java program using “.java” extension.
Step6: Compile and run the java program using “javac” command and “java”
command.
Program:
import [Link];
class SingleThread
{
public static void main(String arg[])
{
int i;
for(i=1;i<=5;i++)
{
try
{
[Link](1000);
[Link]("I="+i);
}
catch(InterruptedException e)
{
[Link]("Error:"+[Link]());
}
}
}
}
Output:
I=1
I=2
I=3
I=4
I=5
Result:
Thus, the Program was Verified and Executed Successfully.
(B)Multithread
Aim:
To perform a multi thread concept using java programming language.
Algorithm:
Step1: Create a new text document and type the java program for implementing the
threads concept.
Step2: Creat a child class named "A" and intialized variable "i" as integer.
Step3: “for” loop is used for executing the thread, “try” block and catch block is
used in "run()" for print the error.
Step4: Creat a child class named "B" and “for” loop is used for executing the thread
“try” block and “catch” block, is used in "run()" for print the error.
Step5: Creat a child class name "C" and for loop is used for executing the thread
“try” block, “catch” block , is used in "run()" for print the error.
Step6: Creat a main class named "Multithread" and create an object ‘a’, ‘b; and ‘c’.
Step7: Print the child classes using "start()" method.
Step8: Save, compile, and run the java program using “.java”, “javac”, and “java”
commands.
Program:
import [Link];
class A extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
try
{
[Link](500);
[Link]("A="+i);
}
catch(InterruptedException e){}
}
}
}
class B extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
try
{
[Link](500);
[Link]("B="+i);
}
catch(InterruptedException e){}
}
}
}
class C extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
try
{
[Link](500);
[Link]("C="+i);
}
catch(InterruptedException e){}
}
}
}
class MultiThread
{
public static void main(String arg[])
{
A a=new A();
B b=new B();
C c=new C();
[Link]();
[Link]();
[Link]();
}}
Output:
C=0
A=0
B=0
C=1
A=1
B=1
A=2
C=2
B=2
A=3
C=3
B=3
C=4
A=4
B=4
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]
Date: TO DISPLAY THE HUMAN FACE USING APPLET
Aim:
To Perform the Java Applet Program to dispaly the Human Face.
Algoritham:
Step1:create a new text document and type the java program for implementing the java
applet concept.
Step2:assign the window's height and width.
Step3: "import" keyword is used to import the java applet class.
Step4:set background color using "setBackground()" method.
Step5:draw a Arc using "drawArc()" method draw a Arc method and draw a circle using
"drawOval" method.
Step6: "xpoints" , "Ypoints" and “Zpoints” is assigned as an integer array and assign "points"
as integer for displaying the object.
Step7:save the java program using ".java" command.
Step8:compile and run the java program using "javac" and appletviewer [Link]"
command.
Program:
/*<applet code="[Link]" height=500 width=400> </applet>*/
import [Link].*;
public class JavaMan1 extends [Link]
{
public void init()
{
setBackground([Link]);
}
public void paint(Graphics screen)
{
//Change color to yellow
[Link]([Link]);
//Draw and fill the face
[Link](100,100,250,250,0,360);
[Link](100,100,250,250,0,360);
//Change color to black
[Link]([Link]);
//Draw the left eye
[Link](170,185,25,25,0,360);
[Link](170,185,25,25,0,360);
//Draw the right eye
[Link](255,185,25,25,0,360);
[Link](255,185,25,25,0,360);
//Draw the smile
[Link](150,215,150,100,0,-180);
}
}
Output:
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]
Date: APPLET CONTROLS
Aim
To Perform the Various java Applet Events uisng Abstract Window Toolkit concept.
Algorithm:
step1: Create a new text document and type the java program for implementing the applet concept
step2: First intialize the width and height of applet window
step3: "import" keyword is used to import the java applet class
step4: create a base class named "Simple" and create a object named button, checkbox and choice
step5: Fill the choice using
"[Link] item()" method.
step7: Add thebutton, checkbox, choice, lable, list and scrollbar in the web form using "add()"
method.
step8: Save compile and run the java program using ".java" "javac" and "java" commands.
Program:
/* <applet code="[Link]" height=350 width=350> </applet> */
import [Link].*;
public class simple extends [Link]
{
public void init()
{
Button button = new Button("Quit");
Checkbox checkbox = new Checkbox("Test");
Choice choice = new Choice();
//fill the choice
[Link]("Clinton");
[Link]("Dole");
[Link]("Perot");
[Link]("Browne");
[Link]("Nadar");
[Link]("Ismayil");
Label label = new Label("This ia a label");
Label label2 = new Label("This is Label2");
List list = new List(5,false);
//fill the list
[Link]("Clinton");
[Link]("Dole");
[Link]("Perot");
[Link]("Browne");
[Link]("Nadar");
[Link]("Ismayil");
Scrollbar scrollbar = new Scrollbar([Link]);
Scrollbar scrollbar2 = new Scrollbar([Link]);
//add the controls to the default layout
add(button);
add(checkbox);
add(choice);
add(label);
add(list);
add(scrollbar);
add(label2);
add(scrollbar2);
}
}
Output:
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]
FILE INPUT STREAM
Date:
Aim:
To Perform the File input Stream concept using Java Programming Language
Algorithm:
Step1: Create a new text document and type the java program for implementing the
file input stream concept.
Step2: “Import" keyword is used to import the java program.
Step3: Create a base class named SimpleRead.
Step4: "try" block is used to find the errors, if the program is stopped.
Step5: Create an object named "fin" and assign the variables "i=0" as Integer.
Step6: "while" loop is used in this program for read the String.
Step7: Print the character using "[Link]()" method and close the
inputstream using "[Link]()" method.
Step8: "catch" block is used to catch the error.
Step9: save the java program using ".java" command compile and run the java
program using "javac" and "java" command.
Program :
import [Link].*;
class SimpleRead
{
public static void main(String args[])
{
try{
FileInputStream fin = new FileInputStream("[Link]");
int i=0;
while((i=[Link]())!=-1)
{
[Link]((char)i);
}
[Link]();
}catch(Exception e)
{
[Link](e);
}
}
}
OUTPUT:
Result:
Thus, the Program was Verified and Executed Successfully.
[Link]
Date: FILE OUTPUT STREAM
Aim:
To Perform the File input Stream concept using Java Programming Language
Algorithm:
Step1: Create a new text document and type the java program for
implementing the file output stream concept.
Step2: "Import" keyword is used to import the java program.
Step3: Create a base class named SimpleRead.
Step4: "try" block is used to find the errors, if the program is stopped.
Step5: Create an object named "fout".
Step6: Print the character using "[Link]()" method and close
the outputstream using "[Link]()" method.
Step7: "catch" block is used to catch the error.
Step8: Save the java program using ".java" command compile and run
the java program using "javac" and "java" command.
Program :
import [Link].*;
class Test
{
public static void main(String args[])
{
try{
FileOutputStream fout = new FileOutputStream("[Link]");
String s = "Sachin Tendulkar is my favourite player";
byte b[] = [Link](); //converting string into byte array
[Link](b);
[Link]();
[Link]("Success...");
}catch(Exception e)
{
[Link](e);
}
}
}
Output:
Success...
Result:
Thus, the Program was Verified and Executed Successfully.
[Link] DATABASE READING
Date:
Aim:
To Perform the Database connectivity concept for data reading using Java Programming
Language
Algorithm:
Step1: Create a new text document and type the java program for implementing the
Database connectivity concept for data reading.
Step2: “Import" keyword is used to import the java io package and [Link] package.
Step3: Create a base class named products.
Step4: "try" block is used to find the errors, if the program is stopped.
Step5: Create the database connectivity using [Link] statement.
Step6: Create Stmt object for Reading a data from table from the database.
Step7: Print the data, in the Studentname, Studentage, Studentclass as field in the table from
the database.
Step8: using the while loop to print all the data, which is the database table.
Step9: save the java program using ".java" command compile and run the java
program using "javac" and "java" command.
Program:
import [Link].*;
import [Link].*;
class products
{
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection Con=[Link]("jdbc:odbc:mydata"," "," ");
Statement Stmt=[Link]();
//studentname,studentage,class
ResultSet rs=[Link]("Select * from Table1");
while([Link]())
{
[Link]("Studentname:"+[Link]("studentname"));
[Link]("Studentage:"+[Link]("studentage"));
[Link]("Studentclass:"+[Link]("class"));
[Link]();
}
[Link]();
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
Output:
Result:
Thus, the Program was Verified and Executed Successfully.
[Link] FINDING IP ADDRESS
Date:
Aim:
To perform the network concept of finding IP address using Java Programming Language
Algorithm:
Step1: Create a new text document and type the java program for implementing the
Networking concept of finding IP address.
Step2: “Import" keyword is used to import the java InetAddress package.
Step3: Create a base class named IPAddress.
Step4: throws Exception block is used to find the errors, if the program is stopped.
Step5: Create the addr object for finding the IP Address.
Step6: Print the IP Address and host name using String datatype with hostname object.
Step7: save the java program using ".java" command compile and run the java
program using "javac" and "java" command.
Program:
import [Link];
public class IPAddress
{
public static void main(String args[]) throws Exception
{
InetAddress IP = [Link]();
[Link]("IP of my system is := "+[Link]());
String hostname = [Link]();
[Link]("Local host name: "+hostname);
}
}
Output:
Result:
Thus, the Program was Verified and Executed Successfully.