BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Practical-1
AIM: Create a simple calculator application using Swing in Java.
CODE:
package parv;
import [Link].*;
import [Link].*;
import [Link].*;
public class Practical_1 extends JFrame implements ActionListener{
static JFrame f;
// create a textfield
static JTextField l;
// store operator and operands
String s0, s1, s2;
// default constructor
Practical_1()
{
s0 = s1 = s2 = "";
}
public static void main(String args[])
{
f = new JFrame("calculator");
try {
// set look and feel
[Link]([Link]());
}
[1]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
catch (Exception e) {
[Link]([Link]());
}
// create a object of class
Practical_1 c = new Practical_1();
// create a textfield
l = new JTextField(16);
// set the textfield to non editable
[Link](false);
// create number buttons and some operators
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
// create number buttons
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
// equals button
beq1 = new JButton("=");
[2]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
// create operator buttons
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
// add action listeners
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[3]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
[Link](c);
[Link](c);
// add elements to panel
[Link](l);
[Link](ba);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](bs);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](bm);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bd);
[Link](be);
[Link](b0);
[Link](beq);
[Link](beq1);
// set Background of panel
[Link]([Link]);
// add panel to frame
[Link](p);
[Link](200, 220);
[4]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
[Link]();
}
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
// if the value is a number
if (([Link](0) >= '0' && [Link](0) <= '9') || [Link](0) == '.') {
// if operand is present then add to second no
if ()
s2 = s2 + s;
else
s0 = s0 + s;
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == '=') {
double te;
// store the value in 1st
if ([Link]("+"))
[5]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
te = ([Link](s0) / [Link](s2));
else
te = ([Link](s0) * [Link](s2));
// set the value of text
[Link](s0 + s1 + s2 + "=" + te);
// convert it to string
s0 = [Link](te);
s1 = s2 = "";
}
else {
// if there was no operand
if ([Link]("") || [Link](""))
s1 = s;
// else evaluate
else {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
[6]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
te = ([Link](s0) / [Link](s2));
else
te = ([Link](s0) * [Link](s2));
// convert it to string
s0 = [Link](te);
// place the operator
s1 = s;
// make the operand blank
s2 = "";
}
// set the value of text
[Link](s0 + s1 + s2);
}
}
}
Output:
[7]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Practical-2
AIM: Implement Student information system using JDBC Simple and Prepared
Statement.
Simple Statement:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package parv;
import [Link].*;
/**
*
* @author Parv Patel
*/
public class simple_statement {
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/student_info","root","1234");
Statement st = [Link]();
String str="select * from student";
ResultSet ps=[Link](str);
while([Link]())
{
[8]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
String sname=[Link](1);
int id = [Link](2);
int age = [Link](3);
[Link](sname+"\t");
[Link](id+"\t");
[Link](age+"\t");
[Link]();
}
[Link]();
}
catch(Exception e)
{
}
}
}
OUTPUT:
[9]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Prepare_Statement:
package parv;
import [Link].*;
/**
*
* @author Parv Patel
*/
public class prepared_statement {
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/student_info","root","1234");
Statement sp = [Link]();
String str="insert into student values(?,?,?)";
PreparedStatement ps = [Link](str);
[Link](1, "Hetul");
[Link](2, 04);
[Link](3,17);
int i=[Link]();
[Link]("No. of Row Updated :"+i);
[Link]();
[10]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
Output:
[11]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Practical-3
AIM: Create a Chat application using TCP protocol.
TCP Server:
package server_tcp.java;
import [Link].*;
import [Link].*;
public class Server_TCPJava {
public static void main(String[] args) {
try
{
ServerSocket ss = new ServerSocket(1545);
Socket s = [Link]();
DataInputStream dis = new DataInputStream([Link]());
String str = (String)[Link]();
[Link]("Message :"+str);
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
[12]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
TCP Client:
package client_tcp;
import [Link].*;
import [Link].*;
/**
*
* @author Parv Patel
*/
public class Client_TCP {
public static void main(String[] args) {
try{
Socket s = new Socket("localhost",1545);
DataOutputStream dout = new DataOutputStream([Link]());
[Link]("Hello Server");
} catch(Exception e)
{
{[Link](e);
}
}
}
}
[13]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Output:
[14]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Practical-4
AIM: Create a Chat application using UDP protocol.
UDP Server:
package server_udp;
import [Link].*;
public class Server_UDP {
public static void main(String[] args) throws Exception{
DatagramSocket ds = new DatagramSocket(1545);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
[Link](dp);
String str = new String ([Link](), 0,[Link]());
[Link](str);
[Link]();
}
}
Client UDP:
package client_udp;
import [Link].*;
public class Client_UDP {
public static void main(String[] args) throws Exception{
DatagramSocket ds= new DatagramSocket();
String str = "Message sent by Datagramsocket";
InetAddress ip = [Link]("[Link]");
DatagramPacket dp = new DatagramPacket
([Link](),[Link](),ip,1545);
[15]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
[Link](dp);
[Link]();
}
}
OUTPUT:
[16]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
Practical-7
AIM: Write a servlet that counts the number of times that web page is visited
and displays the same information on that page using cookie.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class cookie_demo extends HttpServlet {
static int i=1;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String str = [Link](i);
Cookie c = new Cookie("parv",str);
[Link](c);
int j =[Link]([Link]());
if(j==1)
{
[Link]("<h1> Welcome to LDRP</h1>");
}
else
{
[Link]("No of Visit: "+ i +" Times");
}
i++;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
[17]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]
throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Output:
[18]
IT Dept,LDRP-ITR [PATEL PARV K.]