You are on page 1of 54

Paper Name : JAVA PROGRAMMING LAB

Paper Code :
Batch : 2021-2023
Class & Semester : I MCA & 1ST Semester
SEMESTER I
Core Course - V - LAB – I JAVA PROGRAMMING LAB
1. Write a java application program to demonstrate class with constructors.
2. Write a java application program to demonstrate inheritance, interface and package.
3. Write a java application program to implement exception and Multi threading
concept.
4. Write a program to read, write and copy a file using byte streams or character
streams.
5. Develop a banking system using AWT and event handling.
6. Develop a programs using Swing to display the personal detail of an employee.
7. Implement TCP/IP and UDP protocol for message communication.
8. Using JDBC develop a student information system.
9. Implement client/server communication using servlets.
10. Develop a web page using JSP.
SEMESTER -I

JAVA PROGRAMMING LAB

1. Write a java application program to demonstrate class with constructors.

AIM:

To develop a java program using class with Constructors.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create a class EB bill that contains get data (), cell (),

method.STEP 3: Create a main class and assign the object

of EB bill class.

STEP 4: Call the get data () using gets the input from the

user. STEP 5: Call the call () to calculate the amount.

STEP 6: Point the result.


CODING:

class electric_bill {int


units,code;
int r;
String name; void
getdata()
{
Scanner input=new Scanner(System.in);
System.out.println("Enter the name");
name=input.next();
System.out.println("Enter the code");
code=input.nextInt();
System.out.println("Enter the units");
units=input.nextInt();
}
int calculate()import java.util.Scanner;

{
int u;
if(units<=200)
{
r=units*1;
}
else
{
u=units-200;
r=100+u*2;
}
System.out.println("t\t\t ELECTRIC BILL");
System.out.println(“ “); System.out.println("\
tNAME :"+name); System.out.println("\tCODE :"+code);
System.out.println("\tUNITS CONSUMED :"+units);
System.out.println(" ");
System.out.println("\tTOTAL AMOUNT TO BE PAID
:"+r); return 0;
}
}
class show
{ public static void main(String args[])
{ int rs; electric_bill e=new electric_bill();
e.getdata();
rs=e.calculate();
}
}
OUTPUT:

C:\jdk1.7.0\bin>javac show.java
C:\jdk1.7.0\bin>java show
Enter the name
BHUVAN Enter the code
003
Enter the units 120
ELECTRIC BILL

NAME :BHUVANCODE :3
UNITS CONSUMED :120

TOTAL AMOUNT TO BE PAID :120

C:\jdk1.7.0\bin>

RESULT:

Thus the above java program has been executed successfully.


2. Write a java application program to demonstrate inheritance, interface and package

AIM:

To develop a java program using Inheritance, Interface, Package.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create package employee.

STEP 3: Create a class EMP inside employee package emp class contain

Constructor,call(),display() method.

STEP 4: Create Empay class and import package create object for emp

class.

STEP 5: Call the emp class() call(),display()method.

STEP 6: Stop the result.


CODING:

Compile the package and save Emp Class file to Employee1


folder.package Employee1;

public class Emp


{
String name,empid,cty; int
bpay; double hra ,da,npay,pf;
public Emp(String n,String id,String c,int b)
{
name=n; empid=id;
cty=c;
bpay=b;
}
public void call()
{
da=bpay*0.05; hra=bpay*0.05;
pf=bpay*0.08;
npay=bpay+da+hra-pf;
}

public void display()


{
System.out.println("\n\t\t EMPLOYEE
DETAILS"); System.out.println("\n Name :
"+name); System.out.println("\n Employee ID :
"+empid);
System.out.println("\n Category : "+cty);
System.out.println("\n bpay : "+bpay);
System.out.println("\n hra : "+hra);

System.out.println("\n da : "+da);
System.out.println("\n pf: "+pf);
System.out.println("\n npay: "+npay);
}

}
OUTPUT:

C:\jdk1.7.0\bin>javac Emp.java
C:\jdk1.7.0\bin>javac Emppay.java
C:\jdk1.7.0\bin>java Emppay

EMPLOYEE DETAILS
Name : Rama
Employee ID
:IT101 Category :
Female bpay :
10000
hra : 500.0
da : 500.0
pf: 800.0
npay: 10200.0

RESULT:
Thus the above java program has been executed successfully.
3.Write a java application program to implement exception and Multi threading
concept.

AIM:

To develop a java program to implement multithread using priorities.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create a class named as which thread class.

STEP 3: Define the method run.

STEP 4: Create a main class sample and creating object for

class . STEP 5: Set priority with the class A,B,C.

STEP 6: stop the result.


CODING:

class A extends Thread


{
public void run(){
System.out.println("Thread A Started");
for(int i=1;i<=4;i++)
{
System.out.println("\tThread A : ="+i);
}
System.out.println("Exit From A");
}
}

class B extends Thread


{
public void run(){
System.out.println("Thread B
Started"); for(int j=1;j<=4;j++)
{
System.out.println("\tThread B : ="+j);
}
System.out.println("Exit From B");
}
}
class C extends Thread
{
public void run(){
System.out.println("Thread C
Started"); for(int k=0;k<=4;k++)
{
System.out.println("\tThread c : ="+k);
}
System.out.println("Exit From C");
}
}

class sample{
public static void main(String args[]){A
threadA=new A();
B threadB=new B();C
threadC=new C();
threadA.start();
threadB.start(); threadC.start();
}

}
OUTPUT:

C:\jdk1.7.0\bin>javac sample.java C:\


jdk1.7.0\bin>java sample Thread A
Started
Thread C StartedThread B
Started
Thread B : =1
Thread B : =2
Thread B : =3
Thread B : =4
Thread c : =0
Thread c : =1
Thread A : =1
Thread c : =2
Thread c : =3
Thread c : =4
Exit From CExit From B
Thread A : =2
Thread A : =3
Thread A : =4
Exit From A
C:\jdk1.7.0\bin>

RESULT:

Thus the above java program has been executed successfully.


3(B).EXCEPTION HANDLING:

AIM:

To write a program to illustrate the use at exception handling.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create a class are try block and catch block for the

following. STEP 3: Array format exception.

STEP 4:Array index out of bounds exception.

STEP 5:Write code the try and catch block.

STEP 6: stop the Program.


CODING:

class Division {
public static void main(String[] args) {
int a, b, result;
Scanner input = new Scanner(System.in);
System.out.println("Input two integers"); a
= input.nextInt();

b = input.nextInt();
try {
result = a / b;
System.out.println("Result = " + result);
}
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by
zero.");

}
}
}
OUTPUT:

C:\jdk1.7.0\bin>javac Division.java
C:\jdk1.7.0\bin>java Division

Input two integers5

0
Exception caught: Division by
zero. C:\jdk1.7.0\bin>

RESULT:

Thus the above java program has been executed successfully.


4.Write a program to read, write and copy a file using byte streams or character
streams.

AIM:

To develop a java program to read and write with using byte system.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Write a text to given text file using data output system(2,1) import

the java.

STEP 3: Read the text form the same file using data input stream.

STEP 4: Copy character by character from given file to new file using file input and

output stream.

STEP 5: Print the output.

STEP 6: stop the result.


CODING:

import java.io.*;
public class DataInput_Stream{
public static void main(String args[])throws IOException{
DataOutputStream dataOut = new DataOutputStream(new
FileOutputStream("myfile.txt")); dataOut.writeUTF("hello hai welcome");
DataInputStream dataIn = new DataInputStream(new
FileInputStream("myfile.txt")); while(dataIn.available()>0){
String k =
dataIn.readUTF();
System.out.print(k+" ");
}
}
}
OUTPUT:

C:\jdk1.7.0\bin>javac DataInput_Stream.java
C:\jdk1.7.0\bin>java DataInput_Stream
hello hai
welcome C:\
jdk1.7.0\bin>

RESULT:

Thus the above java program has been executed successfully.


5. Develop a banking system using AWT and event handling.

AIM:

To develop a java program using applet.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create a sum class extends applet and implement action

listener.

STEP 3: Insert next field button on the applet window.

STEP 4: Develop the program.

STEP 5: Execute the program.

STEP 6: Stop the result.


CODING:

import java.awt.*; import


java.applet.*; import
java.awt.event.*;
/*<applet code=ae height=100 width=290>
</applet>*/
public class ae extends Applet implements
ActionListener{ TextField tf;
public void init()
{
tf=new TextField(25);
Button b=new Button("click
me"); add(tf);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
}
OUTPUT:

RESULT:
Thus the above java program has been executed successfully.
5(B).SWING EVENT HANDLER:

AIM:

To Develop a java program using swing.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create a class and implement action listener.

STEP 3: Insert table and one button raise the event handling

method.

STEP 4: Execute event handling method.

STEP 5: Stop the process.


CODING:

import javax.swing.*; import


java.awt.*; import
java.awt.event.*;
class ss extends JFrame implements ActionListener
{
private int count
=0; JLabel lblData;
ss()
{
setLayout(new FlowLayout());
lblData = new JLabel("Button Clicked 0
Times");JButton btnClick=new JButton("Click
Me"); btnClick.addActionListener(this);
add(lblData);
add(btnClick);
}
public void actionPerformed(ActionEvent e)
{
count++;
lblData.setText("Button Clicked " + count +" Times");
}
}
class sap
{
public static void main(String args[])
{
ss frame = new ss();
frame.setTitle("Event Handling Java Example");
frame.setBounds(200,150,180,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
OUTPUT:

RESULT:

Thus the above java program has been executed successfully.


6. Develop a programs using Swing to display the personal detail of an employee.

AIM:

To develop a java program Personal Details of an Employee.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create package employee.

STEP 3: Create a class EMP inside employee package emp class contain

Constructor,call(),display() method.

STEP 4: Create Empay class and import package create object for emp

class.

STEP 5: Call the emp class() call(),display() method.

STEP 6: stop the result.


CODING:

Compile the package and save Emp Class file to Employee1


folder.package Employee1;
public class Emp
{
String name,empid,cty;
int bpay;
double hra ,da,npay,pf;
public Emp(String n,String id,String c,int b)
{
name=n;
empid=id;cty=c;
bpay=b;
}
public void call()
{
da=bpay*0.05;
hra=bpay*0.05;
pf=bpay*0.08;
npay=bpay+da+hra-pf;
}

public void display()


{
System.out.println("\n\t\t EMPLOYEE
DETAILS"); System.out.println("\n Name :
"+name); System.out.println("\n Employee ID :
"+empid); System.out.println("\n Category :
"+cty); System.out.println("\n bpay : "+bpay);
System.out.println("\n hra : "+hra);
System.out.println("\n da : "+da);
System.out.println("\n pf: "+pf);
System.out.println("\n npay: "+npay);

}
}
OUTPUT:

C:\jdk1.7.0\bin>javac Emp.java
C:\jdk1.7.0\bin>javac Emppay.java
C:\jdk1.7.0\bin>java Emppay

EMPLOYEE DETAILS
Name : Rama Employee
ID : IT101
Category : Female
bpay : 10000
hra : 500.0
da : 500.0
pf: 800.0
npay: 10200.0

RESULT:
Thus the above java program has been executed successfully.
7.Implement TCP/IP and UDP protocol for message communication.

AIM:

To develop a java program to create in simple TCP/IP and UDP protocol for
messagecommunication.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Declare the datagram socket and datagram packages.

STEP 3: Start the chat source and message for client wait units the client

information. STEP 4: Receive the message from server and given response

to server from client.

STEP 5: Continue the unit the client type s to p.

STEP 6: stop the program.


CODING:

import java.io.*; import


java.net.*;class
UDPserver{
public static DatagramSocket serversocket;
public static DatagramPacket dp;
public static BufferedReader
dis; public static InetAddress ia;
public static byte buf[]=new byte[1024];
public static int cport=789,sport=790;
public static void main(String[] a)throws
IOException{serversocket=new
DatagramSocket(sport);
dp=new DatagramPacket(buf,buf.length);
dis=new BufferedReader(new
InputStreamReader(System.in));
ia=InetAddress.getLocalHost();
System.out.println("Server is Running...");
while(true)
{
serversocket.receive(dp);
String str=new String(dp.getData(),0,
dp.getLength());
if(str.equals("Stop"))
{
System.out.println("Terminated...");break;
}
System.out.println("client:"+str); String
str1=new String(dis.readLine());
buf=str1.getBytes();
serversocket.send(new DatagramPacket(buf,str1.length(),ia,cport));
}
}}
OUTPUT

RESULT:
Thus the above java program has been executed successfully.
8.Using JDBC develop a student information system.

AIM:

To Develop a java program for student mark list using JDBC.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create DNS for connecting database.

STEP 3: Get the import from user like name, no mark. STEP 4:

Calculate total argument and display text box. STEP 5: Store the

information to database.

STEP 6: stop the program.


CODING:
import java.sql.*;public
class jdbc
{
public static void main(String[] args)
{
Connection c; Statement st;
String createstring;
Create String="CREATE TABLE TEA(TEA_NAME
VARCHAR2(30),SUPP_ID NUMBER(6),PRICE NUMBER(6,2),SALES
NUMBER(6,2),TOTAL NUMBER(6,2))";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
c=DriverManager.getConnection("jdbc:odbc:sample1","scott","tiger");
st=c.createStatement();
st.executeUpdate(createstring);
st.close();
c.close();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
}
}
OUTPUT:

Sql>desc tea;

TEA_NAME VARCHAR2(30),
SUPP_ID NUMBER(6),
PRICE NUMBER(6,2),
SALES NUMBER(6,2),
TOTAL NUMBER(6,2))

RESULT:

Thus the above java program has been executed successfully.


9.Implement client/server communication using servlets.

AIM:

To Implement Client/Server communication using Servlets in java program.

PROCEDURE:

STEP 1: Start the program.

STEP 2: Create class WishApp.

STEP 3: public void service (HttpServletRequest req, HttpServletResponseres)


throws IOException, ServletException
STEP 4: int hour = cal.get(Calendar.HOUR_OF_DAY);//24 hrs format
STEP 5: //generate wish message
STEP 6: stop the result.
CODING:
Wish App.java

import java.io.*; import


java.util.*; import
javax.servlet.*;
import javax.servlet.http.*;
public class WishApp extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws
IOException, ServletException
{
//set response content type res.setContentType("text/html");
//get printWrite obj
PrintWriter pw = res.getWriter();
//write request processing logic to generate wish message
Calendar cal = Calendar.getInstance();
//get current hours of the day
int hour = cal.get(Calendar.HOUR_OF_DAY);//24 hrs format
//generate wish message
if(hour<12)
pw.println("Good Morning!!");else
if (hour < 16) pw.println("Good
afternoon");else if(hour<20)
pw.println("Good evening"); else
pw.println("Good night");

pw.println("<br><br><a href= '../WishSrvApp/index.html'>Home</a>");


//close stream objectpw.close();
}
}
Index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wishing message</title>
</head>
<body>
<a href = "http://localhost:2020/WishSrvApp/test">Get wishing</a>
</body>
</html>
Web.xml

<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>WishApp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Output:

When we click on the above link In the morning it displays following message.

RESULT:

Thus the above java program has been executed successfully.


10.Develop a web page using JSP

AIM:

To develop a web page using JSP.

PROCEDURE:

Step1. Open Eclipse, Click on New → Dynamic Web Project.


Step2. Give a name to your project and click on OK.
Step3. You will see a new project created in Project Explorer.
Step4. To create a new JSP file right click on Web Content directory, New → JSP
file.
Step5. Give a name to your JSP file and click Finish.
CODING:

Register_1.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Registration Form</title>
</head>
<body>
<h1>Guru Register Form</h1>
<form action="guru_register" method="post">
<table style="with: 50%"><tr><td>First Name</td>
<td><input type="text" name="first_name"/></td>
</tr>
<tr><td>Last Name</td><td><input type="text" name="last_name" /></td>
</tr><tr>
<td>UserName</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Contact No</td>
<td><input type="text" name="contact" /></td>
</tr></table>
<input type="submit" value="Submit" /></form>
</body>
</html>
Guru_register.java

package demotest;

import java.io.IOException;

import javax.servlet.RequestDispatcher;import
javax.servlet.ServletException; import
javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class guru_register
*/
public class guru_register extends HttpServlet { private static final long
serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
// TODO Auto-generated method stubString
first_name =
request.getParameter("first_name");
String last_name = request.getParameter("last_name");
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
String contact = request.getParameter("contact");

if(first_name.isEmpty() || last_name.isEmpty()
|| username.isEmpty() ||
password.isEmpty() ||
address.isEmpty() || contact.isEmpty())
{
RequestDispatcher req =
request.getRequestDispatcher("register_1.jsp");
req.include(request, response);
}
else
{
RequestDispatcher req =
request.getRequestDispatcher("register_2.jsp");
req.forward(request, response);
}
}

}
Register_2.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<title>Guru Success Page</title>
</head>
<body>
<a><b>Welcome User!!!!</b></a>
</body>
</html>
OUTPUT:

RESULT:

Thus the above java program has been executed successfully.

You might also like