HMR
INSTITUTE OF TECHNOLOGY AND MANAGEMENT
PROGRAMMING IN JAVA Lab File
Bachelor of Technology
in
Information Technology
2022-2026
Code : (CIC-258)
Submitted To : Submitted By :
Mr. Sachin Chauhan Kriti
00513303122
IT
4th Sem
INDEX
.No. Aim of Experiment Experiment Remarks Teacher’s
Date Sign
1. Write a java program to
implement stack and queue
concept.
2. Write a Java program to
produce the tokens from
given long string.
3. Write a Java package to
show dynamic
polymorphism and
interfaces.
4. Write a Java program to
show multithreaded
producer and consumer
application.
5. Create a customized
exception and also make
use of all the five exception
keywords.
6. Convert the content of a
given file into the
uppercase content of the
same file.
7. Write a program in Java to
sort the content of a given
text file.
8. Develop an analog clock
using applet.
9. Develop a scientific
calculator using swings.
10. Create an editor like MS
Word using swings.
EXPERIMENT –1
AIM: Write a Java program to implement Stack and Queue concept
CODE(STACK):
import [Link];
//Stack implementation
class Stack {
private LinkedList<Integer> list;
public Stack() {
list = new LinkedList<>();
}
public void push(int value) {
[Link](value);
}
public int pop() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
return [Link]();
}
public boolean isEmpty() {
return [Link]();
}
}
//Queue implementation
class Queue {
private LinkedList<Integer> list;
public Queue() {
list = new LinkedList<>();
}
public void enqueue(int value) {
[Link](value);
}
public int dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
return [Link]();
}
public boolean isEmpty() {
return [Link]();
}
}
//Main class to test the implementations
public class hello {
public static void main(String[] args) {
// Stack example
Stack stack = new Stack();
[Link](10);
[Link](20);
[Link](30);
[Link]("Popped from stack: " + [Link]());
[Link]("Popped from stack: " + [Link]());
// Queue example
Queue queue = new Queue();
[Link](40);
[Link](50);
[Link](60);
[Link]("Dequeued from queue: " + [Link]());
[Link]("Dequeued from queue: " + [Link]());
}
}
OUTPUT
EXPERIMENT –2
AIM: Write a Java program to produce tokens from given long string.
CODE:
import [Link];
public class hello {
public static void main(String[] args) {
String longString = "This is a long string with multiple tokens";
// Using StringTokenizer
StringTokenizer tokenizer = new StringTokenizer(longString);
[Link]("Tokens using StringTokenizer:");
while ([Link]()) {
String token = [Link]();
[Link](token);
}
// Using split method
[Link]("\nTokens using split method:");
String[] tokens = [Link]("\\s+"); // Split by whitespace
for (String token : tokens) {
[Link](token);
}
}
}
OUTPUT
EXPERIMENT -3
AIM: Write a java package to show dynamic polymorphism and interfaces.
CODE(POLYMORPHISM):
// Interface definition
interface Animal {
void makeSound();
}
// Concrete class implementing the Animal interface
class Dog implements Animal {
@Override
public void makeSound() {
[Link]("Dog barks");
}
}
// Another concrete class implementing the Animal interface
class Cat implements Animal {
@Override
public void makeSound() {
[Link]("Cat meows");
}
}
// Another interface definition
interface Shape {
double calculateArea();
}
// Concrete class implementing the Shape interface
class Circle implements Shape {
private double radius;
public Circle(double radius) {
[Link] = radius;
}
@Override
public double calculateArea() {
return [Link] * radius * radius;
}
}
// Another concrete class implementing the Shape interface
class Square implements Shape {
private double side;
public Square(double side) {
[Link] = side;
}
@Override
public double calculateArea() {
return side * side;
}
}
// Main class demonstrating dynamic polymorphism and interfaces
public class hello {
public static void main(String[] args) {
// Dynamic polymorphism with Animal interface
Animal dog = new Dog();
Animal cat = new Cat();
[Link]();
[Link]();
// Dynamic polymorphism with Shape interface
Shape circle = new Circle(5.0);
Shape square = new Square(4.0);
[Link]("Circle area: " + [Link]());
[Link]("Square area: " + [Link]());
}
}
OUTPUT
EXPERIMENT -4
AIM: Write a java program to show multithreaded producer and
consumer application
CODE:
import [Link];
class SharedResource {
private final int capacity;
private final LinkedList<Integer> buffer;
public SharedResource(int capacity) {
[Link] = capacity;
[Link] = new LinkedList<>();
}
public void produce(int item) throws InterruptedException {
synchronized (this) {
while ([Link]() == capacity) {
wait(); // Wait if the buffer is full
}
[Link](item);
[Link]("Produced: " + item);
notify(); // Notify consumer that an item is produced
}
}
public void consume() throws InterruptedException {
synchronized (this) {
while ([Link]()) {
wait(); // Wait if the buffer is empty
}
int item = [Link]();
[Link]("Consumed: " + item);
notify(); // Notify producer that an item is consumed
}
}
}
class Producer extends Thread {
private final SharedResource sharedResource;
public Producer(SharedResource sharedResource) {
[Link] = sharedResource;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
[Link](i);
[Link](1000); // Simulate some production time
}
} catch (InterruptedException e) {
[Link]();
}
}
}
class Consumer extends Thread {
private final SharedResource sharedResource;
public Consumer(SharedResource sharedResource) {
[Link] = sharedResource;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
[Link]();
[Link](1000); // Simulate some consumption time
}
} catch (InterruptedException e) {
[Link]();
}
}
}
public class hello {
public static void main(String[] args) {
SharedResource sharedResource = new SharedResource(2); // Buffer
capacity is 2
Producer producer = new Producer(sharedResource);
Consumer consumer = new Consumer(sharedResource);
[Link]();
[Link]();
}
}
OUTPUT
EXPERIMENT -5
AIM: Create a customized exception and also make use of all the five
exception keywords.
CODE:
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class hello {
public static void main(String[] args) {
try {
exampleFunction(5);
exampleFunction(-2);
} catch (CustomException ce) {
[Link]("Custom exception caught: "
+[Link]());
} catch (ArithmeticException ae) {
[Link]("Arithmetic exception caught: " +
[Link]());
} finally {
[Link]("Finally block executed.");
}
}
static void exampleFunction(int value) throws CustomException {
try {
if (value < 0) {
throw new CustomException("Input value cannot be negative.");
}
int result = 10 / value;
[Link]("Result: " + result);
} catch (ArithmeticException ae) {
[Link]("Arithmetic exception caught: " +
[Link]()); throw ae; // Re-throwing the caught exception
}
}
OUTPUT