PRACTICAL FILE
SESSION: 2023-24
Programming In Java Lab
(CIC-258)
II Year, 4th Sem
Submitted to: Submitted by:
Ms. Rashmi Sharma Name : Mitesh kumar Singh
Assistant Professor Enrollment No:14518002722
Department of Computer Science and Engineering
Delhi Technical Campus, Greater Noida
INDEX
[Link]. Experiment Signature
Date of Date of
Conduction Submission
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 5 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(a)
Objective: Write a java program to implement stack concepts.
Program:
public class StackArrayImplementation{
int top; int capacity;
int [] Stack;
StackArrayImplementation()
{ top=-1;
capacity=10;
Stack = new int[capacity];
}
public boolean isEmpty(){
return top == -1;
}
public boolean isFull(){ return
top == capacity-1;
}
public int push(int data){
if(isFull()){
[Link]("Stack is Full");
}
return Stack[++top] =data;
} public int
pop(){
if(isEmpty()){
[Link]("Stack is Empty”);
} public int peek(){
return Stack[top];
}
public void display(){
for(int i : Stack){
[Link](i);}
}
public static void main(String args[]){
StackArrayImplementation st = new
StackArrayImplementation(); [Link](34); [Link](314);
[Link](24); [Link](14); [Link](40);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]);
[Link]([Link]());
[Link]([Link]());
[Link]();
}
}
Output:
EXPERIMENT 1(b)
Objective: Write a java program to implement Queue concepts.
Program:
public class QueueImplementation{
int front; int
rear;
int
capacity=5;
int [] object;
QueueImplementation()
{ front = -1; rear = -
1;
object = new int[capacity];
} boolean
isEmpty(){
return rear <= front;
} int enqueue(int
data){
return object [++rear]=data;
} int
dequeue(){
if(isEmpty()){
[Link]("Queue is Empty");
} if(rear >=front){ return
object[++front];
}
return (Integer)null;
} int
size(){
return rear-front;
}
public static void main(String args[])
{
QueueImplementation q = new QueueImplementation();
[Link]([Link]()); [Link](12);
[Link](34);
[Link](6);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
Output:
EXPERIMENT 2
Objective: Write a java program to produce the tokens from given long string.
Program:
import [Link];
class TokenTest { public static void
main(String[] args) {
StringTokenizer S1 = new StringTokenizer("Welcome@to@core@java@programming", "@");
[Link]("No. of tokens: " + [Link]());
while ([Link]()) {
[Link]("Next token is: " + [Link]("@ "));
}
}
}
Output:
EXPERIMENT 3
Objective: Write a java package to show dynamic polymorphism and interfaces.
Program:
interface Area{
final static float pi=3.14F; float
compute(float x,float y);
}
class Rectangle implements Area{ public
float compute(float x , float y)
{ return(x*y);
}
} class Circle implements Area{ public
float compute(float x ,float y)
{
return(pi*x*x);
}
} class InterfaceTest{ public static void
main (String args[]){
Rectangle rect = new Rectangle();
Circle c1 = new Circle();
Area area;
area = rect;
[Link]("Area of Rectangle = "+[Link](10,20));
area = c1;
[Link]("Area of Circle = "+[Link](10,0));
}
Output:
EXPERIMENT 4
AIM: Write a java program to show multithreaded producer and consumer application.
Code:
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
[Link]();
[Link]();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
[Link] = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = [Link]();
[Link]("Consumer #" + [Link] + " got: " + value);
}
}
}
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
[Link] = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
[Link](i);
[Link]("Producer #" + [Link] + " put: " + i);
try {
sleep((int)([Link]() * 100));
} catch (InterruptedException e) { }
}
}
}
Output:
EXPERIMENT 5
AIM: Create a customized exception and also make use of all the 5 exception keywords.
Code:
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class Exp{
public static void main(String[] args) {
try {
throw new CustomException("This is a custom exception.");
} catch (CustomException e) {
[Link]("Caught custom exception: " + [Link]());
} finally {
[Link]("Finally block executed.");
}
}
}
Output:
EXPERIMENT 6
AIM: Convert the content of a given file into the uppercase content of the same file.
Code:
import [Link].*;
public class UppercaseFile {
public static void main(String[] args) throws IOException {
File inputFile = new File("[Link]");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String line = [Link]();
StringBuilder sb = new StringBuilder();
while (line != null) {
[Link]([Link]());
[Link]([Link]());
line = [Link]();
}
[Link]();
FileWriter writer = new FileWriter(inputFile);
[Link]([Link]());
[Link]();
}
}
Output:
[Link] Before:
[Link] After:
Experiment 7
AIM: Write a program in java to sort the content of a given text file.
Code:
import [Link].*;
import [Link].*;
public class SortFile {
public static void main(String[] args) throws IOException {
File inputFile = new File("[Link]");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
List<String> lines = new ArrayList<>();
String line = [Link]();
while (line != null) {
[Link](line);
line = [Link]();
}
[Link]();
[Link](lines);
FileWriter writer = new FileWriter(inputFile);
for (String sortedLine : lines) {
[Link](sortedLine + [Link]());
}
[Link]();
}
}
Output:
[Link] before
[Link] after
EXPERIMENT 8
AIM: Develop an analog clock using applet.
Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class AnalogClock extends Applet implements Runnable {
Thread t = null;
boolean threadSuspended;
public void start() {
if (t == null) {
t = new Thread(this);
threadSuspended = false;
[Link]();
} else {
if (threadSuspended) {
threadSuspended = false;
synchronized(this) {
notify();
}
}
}
}
public void stop() {
threadSuspended = true;
}
public void run() {
try {
while (true) {
[Link](1000);
if (threadSuspended) {
synchronized(this) {
while (threadSuspended)
wait();
}
}
repaint();
}
} catch (InterruptedException e) {}
}
public void paint(Graphics g) {
Calendar cal = [Link]();
int hour = [Link]([Link]);
int minute = [Link]([Link]);
int second = [Link]([Link]);
int xcenter = 125, ycenter = 125;
int radius = 100;
[Link]([Link]);
[Link](xcenter - radius, ycenter - radius, 2 * radius, 2 * radius);
[Link]([Link]);
[Link](xcenter - radius, ycenter - radius, 2 * radius, 2 * radius);
int xhour = (int)([Link]((hour + (double)minute / 60.0) * [Link] / 6.0) * (radius - 10) + xcenter);
int yhour = (int)(-[Link]((hour + (double)minute / 60.0) * [Link] / 6.0) * (radius - 10) + ycenter);
[Link](xcenter, ycenter, xhour, yhour);
int xminute = (int)([Link]((double)minute * [Link] / 30.0) * (radius - 5) + xcenter);
int yminute = (int)(-[Link]((double)minute * [Link] / 30.0) * (radius - 5) + ycenter);
[Link](xcenter, ycenter, xminute, yminute);
int xsecond = (int)([Link]((double)second * [Link] / 30.0) * radius + xcenter);
int ysecond = (int)(-[Link]((double)second * [Link] / 30.0) * radius + ycenter);
[Link]([Link]);
[Link](xcenter, ycenter, xsecond, ysecond);
}
}
/*
<applet code=" [Link]" width= 300 height= 100>
</applet>
*/
Output:
EXPERIMENT 9
AIM: Develop a scientific calculator using swings.
Code:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class ScientificCalculator extends JFrame implements ActionListener {
JTextField tfield;
double temp, temp1, result, a;
static double m1, m2;
int k = 1, x = 0, y = 0, z = 0;
char ch;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, pow3, exp,
fac, plus, min, div, log, rec, mul, eq, addSub, dot, mr, mc, mp,
mm, sqrt, sin, cos, tan;
Container cont;
JPanel textPanel, buttonpanel;
ScientificCalculator() {
cont = getContentPane();
[Link](new BorderLayout());
JPanel textpanel = new JPanel();
tfield = new JTextField(25);
[Link]([Link]);
[Link](new KeyAdapter() {
public void keyTyped(KeyEvent keyevent) {
char c = [Link]();
if (c >= '0' && c <= '9') {
} else {
[Link]();
}
}
});
[Link](tfield);
buttonpanel = new JPanel();
[Link](new GridLayout(8, 4, 2, 2));
boolean t = true;
mr = new JButton("MR");
[Link](mr);
[Link](this);
mc = new JButton("MC");
[Link](mc);
[Link](this);
mp = new JButton("M+");
[Link](mp);
[Link](this);
mm = new JButton("M-");
[Link](mm);
[Link](this);
b1 = new JButton("1");
[Link](b1);
[Link](this);
b2 = new JButton("2");
[Link](b2);
[Link](this);
b3 = new JButton("3");
[Link](b3);
[Link](this);
b4 = new JButton("4");
[Link](b4);
[Link](this);
b5 = new JButton("5");
[Link](b5);
[Link](this);
b6 = new JButton("6");
[Link](b6);
[Link](this);
b7 = new JButton("7");
[Link](b7);
[Link](this);
b8 = new JButton("8");
[Link](b8);
[Link](this);
b9 = new JButton("9");
[Link](b9);
[Link](this);
zero = new JButton("0");
[Link](zero);
[Link](this);
plus = new JButton("+");
[Link](plus);
[Link](this);
min = new JButton("-");
[Link](min);
[Link](this);
mul = new JButton("*");
[Link](mul);
[Link](this);
div = new JButton("/");
[Link](this);
[Link](div);
addSub = new JButton("+/-");
[Link](addSub);
[Link](this);
dot = new JButton(".");
[Link](dot);
[Link](this);
eq = new JButton("=");
[Link](eq);
[Link](this);
rec = new JButton("1/x");
[Link](rec);
[Link](this);
sqrt = new JButton("Sqrt");
[Link](sqrt);
[Link](this);
log = new JButton("log");
[Link](log);
[Link](this);
sin = new JButton("SIN");
[Link](sin);
[Link](this);
cos = new JButton("COS");
[Link](cos);
[Link](this);
tan = new JButton("TAN");
[Link](tan);
[Link](this);
pow2 = new JButton("x^2");
[Link](pow2);
[Link](this);
pow3 = new JButton("x^3");
[Link](pow3);
[Link](this);
exp = new JButton("Exp");
[Link](this);
[Link](exp);
fac = new JButton("n!");
[Link](this);
[Link](fac);
clr = new JButton("AC");
[Link](clr);
[Link](this);
[Link]("Center", buttonpanel);
[Link]("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s = [Link]();
if ([Link]("1")) {
if (z == 0) {
[Link]([Link]() + "1");
} else {
[Link]("");
[Link]([Link]() + "1");
z = 0;
}
}
if ([Link]("2")) {
if (z == 0) {
[Link]([Link]() + "2");
} else {
[Link]("");
[Link]([Link]() + "2");
z = 0;
}
}
if ([Link]("3")) {
if (z == 0) {
[Link]([Link]() + "3");
}
else {
[Link]("");
[Link]([Link]() + "3");
z = 0;
}
}
if ([Link]("4")) {
if (z == 0) {
[Link]([Link]() + "4");
} else {
[Link]("");
[Link]([Link]() + "4");
z = 0;
}
}
if ([Link]("5")) {
if (z == 0) {
[Link]([Link]() + "5");
} else {
[Link]("");
[Link]([Link]() + "5");
z = 0;
}
}
if ([Link]("6")) {
if (z == 0) {
[Link]([Link]() + "6");
} else {
[Link]("");
[Link]([Link]() + "6");
z = 0;
}
}
if ([Link]("7")) {
if (z == 0) {
[Link]([Link]() + "7");
} else {
[Link]("");
[Link]([Link]() + "7");
z = 0;
}
}
if ([Link]("8")) {
if (z == 0) {
[Link]([Link]() + "8");
} else {
[Link]("");
[Link]([Link]() + "8");
z = 0;
}
}
if ([Link]("9")) {
if (z == 0) {
[Link]([Link]() + "9");
} else {
[Link]("");
[Link]([Link]() + "9");
z = 0;
}
}
if ([Link]("0")) {
if (z == 0) {
[Link]([Link]() + "0");
} else {
[Link]("");
[Link]([Link]() + "0");
z = 0;
}
}
if ([Link]("AC")) {
[Link]("");
x = 0;
y = 0;
z = 0;
}
if ([Link]("log")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("1/x")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = 1 / [Link]([Link]());
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("Exp")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("x^2")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()), 2);
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("x^3")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()), 3);
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("+/-")) {
if (x == 0) {
[Link]("-" + [Link]());
x = 1;
} else {
[Link]([Link]());
}
}
if ([Link](".")) {
if (y == 0) {
[Link]([Link]() + ".");
y = 1;
} else {
[Link]([Link]());
}
}
if ([Link]("+")) {
if ([Link]().equals("")) {
[Link]("");
temp = 0;
ch = '+';
} else {
temp = [Link]([Link]());
[Link]("");
ch = '+';
y = 0;
x = 0;
}
[Link]();
}
if ([Link]("-")) {
if ([Link]().equals("")) {
[Link]("");
temp = 0;
ch = '-';
} else {
x = 0;
y = 0;
temp = [Link]([Link]());
[Link]("");
ch = '-';
}
[Link]();
}
if ([Link]("/")) {
if ([Link]().equals("")) {
[Link]("");
temp = 1;
ch = '/';
} else {
x = 0;
y = 0;
temp = [Link]([Link]());
ch = '/';
[Link]("");
}
[Link]();
}
if ([Link]("*")) {
if ([Link]().equals("")) {
[Link]("");
temp = 1;
ch = '*';
} else {
x = 0;
y = 0;
temp = [Link]([Link]());
ch = '*';
[Link]("");
}
[Link]();
}
if ([Link]("MC")) {
m1 = 0;
[Link]("");
}
if ([Link]("MR")) {
[Link]("");
[Link]([Link]() + m1);
}
if ([Link]("M+")) {
if (k == 1) {
m1 = [Link]([Link]());
k++;
} else {
m1 += [Link]([Link]());
[Link]("" + m1);
}
}
if ([Link]("M-")) {
if (k == 1) {
m1 = [Link]([Link]());
k++;
} else {
m1 -= [Link]([Link]());
[Link]("" + m1);
}
}
if ([Link]("Sqrt")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("SIN")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("COS")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("TAN")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = [Link]([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
if ([Link]("=")) {
if ([Link]().equals("")) {
[Link]("");
} else {
temp1 = [Link]([Link]());
switch (ch) {
case '+':
result = temp + temp1;
break;
case '-':
result = temp - temp1;
break;
case '/':
result = temp / temp1;
break;
case '*':
result = temp * temp1;
break;
}
[Link]("");
[Link]([Link]() + result);
z = 1;
}
}
if ([Link]("n!")) {
if ([Link]().equals("")) {
[Link]("");
} else {
a = fact([Link]([Link]()));
[Link]("");
[Link]([Link]() + a);
}
}
[Link]();
}
double fact(double x) {
int er = 0;
if (x < 0) {
er = 20;
return 0;
}
double i, s = 1;
for (i = 2; i <= x; i += 1.0)
s *= i;
return s;
}
public static void main(String args[]) {
try {
UIManager
.setLookAndFeel("[Link]");
} catch (Exception e) {
}
ScientificCalculator f = new ScientificCalculator();
[Link]("ScientificCalculator");
[Link]();
[Link](true);
}
}
Output:
EXPERIMENT 10
AIM: Create an editor like MS-word using swings.
Code:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class editor extends JFrame implements ActionListener {
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
editor()
{
// Create a frame
f = new JFrame("editor");
try {
// Set metal look and feel
[Link]("[Link]");
// Set theme to ocean
[Link](new OceanTheme());
}
catch (Exception e) {
}
// Text component
t = new JTextArea();
// Create a menubar
JMenuBar mb = new JMenuBar();
// Create amenu for menu
JMenu m1 = new JMenu("File");
// Create menu items
JMenuItem mi1 = new JMenuItem("New");
JMenuItem mi2 = new JMenuItem("Open");
JMenuItem mi3 = new JMenuItem("Save");
JMenuItem mi9 = new JMenuItem("Print");
// Add action listener
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](mi1);
[Link](mi2);
[Link](mi3);
[Link](mi9);
// Create amenu for menu
JMenu m2 = new JMenu("Edit");
// Create menu items
JMenuItem mi4 = new JMenuItem("cut");
JMenuItem mi5 = new JMenuItem("copy");
JMenuItem mi6 = new JMenuItem("paste");
// Add action listener
[Link](this);
[Link](this);
[Link](this);
[Link](mi4);
[Link](mi5);
[Link](mi6);
JMenuItem mc = new JMenuItem("close");
[Link](this);
[Link](m1);
[Link](m2);
[Link](mc);
[Link](mb);
[Link](t);
[Link](500, 500);
[Link]();
}
// If a button is pressed
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
if ([Link]("cut")) {
[Link]();
}
else if ([Link]("copy")) {
[Link]();
}
else if ([Link]("paste")) {
[Link]();
}
else if ([Link]("Save")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsSaveDialog function to show the save dialog
int r = [Link](null);
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File([Link]().getAbsolutePath());
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Create buffered writer to write
BufferedWriter w = new BufferedWriter(wr);
// Write
[Link]([Link]());
[Link]();
[Link]();
}
catch (Exception evt) {
[Link](f, [Link]());
}
}
// If the user cancelled the operation
else
[Link](f, "the user cancelled the operation");
}
else if ([Link]("Print")) {
try {
// print the file
[Link]();
}
catch (Exception evt) {
[Link](f, [Link]());
}
}
else if ([Link]("Open")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsOpenDialog function to show the save dialog
int r = [Link](null);
// If the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File([Link]().getAbsolutePath());
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new BufferedReader(fr);
// Initialize sl
sl = [Link]();
// Take the input from the file
while ((s1 = [Link]()) != null) {
sl = sl + "\n" + s1;
}
// Set the text
[Link](sl);
}
catch (Exception evt) {
[Link](f, [Link]());
}
}
// If the user cancelled the operation
else
[Link](f, "the user cancelled the operation");
}
else if ([Link]("New")) {
[Link]("");
}
else if ([Link]("close")) {
[Link](false);
}
}
// Main class
public static void main(String args[])
{
editor e = new editor();
}
}
Output: