0% found this document useful (0 votes)
3 views18 pages

main_java

The document outlines several practical assignments for a 5th-semester Advanced Java Programming course, including creating a simple calculator application using Swing, implementing a student information system using JDBC, and developing chat applications using TCP and UDP protocols. Additionally, it describes a servlet that counts webpage visits using cookies. Each practical includes code examples and expected outputs.

Uploaded by

pateldivy875
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)
3 views18 pages

main_java

The document outlines several practical assignments for a 5th-semester Advanced Java Programming course, including creating a simple calculator application using Swing, implementing a student information system using JDBC, and developing chat applications using TCP and UDP protocols. Additionally, it describes a servlet that counts webpage visits using cookies. Each practical includes code examples and expected outputs.

Uploaded by

pateldivy875
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/ 18

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 javax.swing.*;
import java.awt.event.*;
import java.awt.*;
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
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}

[1]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

catch (Exception e) {
System.err.println(e.getMessage());
}

// create a object of class


Practical_1 c = new Practical_1();

// create a textfield
l = new JTextField(16);

// set the textfield to non editable


l.setEditable(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


bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);
b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);
b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);

[3]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

beq.addActionListener(c);
beq1.addActionListener(c);

// add elements to panel


p.add(l);
p.add(ba);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(bs);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bm);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(bd);
p.add(be);
p.add(b0);
p.add(beq);
p.add(beq1);

// set Background of panel


p.setBackground(Color.blue);

// add panel to frame


f.add(p);

f.setSize(200, 220);

[4]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();

// if the value is a number


if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {
// if operand is present then add to second no
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == '=') {

double te;

// store the value in 1st


if (s1.equals("+"))

[5]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// set the value of text


l.setText(s0 + s1 + s2 + "=" + te);

// convert it to string
s0 = Double.toString(te);

s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;

// store the value in 1st


if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))

[6]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
// convert it to string
s0 = Double.toString(te);
// place the operator
s1 = s;

// make the operand blank


s2 = "";
}
// set the value of text
l.setText(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 java.sql.*;
/**
*
* @author Parv Patel
*/
public class simple_statement {
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info","root","1234");

Statement st = con.createStatement();
String str="select * from student";

ResultSet ps=st.executeQuery(str);
while(ps.next())
{

[8]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

String sname=ps.getString(1);
int id = ps.getInt(2);
int age = ps.getInt(3);

System.out.print(sname+"\t");
System.out.print(id+"\t");
System.out.print(age+"\t");
System.out.println();
}
con.close();
}
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 java.sql.*;
/**
*
* @author Parv Patel
*/
public class prepared_statement {
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info","root","1234");
Statement sp = con.createStatement();

String str="insert into student values(?,?,?)";

PreparedStatement ps = con.prepareStatement(str);

ps.setString(1, "Hetul");
ps.setInt(2, 04);
ps.setInt(3,17);

int i=ps.executeUpdate();

System.out.println("No. of Row Updated :"+i);

ps.close();

[10]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

con.close();
}

catch(Exception e)
{
System.out.println(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 java.net.*;
import java.io.*;
public class Server_TCPJava {

public static void main(String[] args) {


try
{
ServerSocket ss = new ServerSocket(1545);
Socket s = ss.accept();

DataInputStream dis = new DataInputStream(s.getInputStream());


String str = (String)dis.readUTF();
System.out.println("Message :"+str);
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

[12]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

TCP Client:

package client_tcp;
import java.net.*;
import java.io.*;
/**
*
* @author Parv Patel
*/
public class Client_TCP {

public static void main(String[] args) {


try{

Socket s = new Socket("localhost",1545);


DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
} catch(Exception e)
{
{System.out.println(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 java.net.*;

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);
ds.receive(dp);
String str = new String (dp.getData(), 0,dp.getLength());
System.out.println(str);
ds.close();
}
}

Client UDP:
package client_udp;
import java.net.*;

public class Client_UDP {


public static void main(String[] args) throws Exception{
DatagramSocket ds= new DatagramSocket();
String str = "Message sent by Datagramsocket";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket
(str.getBytes(),str.length(),ip,1545);

[15]
IT Dept,LDRP-ITR [PATEL PARV K.]
BE 5th Sem[223SBEIT30016] Adavance Java Programming[CT506A-N]

ds.send(dp);
ds.close();
}
}

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 java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

public class cookie_demo extends HttpServlet {

static int i=1;


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String str = String.valueOf(i);
Cookie c = new Cookie("parv",str);
response.addCookie(c);
int j =Integer.parseInt(c.getValue());

if(j==1)
{
out.println("<h1> Welcome to LDRP</h1>");
}
else
{
out.println("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.]

You might also like