java_practicals_with_outputs[1] (1)
java_practicals_with_outputs[1] (1)
Page 1
Java Practical Solutions
CERTIFICATE OF ACHIEVEMENT
(Computer Science & Engineering)
Mr. Mohit
Roll No.: 238032
Branch: B. Tech(AI & DS)
SEMESTER: 4th Sem
Signature
Page 2
Java Practical Solutions
CERTIFICATE OF ACHIEVEMENT
(Computer Science & Engineering)
Miss. Manya
Roll No.: 238030
Branch: B. Tech(AI & DS)
SEMESTER: 4th Sem
Date:
Signature
Page 3
Java Practical Solutions
importjava.util.*; class
StackQueue {
public static void main(String[] args) { Stack<Integer> stack = new
Stack<>(); Queue<Integer> queue = new LinkedList<>();
Output:
Page 4
Java Practical Solutions
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
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.
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
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
Output:
Page 11
Java Practical Solutions
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
Output:
Visit count: 1
(Refresh page)
Visit count: 2
(Refresh page)
Visit count: 3
Page 13
Java Practical Solutions
import java.beans.*;
this.name = name;
support.addPropertyChangeListener(pcl);
bean.addPropertyChangeListener(event ->
bean.setName("John");
bean.setName("Alex");
Output:
Page 14
Java Practical Solutions
Page 15