Professional Documents
Culture Documents
Advance Java Final
Advance Java Final
Technology(B.Tech)
Class= CSE[BDA-IBM]
Section=E
Submitted by Submitted To
Miss.Kangana Soni Ms.Khushboo Mam
Index
Serial Date of Experiments Page Remarks
No. Experiment No.
1. Write an event handling program to create 4
three buttons; named first, second and third
place these on frame and create three text
boxes where “first” should appear in first
textbox when you click button named first,
“second” should appear in second textbox
when you click button named second, third
should appear in third textbox when you click
button named third.
2. Write an AWT GUI application (called AWT 6
Counter)as shown in figure: Each time the
“Count” button is clicked, the counter value
shall increase by 1.Theprogram has
Three components:
1. ajava. awt. Label “Counter”;
2. anon-edit able java. awt. Text-Field to
display the
Counter value ;and
3. ajava. awt. Button “Count”.
Experiment1
PROGRAM CODE:-
importjava.awt.Frame;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.TextField;
importjava.awt.Button;
publicclassButtonEventextends Frame implements ActionListener{
Button bt1,bt2,bt3;
TextField tx1,tx2,tx3;
ButtonEvent (){
tx1=newTextField();
tx1.setBounds(30, 30, 90, 30);
bt1=new Button(“First”);
bt1.setBounds(150,30,90, 30);
bt1.addActionListener(this);
tx2=newTextField();
tx2.setBounds(30, 60, 90, 30);
bt2=new Button(“second”);
ButtonEvent B=newButtonEvent();
B.addWindowListener(newWindowAdapter() {
publicvoidwindowClosing(WindowEvent we) {
System.exit(0);}});}}
OUTPUT:-
Experiment2
The components are placed inside the top-level AWT container java.
awt. Frame, arranged in Flow Layout.
PROGRAM CODE:-
packageAdvJava_Assignment;
importjava.awt.Frame;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.Label;
importjava.awt.TextField;
importjava.awt.Button;
importjava.awt.FlowLayout;
publicclassCounter_Exampleextends Frame implements ActionListener{
TextField tx1;
Counter_Example(){
Label l=new Label(“Counter”);
tx1=newTextField("0");
Button B=new Button(“Count”);
B.addActionListener(this);
add(l);
add(tx1);
add(B);
setTitle(“AWT Counter”);
tx1.setEditable(false);
setSize(600,400);
setLayout(newFlowLayout());
setVisible(true);}
@Override
publicvoidactionPerformed(ActionEvent e){
String s = tx1.getText();
inti = Integer.parseInt(s);
i=i+1;
String ans = String.valueOf(i);
tx1.setText(ans);}
publicstaticvoid main(String[] args) {
Counter_Example C= newCounter_Example();
C.addWindowListener(newWindowAdapter() {
publicvoidwindowClosing(WindowEvent we) {
System.exit(0);}});}}
OUTPUT:-
Experiment3
Objective:- Write a Swing application ( called Swing Arithmetics ) to include
buttons “+”,”-”,”*”,”/”,”%”(remainder) and “CLEAR ”as shown in figure:
The“+”button applies add operation on integers And display the result. The “-”
button applies subtraction operation on integers and display the result. The “*”
button applies multiplication operation on integers and display the result. The“/”
button applies division operation on integers and display the result. The “%” button
applies modulus operation on integers and display the result. The “CLEAR” button
shall clear all the text-fields.
PROGRAM CODE:-
packageAdvJava_Assignment;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JLabel;
importjavax.swing.JTextField;
importjavax.swing.JButton;
importjavax.swing.JFrame;
publicclassSwing_ArithmeticsextendsJFrameimplements
ActionListener { JTextField tx1,tx2,tx3;
JLabel l1,l2,l3;
JButton bt1,bt2,bt3,bt4,bt5,bt6;
Swing_Arithmetics (){
l1=newJLabel(“First Number”);
l1.setBounds(30,40,100,30);
l2=newJLabel(“Secound Number”);
l2.setBounds(30, 80, 100,30);
l3=newJLabel(“Result”);
l3.setBounds(30,120,100,30);
tx1=newJTextField();
tx1.setBounds(170, 40, 100, 30);
tx2=newJTextField();
tx2.setBounds(170, 80, 100, 30);
tx3 = newJTextField();
tx3.setBounds(170, 120, 100, 30);
tx3.setEditable(false);
bt1=newJButton(“+”);
bt1.setBounds(30, 170, 100, 30);
bt1.addActionListener(this);
bt2=newJButton(“-”);
bt2.setBounds(170, 170, 100, 30);
bt2.addActionListener(this);
bt3=newJButton(“*”);
bt3.setBounds(30, 210, 100, 30);
bt3.addActionListener(this);
bt4=newJButton(“/”);
bt4.setBounds(170, 210, 100, 30);
bt4.addActionListener(this);
bt5=newJButton(“%”);
bt5.setBounds(30, 250, 100, 30);
bt6=newJButton(“clear”);
bt5.addActionListener(this);
bt6.setBounds(170, 250, 100, 30);
bt6.addActionListener(this);
add(l1);
add(l2);
add(l3);
add(tx1);
add(tx2);
add(tx3);
add(bt1);
add(bt1);
add(bt2);
add(bt3);
add(bt4);
add(bt5);
add(bt6);
setTitle(“Swing Arithmetics”);
setSize(500,400);
setLayout(null);
setVisible(true);}
@Override
publicvoidactionPerformed(ActionEvent e) { String s1 =
tx1.getText();
String s2 = tx2.getText();
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
int i3;
if(e.getSource()==bt1) {
i3 = i1+i2;
String sum = String.valueOf(i3);
tx3.setText(sum);}
if(e.getSource()==bt2) { i3 = i1-i2;
String sub = String.valueOf(i3);
tx3.setText(sub);}
if(e.getSource()==bt3) { i3 = i1*i2;
String mul = String.valueOf(i3);
tx3.setText(mul);}
if(e.getSource()==bt4) {i3 = i1/i2;
String div = String.valueOf(i3);
tx3.setText(div);}
if(e.getSource()==bt5) {i3 = i1%i2;
String mod = String.valueOf(i3);
tx3.setText(mod);}
if(e.getSource()==bt6) {
tx1.setText( “ ” );
tx2.setText(“ ”);
tx3.setText(“ ”);}}
publicstaticvoid main(String[] args) {
Swing_ArithmeticsS= newSwing_Arithmetics();}}
OUTPUT:-
Experiment4
Experiment5
Objective:-Write a Servlet application to print the current date and time.
PROGRAM CODE:-
import java.io.*;
importjava.util.Date;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassDateTimeextendsGenericServlet{
publicstaticvoid main(String[] args)
publicvoid service(ServletRequestreq, ServletResponse res)
throwsIOException,
ServletException
{res.setContentType("text/html");
PrintWriter pw = res.getWriter();
java.util.Date date = newjava.util.Date();
pw.println(“<h3>” + “Current Date & Time:” + date.toString</h3>);
pw.close();}}
OUTPUT:-