0% found this document useful (0 votes)
10 views

Index Prog-advJava1

The document contains 15 programming problems related to Java. The problems cover topics like multithreading, applets, JDBC, JSP and networking. Students are required to write Java programs to solve each problem.

Uploaded by

anuragnair420
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Index Prog-advJava1

The document contains 15 programming problems related to Java. The problems cover topics like multithreading, applets, JDBC, JSP and networking. Students are required to write Java programs to solve each problem.

Uploaded by

anuragnair420
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TYBBA (CA) _ Advance Java_603

INDEX

Sr. No Name Remark

1 Write a java program to scroll the text from left to right and vice versa
continuously.

2 Write a java program in multithreading using applet for drawing flag.

3 Write a java program using applet for bouncing ball, for each bounce color of
ball should change randomly.

4 Write a Java Program to delete details of students whose initial character of


their name is ‘S’.
5 Write a java program in multithreading using applet for Traffic signal.

6 Write a Multithreading program in java to display the number’s between 1 to


100 continuously in a Text Field by clicking on button.
7 Write a JDBC Program in java to display the names of Employees starting with
‘S’ character.
8 Write a Java Program to create an Emp (ENo, EName, and Sal) table and insert
record into it. (Use PreparedStatement Interface)
9 Write a java program to count the number of records in a table.

10 Write a Multithreading program in java to display all the alphabets from A to Z


after 3 seconds.
11 Write a java program for simple standalone chatting application.
(using AWT)

12 Write a Socket program in java for simple stand-alone chatting application

13 Write a JSP program to create an online shopping mall. User must be allowed
to do purchase from two pages. Each page should have a page total. The third
page should display a bill, which consists of a page total of whatever the
purchase has been done and print the total. (Use Session)
14 Write a java program to create a student table with field’s rno, name and per.
Insert values in the table. Display all the details of the student on screen.
15 Write a JSP program which accept UserName in a TextBox and greets the user
according to the time on server machine.
Solved Programs

 Write a java program to scroll the text from <APPLET CODE=ScrollingText.class


left to right and vice versa continuously. WIDTH=400 HEIGHT=200 ></APPLET>
import java.applet.Applet; </body>
import java.awt.*;
</html>
import java.awt.event.*;

public class ScrollingText extends Applet  Write a java program in multithreading


implements Runnable using applet for drawing flag.
{
String msg="Welcome to Java Programming import java.awt.*;
Language ....... "; import java.applet.*;
Thread t=null; public class India_Flag extends Applet
public void init() {
{ public void paint(Graphics g)
setBackground(Color.cyan); {
setForeground(Color.red); g.setColor(Color.blue);
t=new Thread(this); g.fillRect(50,20,5,300);
t.start(); g.setColor(Color.black);
} g.drawRect(50,18,3,300);
public void run() g.setColor(Color.orange);
{ g.fillRect(55,20,120,30);
char ch; g.setColor(Color.black);
for(; ;) g.drawRect(55,20,118,28);
{ g.setColor(Color.green);
try g.fillRect(55,80,119,30);
{ g.setColor(Color.black);
repaint(); g.drawRect(55,80,117,28);
Thread.sleep(400); g.setColor(Color.black);
ch=msg.charAt(0); g.drawOval(100,50,30,30);
}
msg=msg.substring(1,msg.length()); }
msg+=ch; <html><head></head><body>
} <applet code = "India_Flag.class" width = "320"
catch(InterruptedException e) height = "480"></applet>
{} } } </body>
public void paint(Graphics g) </html>
{
g.drawString(msg,10,10);
} }

ScrollingText.html
<html>
<head>
</head>
<body>
 Write a java program using applet for if(f1==4)
