0% found this document useful (0 votes)
8 views49 pages

Java Programming Lab

This document outlines the Java Programming Lab syllabus for the third semester at SRI Ramakrishna Mission Vidyalaya, including practical exercises on inheritance, interfaces, packages, constructor overloading, exception handling, string handling, and threading concepts. Each exercise includes an aim, algorithm, program code, output, and verification results. It serves as a record of the practical work done by students in the course.

Uploaded by

pranesh.s787
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)
8 views49 pages

Java Programming Lab

This document outlines the Java Programming Lab syllabus for the third semester at SRI Ramakrishna Mission Vidyalaya, including practical exercises on inheritance, interfaces, packages, constructor overloading, exception handling, string handling, and threading concepts. Each exercise includes an aim, algorithm, program code, output, and verification results. It serves as a record of the practical work done by students in the course.

Uploaded by

pranesh.s787
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/ 49

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 Page.No 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
Ex.No:1
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();
System.out.println("Programmer Salary is:"+P.salary);
System.out.println("Programmer Bonus is:"+P.bonus);
}
}

Output:
Programmer Salary is:40000.0
Programmer Bonus is:10000
Result:

Thus, the Program was Verified and Executed Successfully.

Ex.No:2 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
“System.out.println” 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()
{
System.out.println("Implementation of method1");
}
public void method2()
{
System.out.println("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.

Ex.No:3 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)
System.out.print("------->");
System.out.println(name+"$"+bal);
}
}
import Mypack.Balance;
class TestBalance
{
public static void main(String args[])
{
Balance ob=new Balance("Ramu",99.88);
ob.show();
}
}

Output:
Ramu $ 99.88
Result:

Thus, the Program was Verified and Executed Successfully.

Ex.No:4 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 “System.out.println()” 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)
{
this.eno=eno;
this.ename="Ramesh";
this.job="Salesman";
this.sal=4500;
}
over(int eno,String ename)
{
this.eno=eno;
this.ename=ename;
this.job="Clerk";
this.sal=5600;
}

over(int eno,String ename,String job,double sal)


{
this.eno=eno;
this.ename=ename;
this.job=job;
this.sal=sal;
}
void Display()
{
System.out.println("Employee Number:"+eno);
System.out.println("Employee name:"+ename);
System.out.println("Job:"+job);
System.out.println("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);
x1.Display();
x2.Display();
x3.Display();
x4.Display();
}
}
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.

Ex.No:5
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=Integer.parseInt(arg[0]);
}
catch(ArithmeticException e)
{
System.out.println("Error:"+e);
}
catch(ArrayStoreException e1)
{
System.out.println("Error:"+e1);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Error:"+e2);
}
finally
{
System.out.println("I am Always Executed....");
}
}
}

Output:
Error:java.lang.ArrayIndexOutOfBoundsException: 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 java.io.*;
class ExamThrows
{
public static void main(String args[])throws IOException
{
int v=0;
char ch;
while((ch=(char)System.in.read())!='.')
{
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':v++;
System.out.println("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.

Ex.No:6
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 java.lang.String;
class ExamString
{
public static void main(String args[])
{
String s=new String("Hello");
System.out.println("String Object length :"+s.length());
System.out.println("Char at position :"+s.charAt(1));
System.out.println("Comparing Object :"+s.compareTo("hello"));
System.out.println("String Concadination :"+s.concat("hw"));
System.out.println("String end with :"+s.endsWith("o"));
System.out.println("Equals :"+s.equals("hello"));
System.out.println("Index of :"+s.indexOf("o"));
System.out.println("Replace Char :"+s.replace('H','Z'));
System.out.println("Start with :"+s.startsWith("Z"));
System.out.println("Sub String :"+s.substring(1,4));
System.out.println("Sub String :"+s.substring(1));
System.out.println("Upper Lower :"+"Hello".toLowerCase());
System.out.println("Lower to Upper :"+"hello".toUpperCase());
System.out.println("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.

Ex.No: 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 java.lang.Thread;
class SingleThread
{
public static void main(String arg[])
{
int i;
for(i=1;i<=5;i++)
{
try
{
Thread.sleep(1000);
System.out.println("I="+i);
}
catch(InterruptedException e)
{
System.out.println("Error:"+e.getMessage());
}
}
}
}

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 java.lang.Thread;
class A extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(500);
System.out.println("A="+i);
}
catch(InterruptedException e){}
}
}
}
class B extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(500);
System.out.println("B="+i);
}
catch(InterruptedException e){}
}
}
}
class C extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
try
{
Thread.sleep(500);
System.out.println("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();
a.start();
b.start();
c.start();
}}
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.

