0% found this document useful (0 votes)
2 views

java_practicals_with_outputs[1] (1)

The document outlines practical Java programming experiments completed by students in a Computer Science & Engineering course, including implementations of stacks, queues, polymorphism, multithreading, and GUI applications. It also includes certificates of achievement for two students, Mr. Mohit and Miss. Manya, recognizing their dedication in the Java Programming Lab. Various Java code examples are provided for each experiment, demonstrating different programming concepts and functionalities.

Uploaded by

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

java_practicals_with_outputs[1] (1)

The document outlines practical Java programming experiments completed by students in a Computer Science & Engineering course, including implementations of stacks, queues, polymorphism, multithreading, and GUI applications. It also includes certificates of achievement for two students, Mr. Mohit and Miss. Manya, recognizing their dedication in the Java Programming Lab. Various Java code examples are provided for each experiment, demonstrating different programming concepts and functionalities.

Uploaded by

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

Java Practical Solutions

Sr. Experiment name Date Sign.


No
1. Java program to implement
stack and queue concept
2. Java package to show dynamic
polymorphism and interfaces
3. Java program to show
multithreaded producer and
consumer application
4. Customized exception and use
of exception keywords
5. Convert file content to
uppercase and save to the
same file
6. Analog clock using applet
7. Scientific calculator using
swings
8. MS-Word-like editor using
swings
9. Servlet using Cookies to
track user visits
10. Java bean with bound and
constrained properties

Page 1
Java Practical Solutions

CERTIFICATE OF ACHIEVEMENT
(Computer Science & Engineering)

This is to certify that

Mr. Mohit
Roll No.: 238032
Branch: B. Tech(AI & DS)
SEMESTER: 4th Sem

has successfully completed all the practical experiments in the subject


"Java Programming Lab"
with dedication and excellence.
This certificate is awarded in recognition of their hard work and commitment to
learning.
Date:

Signature

GLOBAL INSTITUTE OF TECHNOLOGY AND MANAGEMENT,


GURUGRAM(HARYANA)

Page 2
Java Practical Solutions

CERTIFICATE OF ACHIEVEMENT
(Computer Science & Engineering)

This is to certify that

Miss. Manya
Roll No.: 238030
Branch: B. Tech(AI & DS)
SEMESTER: 4th Sem

has successfully completed all the practical experiments in the subject


"Java Programming Lab"
with dedication and excellence.

This certificate is awarded in recognition of their hard work and commitment to


learning.

Date:

Signature

GLOBAL INSTITUTE OF TECHNOLOGY AND MANAGEMENT,


GURUGRAM(HARYANA)

Page 3
Java Practical Solutions

Create a java program to implement stack and queue concept.

importjava.util.*; class
StackQueue {
public static void main(String[] args) { Stack<Integer> stack = new
Stack<>(); Queue<Integer> queue = new LinkedList<>();

stack.push(1); stack.push(2); stack.push(3);


System.out.println("Stack: " + stack); System.out.println("Stack
Pop: " + stack.calc());

queue.add(1); queue.add(2); queue.add(3); System.out.println("Queue: " + queue);


System.out.println("Queue Remove: " + queue.remove());
}
}

Output:

Page 4
Java Practical Solutions

Write a java package to show dynamic polymorphism and interfaces.

package polymorphism;
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Dog barks"); }
}
class Cat implements Animal {
public void sound() { System.out.println("Cat meows"); }
}
public class TestPolymorphism {
public static void main(String[] args) { Animal a = new
Dog();
a.sound();
a = new Cat();
a.sound();
}
}

Output:
Dog barks

Catmeows

Page 5
Java Practical Solutions

Write a java program to show multithreaded producer and consumer

application.

class Q {
int num;
boolean valueSet = false; synchronized void
put(int num) {
while(valueSet) {
try { wait(); } catch(Exception e) {}
}
System.out.println("Put: " + num); this.num =
num;
valueSet = true; notify();
}
synchronized int get() { while(!valueSet) {
try { wait(); } catch(Exception e) {}
}
System.out.println("Get: " + num); valueSet =
false;
notify(); return num;
}
}
class Producer implements Runnable { Q q;
Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { int i = 0;
while(true) { q.put(i++); }
}
}
class Consumer implements Runnable { Q q;
Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() {
while(true) { q.get(); }
}
}
public class PC {
public static void main(String[] args) { Q q = new Q();
new Producer(q); new
Consumer(q);
}
}