bouncing ball, for each bounce color of ball g.setColor(Color.yellow);
should change randomly. if(f1==5)
g.setColor(Color.orange);
g.fillOval(150,y+10,20,20);
import java.io.*; if(y==400)
import java.awt.*; {
f1++; f=1;
/*<applet code="BounsingBall.class" height=400 }
width=350></applet>*/ }
public class BounsingBall extends if(f==1) {
java.applet.Applet implements Runnable if(f1==1)
{ g.setColor(Color.green);
Thread t; if(f1==2)
int f,y,f1,f2,f3; g.setColor(Color.blue);
public void init() if(f1==3)
{ g.setColor(Color.red);
t=new Thread(this); if(f1==4)
t.start(); g.setColor(Color.yellow);
f=0; y=0; f1=0; if(f1==5)
} g.setColor(Color.orange);
public void run() g.fillOval(150,y-10,20,20);
{ try{ if(y==0)
if (f==0){ t.sleep(10); {
y=y+5; f1++; f=0;
repaint(); }
if(f1==6) } }
f1=0; }
}
if(f==1) { t.sleep(10);
y=y-5;
repaint();
if(f1==6)
f1=0;
}
}catch(Exception e){ }
run();
}
public void paint(Graphics g)
{
if(f==0) {
if(f1==1)
g.setColor(Color.green);
if(f1==2)
g.setColor(Color.blue);
if(f1==3)
g.setColor(Color.red);
 Write a Java Program to delete details of }
students whose initial character of their name }
is ‘S’. catch(Exception e)
{
import java.io.*; System.out.println(e);
import java.sql.*; }
public class Employee }
{ }
public static void main(String args[]) ******************************************
{
try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:BC
A");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select *
from Employee");
System.out.println(" EID "+" Ename "+"
Address "+" Salary ");
while(rs.next())
{
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getInt(4)+"\t");
System.out.print("\n");
}
String s="delete from Employee where
Ename like 'S%'";
int n=stmt.executeUpdate(s);
if(n>0)
{
System.out.println("Record is
Deleted...!!!");
}
else {
System.out.println("Record is not found
in table...!!!");
}
ResultSet rs1=stmt.executeQuery("Select *
from Employee");
System.out.println(" EID "+" Ename "+"
Address "+" Salary ");
while(rs1.next())
{
System.out.print(rs1.getInt(1)+"\t");
System.out.print(rs1.getString(2)+"\t");
System.out.print(rs1.getString(3)+"\t");
System.out.print(rs1.getInt(4)+"\t");
System.out.print("\n");
 Write a java program in multithreading g.fillOval(100,100,100,100);
using applet for Traffic signal. g.drawOval(100,100,100,100);
import java.awt.*; r=0; }
import java.io.*; if (g1==1)
import java.applet.*; { g.setColor(Color.green);
g.fillOval(100,225,100,100);
public class slip3_2 extends java.applet.Applet g1=0;
implements Runnable g.drawOval(100,225,100,100);
}
{ int r,g1,y,i; if (y==1) {
g.setColor(Color.yellow);
Thread t; g.fillOval(100,350,100,100);
public void init() y=0;
{ g.drawOval(100,350,100,100);
r=0;g1=0;y=0; } }}
t=new Thread(this); <html>
t.start(); <head>
} <body>
public void run() <applet code= "slip3_2.class" height="600"
{ try width="600"></applet>
{
for (i=24;i>=1;i--) public void paint(Graphics g)
{ {
t.sleep(100); g.drawOval(100,100,100,100);
if (i>=16 && i<24) g.drawOval(100,225,100,100);
{ g.drawOval(100,350,100,100);
g1=1; if (r==1) {
repaint(); g.setColor(Color.red);
} g.fillOval(100,100,100,100);
if (i>=8 && i<16) g.drawOval(100,100,100,100);
{ r=0;
y=1; }
repaint(); if (g1==1)
} {
if (i>=1 && i<8) g.setColor(Color.green);
{ g.fillOval(100,225,100,100);
r=1; g1=0;
repaint(); g.drawOval(100,225,100,100);
} }
}
if (i==0) if (y==1)
{
run(); {
} g.setColor(Color.yellow);
}catch(Exception e) {} } g.fillOval(100,350,100,100);
public void paint(Graphics g) y=0;
{ g.drawOval(100,350,100,100);
g.drawOval(100,100,100,100); }
g.drawOval(100,225,100,100); }
g.drawOval(100,350,100,100); </body>
if (r==1) { </html>
g.setColor(Color.red); *****************************************
 Write a Multithreading program in java to {
display the number’s between 1 to 100
new Mythread();
continuously in a Text Field by clicking on
button. }
import javax.swing.*; }
import java.awt.*; class Mythread extends Thread
import java.awt.event.ActionListener; {
import java.awt.event.ActionEvent; Mythread()
public class MultiThread extends JFrame {
implements ActionListener
start();
{
}
Container cc;
public void run()
JButton b1,b2;
{
JTextField t1;
for(int i=1;i<=100;i++)
MultiThread()
{
{
try {
setVisible(true);
Thread.sleep(1000);
setSize(1024,768);
}
cc=getContentPane();
catch (InterruptedException e) {
setLayout(null);
}
t1=new JTextField(500);
t1.setText(t1.getText()+""+i+"\n");
cc.add(t1);
//System.out.println() }
t1.setBounds(10,10,1000,30);
}
b1=new JButton("start");
}
cc.add(b1);
public static void main(String arg[])
b1.setBounds(20,50,100,40);
{
b1.addActionListener(this);
new MultiThread().show();
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
*****************************************
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
 Write a JDBC Program in java to display the public static void main(String args[])
names of Employees starting with ‘S’ { try { int eno,sal;
character. String name;
BufferedReader br=new
import java.sql.*; BufferedReader(new
public class slip21_1
InputStreamReader(System.in));
{
public static void main(String args[ ]) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
{ cn=DriverManager.getConnection("jdbc:odbc:emp
Connection con; ","","");
try{ String str="create table Emp(eno number,name
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); varchar(20),sal number)";
con=DriverManager.getConnection("jdbc:odbc:m ps=cn.prepareStatement(str);
ydsn2"); ps.executeUpdate();
if(con==null){ System.out.println("\nEmployee Table Created");
System.out.println("Connection Failed"); System.out.println("\n How many Records you
System.exit(1);} want to insert?
System.out.println("Connection Established"); int n=Integer.parseInt(br.readLine());
Statement stmt=con.createStatement( );
System.out.println("\nEnter "+n+" Records \n");
ResultSet rs=stmt.executeQuery("select * From
for(int i=0;i<n;i++) {
employee where ename like 'S%' ");
System.out.println("eno\t"+"ename\t"+"department System.out.println("\nEnter Emp No.");
\t"+"sal\t"); eno=Integer.parseInt(br.readLine());
while(rs.next( )) System.out.println("Enter Employee name");
{ name=br.readLine();
System.out.println("\n"+rs.getInt(1)+"\t"+rs.getStri System.out.println("Enter sal");
ng(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4)); sal=Integer.parseInt(br.readLine());
} ps=cn.prepareStatement("insert into Emp
con.close( ); values(?,?,?)");
rs.close( ); ps.setInt(1,eno);
stmt.close( ); ps.setString(2,name);
} ps.setInt(3,sal);
catch(Exception e)
ps.executeUpdate();
{
}
System.out.println(e);
} System.out.println("\nAll Records are: \n");
} st=cn.createStatement();
} rs=st.executeQuery("select * from Emp");
*************************************** System.out.println("eno \t name \t sal.");
while(rs.next()) {
 Write a Java Program to create an Emp
(ENo, EName, and Sal) table and insert System.out.println(rs.getInt("eno")+"\t"+rs.getStrin
record into it. (Use PreparedStatement g ("name")+"\t"+rs.getInt("sal")); }
Interface) cn.close();
import java.io.*; }catch(Exception e)
import java.sql.*; { System.out.println(e);
public class CreateEmpTable } }}
{ static Connection cn; Note: - In this program the table created only
static Statement st; once. After that it will gives msg that “Table is
static PreparedStatement ps; created”
static ResultSet rs; *****************************************
 Write a java program to count the number of  Write a Multithreading program in java to
records in a table. display all the alphabets from A to Z after 3
seconds.
import java.io.*;
import java.sql.*; import java.io.*;
class slip10 import java.util.*;
{ class mythread implements Runnable{
public static void main(String args[ ]) Thread t;
{ public mythread(){
Connection con; t=new Thread(this);
t.start();}
try public void run()
{ {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); char ch;
con=DriverManager.getConnection("jdbc:odbc:em for(ch='a';ch<='z';ch++)
p"); {
if(con==null) System.out.println(ch);
{ try{
System.out.println("Connection failed"); Thread.sleep(3000);}
System.exit(1); catch(Exception e){}}}}
} class demo
System.out.println("Connection Established....."); {public static void main(String s[]){
Statement stmt=con.createStatement( ); mythread m=new mythread();}}
ResultSet rs=stmt.executeQuery("select * from
Emp"); output:-
int cnt=0; C:\Program Files\Java\jdk1.6.0_20\bin>javac
while(rs.next( )) demo.java
{
cnt++; C:\Program Files\Java\jdk1.6.0_20\bin>java demo
}
System.out.println("Number of Records are :- **********************************
"+cnt);

}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:-

C:\Program Files\Java\jdk1.6.0_20\bin>javac
slip10.java

C:\Program Files\Java\jdk1.6.0_20\bin>java slip10


Connection Established.....
Number of Records are:-4

***************************
 Write a java program for simple standalone msg=t1.getText();
chatting application. (using AWT) dos.writeUTF(msg);
t1.setText("");
import java.awt.*; String reply=(String)dis.readUTF();
import java.awt.event.*; l2.setText(reply);
import java.io.*; }
import java.net.*; catch(Exception e){}
import java.util.*; }}
class Awtcchat extends Frame implements public static void main(String a[]) throws
ActionListener { Exception {
TextField t1; Awtcchat ob=new Awtcchat();
}
Button b1; }
Socket s;
Scanner sc;
Server Program:
DataOutputStream dos;
DataInputStream dis; import java.awt.*;
String msg;
Label l1,l2; import java.awt.event.*;
Awtcchat() throws Exception import java.io.*;
import java.net.*;
{ import java.util.*;

setVisible(true); class Awtschat extends Frame implements


setSize(600,600); ActionListener {
t1=new TextField(50); TextField t1;
b1=new Button("send"); Button b1;
l1=new Label("Sever"); ServerSocket ss;
l2=new Label(); Socket s;
Scanner sc;
setLayout(new FlowLayout()); DataOutputStream dos;
add(l1); DataInputStream dis;
add(l2); String msg;
add(t1); Label l1,l2;
add(b1); Awtschat() throws Exception

b1.addActionListener(this); {

s=new Socket("localhost",5000); setVisible(true);


setSize(600,600);
sc=new Scanner(System.in); t1=new TextField(50);
b1=new Button("send");
dos=new DataOutputStream(s.getOutputStream()); l1=new Label("Client");
dis=new DataInputStream(s.getInputStream()); l2=new Label();
} setLayout(new FlowLayout());
public void actionPerformed(ActionEvent ae)
{ add(l1);
if(ae.getSource()==b1) add(l2);
{ add(t1);
try add(b1);
{ b1.addActionListener(this);
ss=new ServerSocket(5000);

sc=new Scanner(System.in);
s=ss.accept();
dos=new
DataOutputStream(s.getOutputStream()); dis=new
DataInputStream(s.getInputStream());
}public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{try
{String reply=(String)dis.readUTF();

l2.setText(reply);

msg=t1.getText();
dos.writeUTF(msg);
t1.setText("");
}catch(Exception e){}
}}
public static void main(String a[]) throws
Exception {
Awtschat ob=new Awtschat();
}
}
Output:

C:\Program Files\Java\jdk1.6.0_20\bin>javac
Awtschat.java
C:\Program Files\Java\jdk1.6.0_20\bin>java
Awtschat

C:\Program Files\Java\jdk1.6.0_20\bin>javac
Awtcchat.java
C:\Program Files\Java\jdk1.6.0_20\bin>java
Awtcchat

*********************************
 Write a Socket program in java for simple
stand-alone chatting application. {
s1 = (String)ios.readUTF();
import java.io.*; if(s1.equals("end") || s1.equals("END"))
import java.net.*; {
class C_Slip13_2 System.out.println("chatting terminated");
{
public static void main(String a[]) throws break;
Exception { }
Socket s = new Socket("localhost",1000); System.out.println("Client "+s1);
System.out.println("client is connected : "); System.out.println("Server ...");
DataInputStream ios=new s2 = br.readLine();
DataInputStream(s.getInputStream()); dos.writeUTF(s2);
DataOutputStream dos=new if(s2.equals("end") || s2.equals("END"))
DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new {
InputStreamReader(System.in)); System.out.println("chatting terminated");
String s1,s2; break;
while(true) }}}}
{ Output:
System.out.println("Server ...."); C:\Program Files\Java\jdk1.6.0_20\bin>javac
s1=br.readLine(); C_Slip13_2.java
dos.writeUTF(s1); C:\Program Files\Java\jdk1.6.0_20\bin>java
s2=(String)ios.readUTF(); C_Slip13_2
if(s2.equals("end") || s2.equals("END")) client is connected :
{ Server ....
System.out.println("chatting terminated"); hi
break; Client hi
}System.out.println("Client "+s2); Server ....
}}} how are u
Client i am fine
import java.io.*; Server ....
import java.net.*; whats going on
Client nothing
class S_Slip13_2 Server ....
{ C:\Program Files\Java\jdk1.6.0_20\bin>javac
public static void main(String a[]) throws S_Slip13_2.java
Exception {
ServerSocket ss = new ServerSocket(1000); C:\Program Files\Java\jdk1.6.0_20\bin>java
S_Slip13_2
System.out.println("Server is waiting for client : "); Server is waiting for client :
Socket s =ss.accept(); Client is connected
System.out.println("Client is connected"); Client hi
DataInputStream ios=new Server ...
DataInputStream(s.getInputStream()); hi
DataOutputStream dos=new Client how are u
DataOutputStream(s.getOutputStream()); Server ...
i am fine
BufferedReader br = new BufferedReader(new Client whats going on
InputStreamReader(System.in)); Server ...
String s1,s2; Nothing
while(true) **************************************
 Write a JSP program to create an online
shopping mall. User must be allowed to Second.jsp
do purchase from two pages. Each page <%@page contentType="text/html"
should have a page total. The third page pageEncoding="UTF-8"%>
should display a bill, which consists of a <!DOCTYPE html>
page total of whatever the purchase has <%
been done and print the total. (Use String rice = request.getParameter("rice");
Session) int r = Integer.parseInt(rice);
String wheat = request.getParameter("wheat");
int w = Integer.parseInt(wheat);
First.jsp int tr = r * 40;
<%@page contentType="text/html"
int wt = w * 60;
pageEncoding="UTF-8"%>
int pt = tr + wt;
<!DOCTYPE html>
session.setAttribute("first_Total", pt);
<html> %>
<head> <html>
<meta http-equiv="Content-Type" <head>
content="text/html; charset=UTF-8"> <meta http-equiv="Content-Type"
<title>JSP Page</title> content="text/html; charset=UTF-8">
</head> <title>JSP Page</title>
<body> </head>
<form action=" Second.jsp" method="post"> <body>
<h1>Shopping Mall</h1>
<h1>Shopping Mall</h1>
Rice : <form action=" Third.jsp" method="post">
<select name="rice"> Maggi :
<option value="2">2 Kg</option> <select name="maggi">
<option value="5">5 Kg</option> <option value="1">1</option>
<option value="10">10 Kg</option> <option value="2">2</option>
<option value="15">15 Kg</option> <option value="3">3</option>
</select> <option value="4">4</option>
Wheat : <option value="5">5</option>
<select name="wheat"> </select>
<option value="2">2 Kg</option>
Good Day :
<option value="5">5 Kg</option>
<select name="good">
<option value="10">10 Kg</option>
<option value="1">1</option>
<option value="15">15 Kg</option> <option value="2">2</option>
</select> <option value="3">3</option>
<input type="submit" value="Submit"> <option value="4">4</option>
</form> <option value="5">5</option>
</body>
</select>
</html> <input type="submit" value="Submit">
</form>
</body>
</html>
Third.jsp
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<% String m = request.getParameter("maggi");
String g = request.getParameter("good");
int mp = Integer.parseInt(m);
int gp = Integer.parseInt(g);
int mpt = mp * 10;
int gpt = gp * 10;
int secont_total = mpt + gpt;
int first_Total =
(Integer)request.getSession().getAttribute("first_To
tal");
int grand_Total = first_Total + secont_total;
%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Shopping Mall</h1>
<h1>First Page Total
<%=first_Total%></h1><br>
<h1>Second Page Total
<%=secont_total%></h1><br>
<h1>Total Bill<%=grand_Total%></h1><br>
</body>
</html>
per=Integer.parseInt(br.readLine());
 Write a java program to create a student
table with field’s rno, name and per. ps=cn.prepareStatement("insert into Stud
Insert values in the table. Display all the values(?,?,?)");
details of the student on screen. ps.setInt(1,rno);
ps.setString(2,name);
import java.io.*; ps.setInt(3,per);
import java.sql.*; ps.executeUpdate();
public class CreateStudentTable }
{ System.out.println("\nAll
static Connection cn; Records are: \n");
static Statement st; st=cn.createStatement();
static PreparedStatement ps; rs=st.executeQuery("select *
static ResultSet rs; from Stud");
public static void main(String args[]) System.out.println("RollNo
{ \t Name \t Perc.");
try while(rs.next())
{int rno,per; {
String name;
BufferedReader br=new System.out.println(rs.getInt("rno")+"\t"+rs.getStrin
BufferedReader(new g("name")+"\t"+rs.getInt("per"));
InputStreamReader(System.in)); }
cn.close();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch(Exception e
cn=DriverManager.getConnection("jdbc:odbc:dsn1 {
","",""); System.out.println(e);
String str="create table }
Stud(rno number,name varchar(20),per number)"; }
ps=cn.prepareStatement(str); }
ps.executeUpdate();
*************************************
System.out.println("\nStudent Table Created");
System.out.println("\n How
many Records you want to insert?");
int
n=Integer.parseInt(br.readLine());
System.out.println("\nEnter
"+n+" Records \n");
for(int i=0;i<n;i++)
{

System.out.println("\nEnter Roll No.");

rno=Integer.parseInt(br.readLine());

System.out.println("Enter Student name");


name=br.readLine();

System.out.println("Enter per");
 Write a JSP program which accept
UserName in a TextBox and greets the
user according to the time on server
machine.

Greet.html
<html>
<body>
<form action=”Greet.jsp” method=”get”>
Username:<input type=”text” name=”uname”/>
</br>
<input type=”button” name=”submit”/>
</form>
</body>
</html>

Greet.jsp
<%@ page language=”java”
import=”java.util.*”%>
<html>
<body>
<% String
name=request.getParameter(“uname”);%>
<%=”Welcome”+name+”<br>”%>
<%=”Server date/time:”+new java.util.Date()%>
</body>
</html>
*****************************************

You might also like