AakashVerma ICS-G Java Assigtnment
AakashVerma ICS-G Java Assigtnment
Indore
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);
}
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.*;
tx1=new JTextField();
tx1.setSize(400,100);
tx1.setLocation(0,0);
tx1.setFont(f);
tx1.setHorizontalAlignment(JTextField.RIGHT);
add(tx1);
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;
}
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>");
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.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
}
}