Page 6
Java Practical Solutions

Output:

Page 7
Java Practical Solutions

Create a customized exception and also make use of all the 5 exception

keywords.

class MyException extends Exception {


public MyException(String message) { super(message); }
}
public class TestException {
public static void main(String[] args) { try {
throw new MyException("Custom Exception");
} catch(MyException e) { System.out.println("Caught: " + e);
} finally {
System.out.println("Finally block executed");
}
}
}

Output:

Page 8
Java Practical Solutions

Convert the content of a given file into the upper-case content of the same file.

import java.io.*;
public class UpperCaseFile {
public static void main(String[] args) throws IOException { FileReader fr = new
FileReader("input.txt");
FileWriter fw = new FileWriter("output.txt"); int ch;
while((ch = fr.read()) != -1) { fw.write(Character.toUpperCase(ch));
}
fr.close();
fw.close();
}
}

Output:

Page 9
Java Practical Solutions

Develop an analog clock using applet.

importjava.applet.*; import
java.awt.*; import java.util.*;
public class AnalogClock extends Applet implements Runnable { Thread t;
public void init() { t = new Thread(this); t.start(); } public void paint(Graphics g) {
Calendar cal = Calendar.getInstance(); int h =
cal.get(Calendar.HOUR);
int m = cal.get(Calendar.MINUTE); int s =
cal.get(Calendar.SECOND);
g.drawOval(100, 100, 200, 200);
g.drawString(h + ":" + m + ":" + s, 150, 200);
}
public void run() { while(true) {
repaint();
try { Thread.sleep(1000); } catch(Exception e) {}
}
}
}

Output:
(Analog Clock window showing current time updated every second)

Page 10
Java Practical Solutions

Develop a scientific calculator using swings.

import javax.swing.*; import


java.awt.*; import
java.awt.event.*;
public class Calculator {
public static void main(String[] args) { JFrame f = new
JFrame("Calculator"); JTextField tf = new
JTextField(); tf.setBounds(30, 40, 280, 30);
JButton b1 = new JButton("1"); b1.setBounds(40, 100, 50, 40);
// Add more buttons similarly and their listeners f.add(tf); f.add(b1);
f.setSize(400, 500); f.setLayout(null);
f.setVisible(true);
}
}

Output:

Page 11
Java Practical Solutions

Create an editor like MS-word using swings.

importjavax.swing.*; public
class Editor {
public static void main(String[] args) { JFrame f = new
JFrame("Editor"); JTextArea area = new
JTextArea();
JScrollPane scroll = new JScrollPane(area); f.add(scroll);
f.setSize(500, 500); f.setVisible(true);
}
}

Output:

Page 12
Java Practical Solutions

Create a servlet that uses Cookies to store the number of times a user has

visited your servlet.

import java.io.*; import


javax.servlet.*;
import javax.servlet.http.*;
public class CookieServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
response.setContentType("text/html"); PrintWriter out =
response.getWriter(); Cookie[] cookies =
request.getCookies(); int count = 1;
if(cookies != null) { for(Cookie c : cookies)
{
if(c.getName().equals("visit")) {
count = Integer.parseInt(c.getValue()) + 1;
}
}
}
Cookie visit = new Cookie("visit", Integer.toString(count)); response.addCookie(visit);
out.println("Visit count: " + count);
}
}

Output:
Visit count: 1
(Refresh page)

Visit count: 2
(Refresh page)

Visit count: 3

Page 13
Java Practical Solutions

Create a simple java bean having bound and constrained properties.

import java.beans.*;

public class PersonBean {

private String name;

private PropertyChangeSupport support = new PropertyChangeSupport(this);

public String getName() { return name; }

public void setName(String name) {

String oldName = this.name;

this.name = name;

support.firePropertyChange("name", oldName, name);

public void addPropertyChangeListener(PropertyChangeListener pcl) {

support.addPropertyChangeListener(pcl);

public class TestBean {

public static void main(String[] args) {

PersonBean bean = new PersonBean();

bean.addPropertyChangeListener(event ->

System.out.println("Old Name: " + event.getOldValue() + ", New Name: " + event.getNewValue()));

bean.setName("John");

bean.setName("Alex");

Output:

Page 14
Java Practical Solutions

Page 15

You might also like