0% found this document useful (0 votes)
66 views20 pages

AakashVerma ICS-G Java Assigtnment

This document contains code for 5 Java programming assignments: 1. A program to handle button clicks and display text in text boxes. 2. A program creating a counter application with a label, text field, and button. 3. A program creating a calculator application with arithmetic buttons and text field. 4. A servlet program to count website visits by storing a visit cookie. 5. A servlet program to print the current date and time.

Uploaded by

Prerna Morwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views20 pages

AakashVerma ICS-G Java Assigtnment

This document contains code for 5 Java programming assignments: 1. A program to handle button clicks and display text in text boxes. 2. A program creating a counter application with a label, text field, and button. 3. A program creating a calculator application with arithmetic buttons and text field. 4. A servlet program to count website visits by storing a visit cookie. 5. A servlet program to print the current date and time.

Uploaded by

Prerna Morwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Shree Vaishnav Institute of Information Technology

Indore

Advanced Java lab


Assignment

Submitted to: Submitted by:


Kavita Mulchandani Aakash Verma
20100BTCSICS07309
Practical 1
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:
import java.awt.*;
import java.awt.event.*;

class Myframe extends Frame implements ActionListener{

Label l,m,n;
TextField l1,m1,n1;
Button b;
TextArea all;
public Myframe(){

super("AAKASH VERMA");
Font f1=new Font("Bauhuas 93",Font.BOLD,50);
l=new Label("First name");
l.setFont(f1);

l1=new TextField(20);
l1.setFont(f1);
l1.setBounds(40, 60, 70, 80);
m=new Label("Last name");
m.setFont(f1);
m1=new TextField(20);
m1.setFont(f1);
l1.setBounds(40, 60, 70, 80);
n=new Label("Nick Name ");
n.setFont(f1);
n1=new TextField(20);
n1.setFont(f1);
l1.setBounds(40, 60, 70, 80);

all=new TextArea(10,40);
all.setFont(f1);
b=new Button("Login");
b.setFont(f1);

setLayout(new FlowLayout());

add(l);
add(l1);
add(m);
add(m1);
add(n);
add(n1);
add(b);
add(all);

b.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


all.append(l1.getText());
all.append(m1.getText());
all.append(n1.getText());
}
}
public class nameform {

public static void main(String[] args){


Myframe obj=new Myframe();
obj.setSize(400,400);
obj.setVisible(true);
}
}
Practical 2
Write an AWT GUI application (called AWTCounter) as shown in figure.
Each time the “Count” button is clicked, the counter value shall increase by 1.
The program has three components:
1. A java.awt.Label “Counter”;
2. A non-editable java.awt.TextField to display the counter value; and
3. A java.awt.Button “Count”;
The components are placed inside the top-level AWT container java.awt.Frame,
arrange in FlowLayout.
Program:
import java.awt.*;
import java.awt.event.*;

class count1 extends Frame implements ActionListener{


int count=0;
Label l;
TextField ta;
Button Press;
count1(){
super("COUNT VALUE CREATED BY AAKASH ");
l=new Label("Count");
ta=new TextField("0",23);
Press=new Button("press");
setLayout(new FlowLayout());
add(l);
add(ta);
add(Press);
Press.addActionListener(this);
}public void actionPerformed(ActionEvent e) {

count++;
ta.setText(" "+count);
}

}
public class count {
public static void main(String[] arg){
count1 ob=new count1();
ob.setSize(400,400);
ob.setVisible(true);
}
}
Practical 3
Write a Swing application(called SwingArithmetics) to include buttons “+” , “-” ,
“*” , “%”(remainder) and “CLEAR” as shown in figure.
The “+” button applies add operation on integers and display the result.
The “-” button applies substract operation on integers and display the result.
The “*” button applies multiply operation on integers and display the result.
The “%” button applies division operation on integers and display the result.
The “CLEAR” button shall clear all the text fields.
Program:
[24:41, 4/14/2022] Aakasshhh: import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.script.*;

class Fdemo extends JFrame implements ActionListener


{
int x=0 ,y=100,w=100,h=100;
int k=0;
String
data[]={"B","C","1/x","sqrt","7","8","9","/","6","5","4","*","1","2","3","+","0",".","=",
"-"};
JTextField tx1;
JButton b[]=new JButton[20];
Fdemo()
{
setLayout(null);
Font f=new Font(" ",Font.BOLD,30);

tx1=new JTextField();
tx1.setSize(400,100);
tx1.setLocation(0,0);
tx1.setFont(f);
tx1.setHorizontalAlignment(JTextField.RIGHT);
add(tx1);

for(int i=1; i<=5; i++)


{
for(int j=1;j<=4;j++)
{

b[k]=new JButton(data[k]);
b[k].setSize(w,h);
b[k].setLocation(x,y);
b[k].setFont(f);
b[k].setBackground(Color.GRAY);
add(b[k]);

b[k].addActionListener(this);
k++;
x=x+100;
}

x=0;
y=y+100;
}

public void actionPerformed(ActionEvent e)


{

if(e.getSource()==b[0])
{
String s1=tx1.getText();
tx1.setText(s1.substring(0,s1.length()-1));
}
else if(e.getSource()==b[1])
{
tx1.setText("");
}
else if(e.getSource()==b[2])
{
String s1=tx1.getText();
double a=Double.parseDouble(s1);
a=1/a;
tx1.setText(""+a);
}
else if(e.getSource()==b[3])
{
String s1=tx1.getText();
double a=Double.parseDouble(s1);
tx1.setText(""+Math.sqrt(a));
}
else if(e.getSource()==b[18])
{
String s1=tx1.getText();
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine se=sem.getEngineByName("js");
try
{
tx1.setText(""+se.eval(s1));
}
catch(Exception exc){}
}
else
{
JButton b1=(JButton)e.getSource();
String s5=tx1.getText() + b1.getLabel();
tx1.setText(s5);
}
}
}

class cal01
{
public static void main (String arg [])
{
Fdemo f=new Fdemo ();
f.setSize(425,650);
f.setLocation(300,200);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
}
Practical 4
Write a Servlet application to count the total number of visits on your website.
Program:
package serverpage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class visitPage
*/
@WebServlet("/visitPage")
public class visitPage extends HttpServlet {
static int i=1;
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public visitPage() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub

PrintWriter out=response.getWriter();
response.setContentType("text/html");
out.print("<html>");
out.print("<body>");
out.println("<title>Servlet Page visit</title>");
out.print("<h4>first servelt page in java created by Aakash Verma");
out.print("<br>");

Cookie c=new Cookie("visit",String.valueOf(i));


response.addCookie(c);

int j=Integer.parseInt(c.getValue());
if(j==1){
out.println("Welcom user");
}
else{
out.println("you have visited this website"+ " "+j+" "+"times");
}
i++;

// out.print("<h1>Servelet visitPage at"+ request.getContextPath()+"</h1>");


out.print("<body>");
out.print("</html>");

out.close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

Practical 5
Write a Servlet application to print the current date and time.
Program:
package Aakash.verma;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Datetime
*/
@WebServlet("/Datetime")
public class Datetime extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public Datetime() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html><body>");
out.print("Hellooo User!....Current date and time is: " );
Date d=new Date();
d.getDate();
out.print(d);

//d.getTime();
out.print("</body></html>");

}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

You might also like