CD FILE
CD FILE
Submission
S.No. EXPERIMENT NAME Practical Date REMARKS
Date
Create a customized
exception and also make use
5.
of all the 5 exception
keywords.
Theory:
Stack:- The stack is a linear data structure that is used to store the
collection of objects. It is based on the Last-In- First-Out (LIFO). Java
collection framework provides many interfaces and classes to store the
collection of objects.
Queue:- A queue is an interface provided in the java.util package that
extends collections to provide additional methods to access or manipulate
items at the head of the queue. Queue elements are commonly accessed in
a First-In- First-Out (FIFO) manner.
Code : Stack
public class Stack {
Stack(int size){
arr=new int[size];
capacity=size;
top=-1;
{ if(isFull()){
System.out.println("Stack Overflow");
System.exit(1);
System.out.println("Inserting"+x); arr[++top]=x;
if(isEmpty()){
}
return arr[top--];
return top+1;
return top==-1;
return top==capacity-1;
System.out.println(arr[i]+",");
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
stack.pop();
stack.printStack();
}
Code : Queue
public class Queue {
int size=5;
int front,rear;
Queue(){
front=-1; rear=-1;
boolean isFull(){
return true;
return false;
boolean isEmpty(){
if(front==-1){
return true;
else{
return false;
if (isFull()){
System.out.println("Queue is Full");
else{
if(front==-1){ front=0;
rear++; items[rear]=element;
System.out.println("Insert"+element);
}
}
int deQueue(){
int element;
if(isEmpty()){
else{
element=items[front];
if(front>=rear){ front=-1;
rear=-1;
else{
front++;
void display(){
int i;
if(isEmpty()){
System.out.println("Empty Queue");
else{
System.out.println("Items ->");
for(i=front;i<=rear;i++){
System.out.print(items[i]+" ");
q.deQueue();
for(int i=1;i<6;i++){
q.enQueue(i);
q.enQueue(6);
q.display();
q.deQueue();
q.display();
OUTPUT:
EXPERIMENT-2
Aim: Write a Java program to produce the tokens from given long string.
Language used: Java
Theory:
Token: In Java, tokens are the basic building blocks of a program. They are the smallest
individual units of a program that have meaning to the compiler and are used to represent
the various elements of a program such as keywords, identifiers and literals.
Code:
import java.util.StringTokenizer; public class
Simple {
System.out.println(st.nextToken());
OUTPUT:
EXPERIMENT-3
Aim: Write a Java package to show dynamic polymorphism and interfaces.
Language used: Java
Theory:
Dynamic Polymorphism:- Dynamic polymorphism in Java refers to the process when a
call to an overridden process is resolved at the runtime. The reference variable of a
superclass calls the overridden method. As the name dynamic connotes, dynamic
polymorphism happens among different classes as opposed to static polymorphism.
void name(){
ob.name();
ob1.name();
Code: Interface
import java.io.*; interface
In1{
System.out.println("Geek");
System.out.println(a);
Output:
EXPERIMENT-4
Aim: Write a Java program to show multithreaded producer and consumer application.
Language used: Java
Theory: The producer is to either go to sleep or discard data if the buffer is full. The next
time the consumer removes an item from the buffer, it notifies the producer, who starts to
fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to
be empty. The next time the producer puts data into the buffer, it wakes up the sleeping
consumer. An inadequate solution could result in a deadlock where both processes are
waiting to be awakened.
Code:
import java.util.LinkedList; public
class Threadexample {
pc.produce();
catch(InterruptedException e){
e.printStackTrace();
});
pc.consume();
catch(InterruptedException e){
e.printStackTrace();
});
t1.start();
t2.start();
t1.join();
t2.join();
while (true){
synchronized(this){
notify(); Thread.sleep(1000);
synchronized(this){
while(list.size()==0)
wait();
Thread.sleep(1000);
}
OUTPUT:
EXPERIMENT-5
Aim: Create a customized exception and also make use of all the 5 exception keywords.
Language used: Java
Theory: Customized Exception:- An exception is an issue that occurred during the
execution of a program. When an exception occurred the program gets terminated
abruptly and the code past the line that generated the execution never gets executed.
Java provides the facility to create our own exceptions, which are basically derived
classes of Exception. Creating our own exception is known as custom exception.
Basically, Java custom exceptions are used to customize the exception according to
our needs.
Code:
class MyException extends Exception{ public
MyException(String s){
super(s);
OUTPUT:
EXPERIMENT-6
Aim: Convert the content of a given file into the uppercase content of the same file.
Language used: Java
Code:
class Main{
Output:
EXPERIMENT-7
Aim: Write a program in Java to sort the content of a given text file.
import java.util.Scanner;
scanner.nextLine(); try {
writer.close();
reader.close(); Collections.sort(names);
writer.write(name + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
scanner.close();
OUTPUT:
EXPERIMENT-8
Aim; Develop an analog clock using applet.
Code:
import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
SwingUtilities.invokeLater(analogClock::createAndShowGUI);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new analogClock());
frame.pack();
frame.setVisible(true);
public analogClock() {
new Thread(this).start();
@Override
while (true) {
repaint();
delayAnimation();
} }
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
} }
@Override
super.paintComponent(g);
// 12 hour format
hour -= 12;
g.setColor(Color.WHITE);
// Labeling
g.setColor(Color.BLACK);
int x, y;
g.setColor(Color.RED);
g.setColor(Color.BLUE);
g.setColor(Color.BLACK);
@Override
}
OUTPUT:
EXPERIMENT-9
Aim: Develop a scientific calculator using swings.
Code:
import javax.swing.*; import
java.awt.*;
setTitle("Scientific Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
};
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
setLayout(new FlowLayout());
add(displayField); add(buttonPanel);
pack(); setLocationRelativeTo(null);
setVisible(true);
}
private class ButtonClickListener implements ActionListener {
calculate();
} else if ("clr".equals(command)) {
displayField.setText("");
} else if ("sin".equals(command)) {
displayField.setText(String.valueOf(Math.sin(Double.parseDoubl
e(expression))));
} else if ("cos".equals(command)) {
displayField.setText(String.valueOf(Math.cos(Double.parseDoubl
e(expression))));
} else if ("tan".equals(command)) {
displayField.setText(String.valueOf(Math.tan(Double.parseDoubl
e(expression))));
} else if ("sqrt".equals(command)) {
displayField.setText(String.valueOf(Math.sqrt(Double.parseDoub
le(expression))));
} else {
displayField.setText(displayField.getText() + command);
} catch (NumberFormatException e) {
displayField.setText("Error");
nextChar();
return true;
return false;
double parse() {
nextChar();
double x = parseExpression();
return x;
double parseExpression() {
double x = parseTerm();
while(true) {
if (eat('+')) x += parseTerm();
double parseTerm() {
double x = parseFactor();
while (true) {
}
double parseFactor() {
x = parseExpression();
eat(')');
nextChar();
x = Double.parseDouble(expression.substring(startPos, this.pos));
} else if (eat('e')) { x =
Math.E;
x = Math.PI;
} else {
return x;
}.parse();
public void
run() {
new
Calcul
ator();
});
}
OUTPUT:
EXPERIMENT-10
Aim: Create an editor like MS- Word using swings.
Code:
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
editor()
// Create a frame
f = new JFrame("editor");
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
catch (Exception e) {
// Text component
t = new JTextArea();
// Create a menubar
JMenuBar mb = new JMenuBar();
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
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
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")) {
int r = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
try {
// Write
w.write(t.getText());
w.flush();
w.close();
JOptionPane.showMessageDialog(f, evt.getMessage());
else
else if (s.equals("Print")) {
try {
t.print(); }
JOptionPane.showMessageDialog(f, evt.getMessage());
else if (s.equals("Open")) {
int r = j.showOpenDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
try {
// String
// File reader
// Buffered reader
// Initialize sl
sl = br.readLine();
sl = sl + "\n" + s1; }
JOptionPane.showMessageDialog(f, evt.getMessage()); }
else
else if (s.equals("New")) {
t.setText("");
else if (s.equals("close")) {
f.setVisible(false) }
// Main class
OUTPUT: