Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Shri Vaishnav Vidyapeeth Vishvidyalaya

Shri Vaishnav Institute of Information


Technology

Session= Jan-Jun 2022

Subject Code = BTCS307

Subject Name = Advanced Java


Branch = Bachelor in

Technology(B.Tech)

Class= CSE[BDA-IBM]

Section=E

Year= 2nd /Semister= 4th

Enrollment Number = 20100BTBDAI07213

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”.

The components are placed inside the top-


level AWT container java. awt. Frame,
arranged in Flow Layout.
3. Write a Swing application ( called 8
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.

4. Write a Servlet application to count the total 12


Number of visits on your website.
5. Write a Servlet application to print the current 13
date and time.

Experiment1

Objective:- Write an event handling program to create 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.

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”);

bt2.setBounds(150,60, 90, 30);


bt2.addActionListener(this);
tx3=newTextField();
tx3.setBounds(30, 90, 90, 30);
bt3=new Button(“third”);
bt3.setBounds(150,90, 90, 30);
bt3.addActionListener(this);
add(tx1);
add(tx2);
add(tx3);
add(bt1);
add(bt2);
add(bt3);
setSize(500,500);
setLayout(null);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvent e){
if(e.getSource()==bt1) { tx1.setText(“first”);}
if(e.getSource()==bt2) { tx2.setText(“second”);}
if(e.getSource()==bt3) {tx3.setText(“third”);}}

publicstaticvoid main(String args[]) {

ButtonEvent B=newButtonEvent();
B.addWindowListener(newWindowAdapter() {
publicvoidwindowClosing(WindowEvent we) {
System.exit(0);}});}}

OUTPUT:-

Experiment2

Objective:- Write an AWT GUI application (called AWT 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”.

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

Objective:-Write a Servlet application to count the total number of


visits on your website.
PROGRAM CODE:-
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclasswebsite_viewextendsHttpServlet {
privateintiHitCounter;publicvoidinit() throwsServletException
{iHitCounter = 0;}
publicvoiddoGet(HttpServletRequest request, HttpServletResponse
response)
throwsServletException,
IOException
{PrintWriter out = response.getWriter();
out.println(“<form>”<fieldset style= ‘width:15’> ”);
out.println(“<h3>”Welcome to my website!</h3><hr>”);
out.println(“You are visitor number:” + (++iHitCounter));
out.println(“</fieldset></form>”);}
publicvoiddoPost(HttpServletRequest request, HttpServletResponse
response)
throwsServletException,
IOException
{doGet(request, response);}}
OUTPUT:-

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(&quot;text/html&quot;);
PrintWriter pw = res.getWriter();
java.util.Date date = newjava.util.Date();
pw.println(“<h3>” + “Current Date & Time:” + date.toString</h3>);
pw.close();}}

OUTPUT:-

You might also like