Ex.No:8
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 JavaMa.java"
command.
Program:
/*<applet code="JavaMan1.class" height=500 width=400> </applet>*/
import java.awt.*;
public class JavaMan1 extends java.applet.Applet
{
public void init()
{
setBackground(Color.red);
}
public void paint(Graphics screen)
{
//Change color to yellow

screen.setColor(Color.yellow);
//Draw and fill the face
screen.drawArc(100,100,250,250,0,360);
screen.fillArc(100,100,250,250,0,360);
//Change color to black
screen.setColor(Color.black);
//Draw the left eye
screen.drawArc(170,185,25,25,0,360);
screen.fillArc(170,185,25,25,0,360);
//Draw the right eye
screen.drawArc(255,185,25,25,0,360);
screen.fillArc(255,185,25,25,0,360);
//Draw the smile
screen.drawArc(150,215,150,100,0,-180);
}
}
Output:

Result:
Thus, the Program was Verified and Executed Successfully.

Ex.No:9
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
"choice.add 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="simple.class" height=350 width=350> </applet> */
import java.awt.*;
public class simple extends java.applet.Applet
{
public void init()
{
Button button = new Button("Quit");
Checkbox checkbox = new Checkbox("Test");
Choice choice = new Choice();
//fill the choice
choice.addItem("Clinton");
choice.addItem("Dole");
choice.addItem("Perot");
choice.addItem("Browne");
choice.addItem("Nadar");
choice.addItem("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
list.addItem("Clinton");
list.addItem("Dole");
list.addItem("Perot");
list.addItem("Browne");
list.addItem("Nadar");
list.addItem("Ismayil");
Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL);
Scrollbar scrollbar2 = new Scrollbar(Scrollbar.VERTICAL);
//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.

Ex.No:10
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 "System.out.println()" method and close the
inputstream using "fin.close()" 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 java.io.*;
class SimpleRead
{
public static void main(String args[])
{
try{
FileInputStream fin = new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1)
{
System.out.println((char)i);
}
fin.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
Result:

Thus, the Program was Verified and Executed Successfully.

Ex.No:11
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 "System.out.println()" method and close
the outputstream using "fout.close()" 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 java.io.*;
class Test
{
public static void main(String args[])
{
try{
FileOutputStream fout = new FileOutputStream("abc.txt");
String s = "Sachin Tendulkar is my favourite player";
byte b[] = s.getBytes(); //converting string into byte array
fout.write(b);
fout.close();
System.out.println("Success...");
}catch(Exception e)
{
System.out.println(e);
}
}
}

Output:
Success...
Result:
Thus, the Program was Verified and Executed Successfully.

Ex.No:12 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 java.sql 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 sun.jdbc.odbc.JdbcOdbcDriver 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 java.io.*;
import java.sql.*;
class products
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con=DriverManager.getConnection("jdbc:odbc:mydata"," "," ");
Statement Stmt=Con.createStatement();
//studentname,studentage,class
ResultSet rs=Stmt.executeQuery("Select * from Table1");
while(rs.next())
{
System.out.println("Studentname:"+rs.getString("studentname"));
System.out.println("Studentage:"+rs.getString("studentage"));
System.out.println("Studentclass:"+rs.getString("class"));
System.out.println();
}
Stmt.close();
Con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

Result:

Thus, the Program was Verified and Executed Successfully.


Ex.No:13 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 java.net.InetAddress;
public class IPAddress
{
public static void main(String args[]) throws Exception
{
InetAddress IP = InetAddress.getLocalHost();
System.out.println("IP of my system is := "+IP.getHostAddress());
String hostname = IP.getHostName();
System.out.println("Local host name: "+hostname);

}
}
Output:

Result:

Thus, the Program was Verified and Executed Successfully.

You might also like