[Link]
in/
Create a simple TCP chat application where client and server can chat
with each other.
/*
[Link]
*/
import [Link].*;
import [Link].*;
class clientServerChatApplicationClientSideProgramming
{
public static void main(String args[ ])
throws Exception
{
Socket s = new Socket("localhost", 888);
DataOutputStream dos = new
DataOutputStream([Link]());
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]()));
BufferedReader kb = new BufferedReader(new
InputStreamReader([Link]));
String str,str1;
while(!(str = [Link]()).equals("stop"))
{
[Link](str+"\n");
str1 = [Link]();
[Link](str1);
}
[Link]();
[Link]();
[Link]();
[Link]();
}
}
/*
[Link]
*/
import [Link].*;
import [Link].*;
class [Link]
{
public static void main(String args[ ])
throws Exception
[Link]
{
ServerSocket ss = new ServerSocket(888);
Socket s = [Link]();
[Link]("Success!! Connection Established!!");
PrintStream ps = new PrintStream([Link]());
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]()));
BufferedReader kb = new BufferedReader(new
InputStreamReader([Link]));
while(true)
{
String str,str1;
while((str = [Link]()) != null)
{
[Link](str);
str1 = [Link]();
[Link](str1);
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link](0);
}
}
}
Create login form which contains a userid, password field, and two
buttons, Submit and Reset. If the userid or password field is left
blank, then on click of submit button, show a message to the user to
fill in the fields. On click of reset button, clear the fields.
import [Link].*;
import [Link].*;
import [Link].*;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT,RESET;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
[Link]("Username:");
[Link]
text1 = new JTextField(15);
label2 = new JLabel();
[Link]("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
RESET=new JButton("RESET");
panel=new JPanel(new GridLayout(3,1));
[Link](label1);
[Link](text1);
[Link](label2);
[Link](text2);
[Link](SUBMIT);
[Link](RESET);
add(panel,[Link]);
[Link](this);
[Link](this);
setTitle("LOGIN FORM by KAUSHAL");
}
public void actionPerformed(ActionEvent ae)
{
JButton b = (JButton) [Link]();
if (b == SUBMIT)
{
String value1=[Link]();
String value2=[Link]();
if ([Link]("") || [Link](""))
{
[Link](this,"username and password
must not blank",
"Error",JOptionPane.ERROR_MESSAGE);}
else
{
[Link](this,"welcome"+" "+value1);
}
}
else
{
[Link]("");
[Link]("");
}
}
}
class pract3
{
public static void main(String arg[])
{
[Link]
try
{
Login frame=new Login();
[Link](300,100);
[Link](true);
}
catch(Exception e)
{
[Link](null, [Link]());}
}
}
Create a client/server application where the client requests for a
particular file on the server. If the file exists on the server, then write
the contents of the file to the client.
/*
[Link]
*/
import [Link].*;
import [Link].*;
class clientServerApplicationFileRetriveServerSideProgramming
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
Socket s = [Link]();
[Link]("Success!! Connection Established!!");
BufferedReader cbf = new BufferedReader(new
InputStreamReader([Link]()));
String FileName = [Link]();
DataOutputStream dos = new
DataOutputStream([Link]());
FileReader fr = null;
BufferedReader filebr = null;
File fileNew = new File(FileName);
if([Link]())
{
[Link]("Find"+"\n");
fr = new FileReader(FileName);
filebr = new BufferedReader(fr);
String str;
while( (str = [Link]()) != null)
{
[Link]
[Link](str+"\n");
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
else
{
[Link]("Oops!! File Not Found!!"+"\n");
}
}
}
/*
[Link]
*/
import [Link].*;
import [Link].*;
class clientServerApplicationFileRetriveClientSideProgramming
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost",8888);
BufferedReader cbf = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter Filename : ");
String FileName = [Link]();
DataOutputStream dos = new
DataOutputStream([Link]());
[Link](FileName+"\n");
BufferedReader sbf = new BufferedReader(new
InputStreamReader([Link]()));
String str;
str = [Link]();
if([Link]("Find"))
{
while((str = [Link]()) != null)
{
[Link](str);
}
[Link]();
[Link]();
[Link]
[Link]();
[Link]();
}
}
}
Write an AWT program to create checkboxes for different courses
belongs to a university such that the courses selected would be
displayed.
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
/*
<applet code="pract1" width=200 height=200>
</applet>
*/
public class pract1 extends Applet implements ItemListener,ActionListener
{
Button b1;
Checkbox IT = null;
Checkbox CE = null;
Checkbox mech = null;
Checkbox EE = null;
Checkbox EC = null;
Checkbox civil = null;
public void init()
{
//create checkboxes
IT = new Checkbox("IT");
CE = new Checkbox("CE");
mech = new Checkbox("mech");
EE = new Checkbox("EE");
EC = new Checkbox("EC");
civil = new Checkbox("Civil");
b1 = new Button("Output");
add(IT);
add(CE);
add(mech);
add(EE);
add(EC);
add(civil);
add(b1);
//add item listeners
[Link]
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
if ([Link]() == b1)
{
repaint();
}
}
public void paint(Graphics g)
{
int i=10;
int k=60;
if([Link]()==true)
{
[Link]("IT",10,k+=20);
}
if([Link]()==true)
{
[Link]("CE",10,k+=20);
}
if([Link]()==true)
{
[Link]("mech",10,k+=20);
}
if([Link]()==true)
{
[Link]("EE",10,k+=20);
}
if([Link]()==true)
{
[Link]("EC",10,k+=20);
}
if([Link]()==true)
{
[Link]("civil",10,k+=20);
}
}
public void itemStateChanged(ItemEvent ie)
{
[Link]
//repaint();
}
}
Create a list of vegetables. If you click on one of the items of the list,
the item should be displayed in a Textbox
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
/*
<applet code="pract2" width=200 height=200>
</applet>
*/
public class pract2 extends Applet implements ItemListener
{
List list = null;
TextField a;
public void init()
{
a = new TextField( 70);
//create a multi select list
list = new List(5, true);
//add items to a list
[Link]("potatoes,");
[Link]("Brinjal,");
[Link]("Cabbage,");
[Link]("Carrot,");
[Link]("Onion,");
[Link]("Mint,");
[Link]("potato,");
//add list
add(list);
add(a);
//add listener
[Link](this);
}
public void paint(Graphics g)
{
String[] items = [Link]();
String msg = "";
for(int i=0; i < [Link]; i++)
{
msg = items[i] + " " + msg;
[Link]
}
[Link]("vegetables:"+ msg);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
}
Write JDBC program to insert the detail of college student in the MS-
Access database.
/**
/* database connectivity with MS-Access is done by creating
DataSourceName(dsn) in this example*/
/* Steps to use this example:
* go to ms-access and make a database called "student_base" and create
table named student_base.mdb
* 1. Go to Control Panel
2. Click on Administrative Tools(windows 2000/xp), Click on
ODBC(win98)
OR if u have (windows 7) than go to C:\Windows\SysWOW64\[Link]
3. click on ODBC
4. Then , you will see a ODBC dialog box. Click on UserDSn
5. Click on Add Button
6. Select Microsoft Access Driver(*.mdb) driver and click on finish
7. Give a Data Source Name : student_base
8. Then Click on Select
9. Browse on the database [Link] file on your disk by downloading it
link provided..
will be stored
10. Click on OK.
Once the DSN is created, you can do this example*/
//Java Core Package
import [Link].*;
//Java Extension Package
import [Link].*;
import [Link].*;
import [Link].*;
public class addItemToDatabase extends JFrame
{
//Initializing Components
private JTextField inputs[];
private JButton add, reset;
private JLabel labels[];
private String fldLabel[] = {"First Name: ","Last Name: ","Branch","Enroll-no "};
[Link]
private JPanel p1;
Connection con;
Statement st;
ResultSet rs;
String db;
//Setting up GUI
public addItemToDatabase()
{
//Setting up the Title of the Window
super("Adding Data to the Database");
//Set Size of the Window (WIDTH, HEIGHT)
setSize(300,180);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Constructing Components
inputs = new JTextField[4];
labels = new JLabel[4];
add = new JButton("Add");
reset = new JButton("Reset");
p1 = new JPanel();
//Setting Layout on JPanel 1 with 5 rows and 2 column
[Link](new GridLayout(5,2));
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
//Setting up the container layout
GridLayout grid = new GridLayout(1,1,0,0);
[Link](grid);
//Creating a connection to MS Access and fetching errors using "try-catch" to
check if it is successfully connected or not.
try
{
[Link]("[Link]");
//db = "jdbc:odbc:Driver={Microsoft Access Driver
(*.mdb)};DBQ=[Link];";
con =
[Link]("jdbc:odbc:student_base");
st = [Link]();
[Link](null,"Successfully
Connected to Database","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception e)
{
[Link](null,"Failed to
Connect to Database","Error Connection", JOptionPane.ERROR_MESSAGE);
[Link](0);
[Link]
}
//Constructing JLabel and JTextField using "for loop" in their desired order
for(int count=0; count<[Link] && count<[Link]; count++) {
labels[count] = new JLabel(fldLabel[count]);
inputs[count] = new JTextField(20);
//Adding the JLabel and the JTextFied in JPanel 1
[Link](labels[count]);
[Link](inputs[count]);
}
//Implemeting Even-Listener on JButton add
[Link](new ActionListener()
{
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event)
{
if (inputs[0].getText().equals("") ||
inputs[1].getText().equals("") || inputs[2].getText().equals("") || inputs[0].getText() == null
|| inputs[1].getText() == null || inputs[2].getText() == null)
[Link](null,"Fill
up all the Fields","Error Input", JOptionPane.ERROR_MESSAGE);
else
try
{
String add = "insert into student
(firstName,LastName,Branch,Enroll_no) values
('"+inputs[0].getText()+"','"+inputs[1].getText()+"','"+inputs[2].getText()+"',"+inputs[3].get
Text()+")";
[Link](add); //Execute the add sql
[Link](inputs[3].getText());
//Convert JTextField Age in to INTEGER
[Link](null,"Item
Successfully Added","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}
catch (NumberFormatException e)
{
[Link](null,"Please enter an integer on the Field
Enroll-no","Error Input", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ei)
{
[Link](null,"Failure to Add Item. Please Enter a
number on the Field Enroll-no","Error Input", JOptionPane.ERROR_MESSAGE);
}
[Link]
}
}
);
//Implemeting Even-Listener on JButton reset
[Link](new ActionListener()
{
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
inputs[0].setText(null);
inputs[1].setText(null);
inputs[2].setText(null);
inputs[3].setText(null);
}
}
);
//Adding JButton "add" and "reset" to JPanel 1 after the JLabel and
JTextField
[Link](add);
[Link](reset);
//Adding JPanel 1 to the container
[Link](p1);
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}
//Main Method
public static void main (String[] args) {
addItemToDatabase aid = new addItemToDatabase();
}
}
Create a split pane which divides the frame into two parts. The first
part possesses a list and on selecting an item in a list, the item
should be displayed in the other portion.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class pract4
{
public static void main(String[] args)
{
JFrame frame = new SplitPaneFrame();
[Link]
[Link]();
}
}
class SplitPaneFrame extends JFrame implements ListSelectionListener
{
public SplitPaneFrame()
{
setSize(400, 300);
list = new JList(texts);
[Link](this);
description = new JTextArea();
JSplitPane innerPane= new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list, description);
getContentPane().add(innerPane, "Center");
}
public void valueChanged(ListSelectionEvent event)
{
JList source = (JList)[Link]();
Display value = (Display)[Link]();
[Link]([Link]());
}
private JList list;
private JTextArea description;
private Display[] texts =
{
new Display("Text1", "This is text1."),
new Display("Text2", "This is text2."),
new Display("Text3", "This is text3."),
new Display("Text4", "This is text4.")
};
}
class Display
{
public Display(String n, String t)
{
name = n;
des = t;
}
public String toString()
{
return name;
}
public String getDescription()
{
return des;
}
[Link]
private String name;
private String des;
}