Java.varshaKumari
Java.varshaKumari
2022
Department of Computer Science & Engineering
9. Create a servlet that uses Cookies to store the number of times a 34-36
user has visited your servlet.
10. Create a simple java bean having bound and constrained 37-38
properties.
PROGRAM-1
package com.company;
import java.util.Stack;
class Main1 {
// Creating a stack
Main1(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}
// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
}
}
OUTPUT
Queue() {
front = -1;
rear = -1;
}
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
// remove element from the front of queue
element = items[front];
q.display();
}
}
OUTPUT
2. Write a java package to show dynamic polymorphism and interfaces
Polymorphism is that it has many forms that mean one specific defined form is used in many different
ways.
Dynamic Polymorphism
This type of polymorphism is resolved by the java virtual machine, not by the java compiler. That’s
why this type of polymorphism is called run-time polymorphism. Run time polymorphism occurs
during method overriding in java.
import java.io.*;
class GFG1 {
//name method
void name() {
// Method 1
void name() {
// Print statement
// Method 2
// Now creating 2 objects with different references and // calling the Method 1 over the
objects
ob.name();
ob1.name();
}
}
Interface - Interfaces are very similar to classes. They have variables and methods but the interfaces
allow only abstract methods(that don’t contain the body of the methods), but what is the difference
between the classes and the interfaces? The first advantage is to allow interfaces to implement the
multiple inheritances in a particular class. The JAVA language doesn’t support multiple inheritances if
we extend multiple classes in the class, but with the help of the interfaces, multiple inheritances are
allowed in Java.
}
void printSalary() { System.out.println(this.salary); }
}
// Class 2
// Implementing the salary inside the SDE2 class
class SDE2 implements salary {
int salary;
@Override public void insertsalary(int salary) {
this.salary = salary;
}
void printSalary() { System.out.println(this.salary); }
}
System.out.println("Producer produced-"
+ value);
// to insert the jobs in the list
list.add(value++);
// and sleep
Thread.sleep(1000);
}
}
}
}
}
4. Create a customised exception and also make use of all the 5 exception
keywords ?
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
// Class
class GFG {
// Condition check
// Reading the file using read() method which
// returns -1 at EOF while reading
while ((i = fr.read()) != -1) {
// Storing every character in the string and converting into upper case
str += (char)i.toUpperCase();
}
// Display message
System.out.println(
"File reading and writing both done");
}
// Display message
System.out.println(
"There are some IOException");
}
}
}
6. Develop an analog clock using applet .
// Java program to illustrate
// analog clock using Applets
import java.applet.Applet;
import java.awt.*;
import java.util.*;
@Override
public void init()
{
// Applet window size & color
this.setSize(new Dimension(800, 400));
setBackground(new Color(50, 50, 50));
new Thread() {
@Override
public void run()
{
while (true) {
repaint();
delayAnimation();
}
}
}.start();
}
// 12 hour format
if (hour > 12) {
hour -= 12;
}
// Labeling
g.setColor(Color.black);
g.drawString("12", 390, 120);
g.drawString("9", 310, 200);
g.drawString("6", 400, 290);
g.drawString("3", 480, 200);
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class calculator extends JFrame implements ActionListener {
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
// default constructor
calculator()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
// create a textfield
l = new JTextField(16);
// equals button
beq1 = new JButton("=");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
double te;
// convert it to string
s0 = Double.toString(te);
s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
// convert it to string
s0 = Double.toString(te);
// place the operator
s1 = s;
// Frame
JFrame f;
// Constructor
editor()
{
// Create a frame
f = new JFrame("editor");
try {
// Set metal look and feel
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// Text component
t = new JTextArea();
// Create a menubar
JMenuBar mb = new JMenuBar();
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi9);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
mc.addActionListener(this);
mb.add(m1);
mb.add(m2);
mb.add(mc);
f.setJMenuBar(mb);
f.add(t);
f.setSize(500, 500);
f.show();
}
// If a button is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("cut")) {
t.cut();
}
else if (s.equals("copy")) {
t.copy();
}
else if (s.equals("paste")) {
t.paste();
}
else if (s.equals("Save")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
if (r == JFileChooser.APPROVE_OPTION) {
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Write
w.write(t.getText());
w.flush();
w.close();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "the user cancelled the operation");
}
else if (s.equals("Print")) {
try {
// print the file
t.print();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
else if (s.equals("Open")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new BufferedReader(fr);
// Initialize sl
sl = br.readLine();
// Main class
public static void main(String args[])
{
editor e = new editor();
}
}
9. Create a servlet that uses cookies to store the number of times a user has
visited your servlet ?
index.html
FirstServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
out.close();
}catch(Exception e){System.out.println(e);}
}
}
SecondServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
}
web.xml
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
Output
import java.awt.Graphics;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.swing.JComponent;
/**
* Bean with bound properties.
*/
public class MyBean
extends JComponent
implements Serializable
{
private String title;
private String[] lines = new String[10];
if ( this.lines != null )
{
int step = height;
for ( String line : this.lines )
paintString( g, line, height += step );
}
}